Skip to content

Commit

Permalink
string typedef: Fix fmt import and return type bugs (#568)
Browse files Browse the repository at this point in the history
PR (#554) introduced an optimization for the String() method of typedefs.

There are two issues with this change:
- Since the "fmt" import was global, if fmt.Sprint was dropped from the typedef code generation the import would be unused.
- Using compiled.RootTypeSpec for isStringType results in situations where a non-string type is return from a String() function resulting in uncompilable code. func(v StringReDef) String() string { x := (stringdef.StringDef)(v) return x } This happens when a type is a typedef of another string typedef.

I've added generated code cases to demonstrate and verify the corrected behavior.
  • Loading branch information
r-hang committed Apr 6, 2023
1 parent 51abcb3 commit cec3b68
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 7 deletions.
2 changes: 2 additions & 0 deletions gen/internal/tests/collision/collision.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions gen/internal/tests/nozap/nozap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gen/internal/tests/services/services.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions gen/internal/tests/set_to_slice/set_to_slice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions gen/internal/tests/stringdef/stringdef.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gen/internal/tests/structs/structs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gen/internal/tests/thrift/stringdef.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef string StringDef
3 changes: 3 additions & 0 deletions gen/internal/tests/thrift/typedefs.thrift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include "./structs.thrift"
include "./enums.thrift"
include "./stringdef.thrift"

/**
* Number of seconds since epoch.
Expand All @@ -9,6 +10,8 @@ include "./enums.thrift"
typedef i64 Timestamp // alias of primitive
typedef string State

typedef stringdef.StringDef StringReDef // alias of an alias of a primitive

typedef i128 UUID // alias of struct

typedef UUID MyUUID // alias of alias
Expand Down
79 changes: 76 additions & 3 deletions gen/internal/tests/typedefs/typedefs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gen/internal/tests/uuid_conflict/uuid_conflict.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cec3b68

Please sign in to comment.