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

Thrift compiler generates uncompilable go code around optional constants

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 0.14.1
    • Fix Version/s: 0.15.0
    • Component/s: Go - Compiler
    • Labels:
      None

      Description

      This is essentially the same issue as THRIFT-4253, just the fix of THRIFT-4253 was not complete.

      THRIFT-4253 fixed the primitive types, but does not work for enums. Here's a minimal reproducible thrift file:

      namespace go foo
      
      enum Foo {
        One = 1,
        Two = 2,
      }
      
      struct Bar {
        1: optional Foo foo,
      }
      
      const list<Bar> CONSTANTS = [
        {
          "foo": Foo.One,
        },
      ]
      

      thrift compiler generated go code:

      $ cat gen-go/foo/foo-consts.go 
      // Code generated by Thrift Compiler (0.14.1). DO NOT EDIT.
      
      package foo
      
      import(
              "bytes"
              "context"
              "fmt"
              "time"
              "github.com/apache/thrift/lib/go/thrift"
      )
      
      // (needed to ensure safety because of naive import list construction.)
      var _ = thrift.ZERO
      var _ = fmt.Printf
      var _ = context.Background
      var _ = time.Now
      var _ = bytes.Equal
      
      var CONSTANTS []*Bar
      
      func init() {
      CONSTANTS = []*Bar{
        &Bar{
          Foo:     1,
      }, }
      
      }
      

      While Bar.Foo is supposed to be a pointer (as it's optional).

      This bug also applies to typedef'd types as well:

      namespace go typedefstring
      
      typedef string Foo
      
      struct Struct {
        1: optional Foo foo,
      }
      
      const list<Struct> CONSTANTS = [
        {
          "foo": "hello",
        },
      ]
      
      $ cat gen-go/typedefstring/typedefstring-consts.go 
      // Code generated by Thrift Compiler (0.14.1). DO NOT EDIT.
      
      package typedefstring
      
      import(
              "bytes"
              "context"
              "fmt"
              "time"
              "github.com/apache/thrift/lib/go/thrift"
      )
      
      // (needed to ensure safety because of naive import list construction.)
      var _ = thrift.ZERO
      var _ = fmt.Printf
      var _ = context.Background
      var _ = time.Now
      var _ = bytes.Equal
      
      var CONSTANTS []*Struct
      
      func init() {
      CONSTANTS = []*Struct{
        &Struct{
          Foo: &(&struct{x string}{"hello"}).x,
      }, }
      
      }
      

      In const code we try to assign a *string into *Foo field.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                fishywang Yuxuan Wang
                Reporter:
                fishywang Yuxuan Wang
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m