Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4334

Perl indentation incorrect when defaulting field attribute to a struct

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Trivial
    • Resolution: Fixed
    • 0.10.0
    • 0.11.0
    • Perl - Compiler
    • None

    Description

      Improper indentation is used when defaulting an attribute in a struct to another struct or hash:

      See the following example thrift

      struct Object {
        1: map<string, string> hashWithDefault = {"foo": "bar"}
        2: list<string> arrayWithDefault = ["foo", "bar", "baz"]
      }
      struct OtherObject {
        1: Object objectWithDefault = {"hashWithDefault": {"baz": "bat"}, "arrayWithDefault": ["a", "b", "c"]}
        2: string primitiveStringType
      }
      

      Object constructor: This uses improper indentation when specifying the hash keys to use in the initialization of hashWithDefault and arrayWithDefault, but the indentation is returned to the correct level afterwards

      package Object;
      use base qw(Class::Accessor);
      Object->mk_accessors( qw( hashWithDefault arrayWithDefault ) );
      
      sub new {
        my $classname = shift;
        my $self      = {};
        my $vals      = shift || {};
        $self->{hashWithDefault} = {
      "foo" => "bar",
      };
        $self->{arrayWithDefault} = [
      "foo",
      "bar",
      "baz",
      ];
        if (UNIVERSAL::isa($vals,'HASH')) {
          if (defined $vals->{hashWithDefault}) {
            $self->{hashWithDefault} = $vals->{hashWithDefault};
          }
          if (defined $vals->{arrayWithDefault}) {
            $self->{arrayWithDefault} = $vals->{arrayWithDefault};
          }
        }
        return bless ($self, $classname);
      }
      

      OtherObject constructor: This uses improper indentation when specifying the hash keys to use in the initialization of Object, but the indentation is NOT returned to the correct level afterwards

      package OtherObject;
      use base qw(Class::Accessor);
      OtherObject->mk_accessors( qw( objectWithDefault primitiveStringType ) );
      
      sub new {
        my $classname = shift;
        my $self      = {};
        my $vals      = shift || {};
        $self->{objectWithDefault} = undef;
        $self->{primitiveStringType} = undef;
        $self->{objectWithDefault} = new Object({
      "hashWithDefault" => {
      "baz" => "bat",
      },
      "arrayWithDefault" => [
      "a",
      "b",
      "c",
      ],
      });
          if (UNIVERSAL::isa($vals,'HASH')) {
            if (defined $vals->{objectWithDefault}) {
              $self->{objectWithDefault} = $vals->{objectWithDefault};
            }
            if (defined $vals->{primitiveStringType}) {
              $self->{primitiveStringType} = $vals->{primitiveStringType};
            }
          }
          return bless ($self, $classname);
      }
      

      Since this indentation bug is additive, every struct in the file that default instantiates a struct in its constructor will offset the indentation to the right by one level.

      The cause of this bug looks to be in t_perl_generator::render_const_value(), which does not indent() the keys set in the sub-object constructors. It also does not set indent_down() at the end of instantiating the object.

      Attachments

        Issue Links

          Activity

            People

              jking3 James E. King III
              bforbis Brian Forbis
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: