Skip to content

Commit

Permalink
func (v <T>) Ptr() *<T> for primitive typedefs
Browse files Browse the repository at this point in the history
Using typedefs on a base type is a common thrift pattern used to
describe the kind of value that is within. While structs are trivial to
form a pointer to (`&Foo{"foo"}`), this is not the case with primitive
types (`type Jack string`, `&Jack("RJ45")`). This adds `<T>.Ptr()`
methods for primitive type aliases (`Jack("RJ45").Ptr()`).
  • Loading branch information
Tristan Willy authored and twilly committed Mar 28, 2019
1 parent 47ade7d commit 84946ed
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gen/internal/tests/collision/types.go

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

5 changes: 5 additions & 0 deletions gen/internal/tests/services/types.go

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

15 changes: 15 additions & 0 deletions gen/internal/tests/typedefs/types.go

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

5 changes: 5 additions & 0 deletions gen/internal/tests/uuid_conflict/types.go

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

33 changes: 33 additions & 0 deletions gen/quick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ func isThriftNillable(typ reflect.Type) bool {
return false
}

// isThriftPrimitive returns true for Go types that are considered primitive
// (see gen.isPrimitiveType).
func isThriftPrimitive(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8,
reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32,
reflect.Float64, reflect.String:
return true
}
return false
}

type thriftKind int

const (
Expand Down Expand Up @@ -531,6 +544,15 @@ func TestQuickSuite(t *testing.T) {
t.Run("Accessors/IsSetOnNil", func(t *testing.T) {
suite.testIsSetAccessorsOnNil(t)
})

case thriftTypedef:
if isThriftPrimitive(typ) {
t.Run("Ptr", func(t *testing.T) {
for _, give := range values {
suite.testTypedefPrimitivePtr(t, give)
}
})
}
}

if !tt.NoLog {
Expand Down Expand Up @@ -783,6 +805,17 @@ func (q *quickSuite) testEnumPtr(t *testing.T, give thriftType) {
"pointer must point back to original value")
}

// Tests that Ptr methods on typedefs exist and return the same value back.
func (q *quickSuite) testTypedefPrimitivePtr(t *testing.T, give thriftType) {
v := reflect.ValueOf(give)
method := v.MethodByName("Ptr")
require.True(t, method.IsValid(), "expected Ptr method to be present")
ptr := method.Call(nil)[0]
require.Equal(t, reflect.Ptr, ptr.Kind(), "must be a pointer")
assert.Equal(t, give, ptr.Interface(),
"pointer must point back to original value")
}

// Tests that each field of a struct has an accessor that returns the same
// value as the field.
func (q *quickSuite) testGetAccessors(t *testing.T, giveVal thriftType) {
Expand Down
8 changes: 8 additions & 0 deletions gen/typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func typedef(g Generator, spec *compile.TypedefSpec) error {
<$v := newVar "v">
<$x := newVar "x">
<- if isPrimitiveType .>
// <typeName .>Ptr returns a pointer to a <$typedefType>
func (<$v> <typeName .>) Ptr() *<$typedefType>{
return &<$v>
}
<- end>
// ToWire translates <typeName .> into a Thrift-level intermediate
// representation. This intermediate representation may be serialized
// into bytes using a ThriftRW protocol implementation.
Expand Down
4 changes: 4 additions & 0 deletions gen/typedef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,7 @@ func TestTypedefAccessors(t *testing.T) {
})
})
}

func TestTypedefPtr(t *testing.T) {
assert.Equal(t, td.State("foo"), *td.State("foo").Ptr())
}
10 changes: 10 additions & 0 deletions plugin/api/types.go

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

0 comments on commit 84946ed

Please sign in to comment.