Skip to content

Commit

Permalink
Generate non-reference type fields in structs as pointers to such types
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuntao Lu committed Mar 28, 2017
1 parent f5ba8f3 commit 44ccfea
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 34 deletions.
11 changes: 10 additions & 1 deletion codegen/method.go
Expand Up @@ -113,6 +113,12 @@ func (ms *MethodSpec) setRequestType(curThriftFile string, funcSpec *compile.Fun
return nil
}

func isStructType(spec compile.TypeSpec) bool {
spec = compile.RootTypeSpec(spec)
_, isStruct := spec.(*compile.StructSpec)
return isStruct
}

func (ms *MethodSpec) newRequestType(curThriftFile string, f *compile.FunctionSpec, h *PackageHelper) (string, error) {
requestType := strings.Title(f.Name) + "HTTPRequest"
ms.RequestStruct = make([]StructSpec, len(f.ArgsSpec))
Expand All @@ -121,6 +127,9 @@ func (ms *MethodSpec) newRequestType(curThriftFile string, f *compile.FunctionSp
if err != nil {
return "", errors.Wrap(err, "failed to generate new request type")
}
if isStructType(arg.Type) {
typeName = "*" + typeName
}
ms.RequestStruct[i] = StructSpec{
Type: typeName,
Name: arg.Name,
Expand Down Expand Up @@ -328,7 +337,7 @@ func (ms *MethodSpec) setRequestFieldMap(
default:
thriftPkgNameParts := strings.Split(field.Type.ThriftFile(), "/")
thriftPkgName := thriftPkgNameParts[len(thriftPkgNameParts)-2]
ms.RequestTypeMap[field.Name] = "clientType" + strings.Title(thriftPkgName) + "." + field.Type.ThriftName()
ms.RequestTypeMap[field.Name] = "(*clientType" + strings.Title(thriftPkgName) + "." + field.Type.ThriftName() + ")"
}
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions codegen/test_data/bar.json
Expand Up @@ -176,7 +176,7 @@
"RequestBoxed": false,
"RequestStruct": [
{
"Type": "bar.BarRequest",
"Type": "*bar.BarRequest",
"Name": "request",
"Annotations": null
}
Expand Down Expand Up @@ -226,12 +226,12 @@
"RequestBoxed": false,
"RequestStruct": [
{
"Type": "bar.BarRequest",
"Type": "*bar.BarRequest",
"Name": "request",
"Annotations": null
},
{
"Type": "foo.FooStruct",
"Type": "*foo.FooStruct",
"Name": "foo",
"Annotations": null
}
Expand Down
6 changes: 3 additions & 3 deletions codegen/test_data/clients/bar_structs.gogen
Expand Up @@ -23,11 +23,11 @@ type ArgNotStructHTTPRequest struct {

// NormalHTTPRequest is the http body type for endpoint normal.
type NormalHTTPRequest struct {
Request bar.BarRequest
Request *bar.BarRequest
}

// TooManyArgsHTTPRequest is the http body type for endpoint tooManyArgs.
type TooManyArgsHTTPRequest struct {
Request bar.BarRequest
Foo foo.FooStruct
Request *bar.BarRequest
Foo *foo.FooStruct
}
2 changes: 1 addition & 1 deletion codegen/test_data/endpoints/bar_bar_method_normal.gogen
Expand Up @@ -73,7 +73,7 @@ func HandleNormalRequest(
func convertToNormalClientRequest(body *NormalHTTPRequest) *barClient.NormalHTTPRequest {
clientRequest := &barClient.NormalHTTPRequest{}

clientRequest.Request = clientTypeBar.BarRequest(body.Request)
clientRequest.Request = (*clientTypeBar.BarRequest)(body.Request)

return clientRequest
}
Expand Down
4 changes: 2 additions & 2 deletions codegen/test_data/endpoints/bar_bar_method_toomanyargs.gogen
Expand Up @@ -77,8 +77,8 @@ func HandleTooManyArgsRequest(
func convertToTooManyArgsClientRequest(body *TooManyArgsHTTPRequest) *barClient.TooManyArgsHTTPRequest {
clientRequest := &barClient.TooManyArgsHTTPRequest{}

clientRequest.Foo = clientTypeFoo.FooStruct(body.Foo)
clientRequest.Request = clientTypeBar.BarRequest(body.Request)
clientRequest.Foo = (*clientTypeFoo.FooStruct)(body.Foo)
clientRequest.Request = (*clientTypeBar.BarRequest)(body.Request)

return clientRequest
}
Expand Down
6 changes: 3 additions & 3 deletions codegen/test_data/endpoints/bar_structs.gogen
Expand Up @@ -23,11 +23,11 @@ type ArgNotStructHTTPRequest struct {

// NormalHTTPRequest is the http body type for endpoint normal.
type NormalHTTPRequest struct {
Request bar.BarRequest
Request *bar.BarRequest
}

// TooManyArgsHTTPRequest is the http body type for endpoint tooManyArgs.
type TooManyArgsHTTPRequest struct {
Request bar.BarRequest
Foo foo.FooStruct
Request *bar.BarRequest
Foo *foo.FooStruct
}
6 changes: 3 additions & 3 deletions examples/example-gateway/build/clients/bar/bar_structs.go

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

50 changes: 44 additions & 6 deletions examples/example-gateway/build/clients/bar/bar_structs_easyjson.go
Expand Up @@ -7,6 +7,8 @@ import (
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
bar "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/github.com/uber/zanzibar/clients/bar/bar"
foo "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/github.com/uber/zanzibar/clients/foo/foo"
)

// suppress unused package warning
Expand Down Expand Up @@ -37,9 +39,25 @@ func easyjsonCa6a3ed2DecodeGithubComUberZanzibarExamplesExampleGatewayBuildClien
}
switch key {
case "Request":
(out.Request).UnmarshalEasyJSON(in)
if in.IsNull() {
in.Skip()
out.Request = nil
} else {
if out.Request == nil {
out.Request = new(bar.BarRequest)
}
(*out.Request).UnmarshalEasyJSON(in)
}
case "Foo":
(out.Foo).UnmarshalEasyJSON(in)
if in.IsNull() {
in.Skip()
out.Foo = nil
} else {
if out.Foo == nil {
out.Foo = new(foo.FooStruct)
}
(*out.Foo).UnmarshalEasyJSON(in)
}
default:
in.SkipRecursive()
}
Expand All @@ -59,13 +77,21 @@ func easyjsonCa6a3ed2EncodeGithubComUberZanzibarExamplesExampleGatewayBuildClien
}
first = false
out.RawString("\"Request\":")
(in.Request).MarshalEasyJSON(out)
if in.Request == nil {
out.RawString("null")
} else {
(*in.Request).MarshalEasyJSON(out)
}
if !first {
out.RawByte(',')
}
first = false
out.RawString("\"Foo\":")
(in.Foo).MarshalEasyJSON(out)
if in.Foo == nil {
out.RawString("null")
} else {
(*in.Foo).MarshalEasyJSON(out)
}
out.RawByte('}')
}

Expand Down Expand Up @@ -112,7 +138,15 @@ func easyjsonCa6a3ed2DecodeGithubComUberZanzibarExamplesExampleGatewayBuildClien
}
switch key {
case "Request":
(out.Request).UnmarshalEasyJSON(in)
if in.IsNull() {
in.Skip()
out.Request = nil
} else {
if out.Request == nil {
out.Request = new(bar.BarRequest)
}
(*out.Request).UnmarshalEasyJSON(in)
}
default:
in.SkipRecursive()
}
Expand All @@ -132,7 +166,11 @@ func easyjsonCa6a3ed2EncodeGithubComUberZanzibarExamplesExampleGatewayBuildClien
}
first = false
out.RawString("\"Request\":")
(in.Request).MarshalEasyJSON(out)
if in.Request == nil {
out.RawString("null")
} else {
(*in.Request).MarshalEasyJSON(out)
}
out.RawByte('}')
}

Expand Down

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

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

6 changes: 3 additions & 3 deletions examples/example-gateway/build/endpoints/bar/bar_structs.go

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

Expand Up @@ -7,6 +7,8 @@ import (
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
bar "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/github.com/uber/zanzibar/endpoints/bar/bar"
foo "github.com/uber/zanzibar/examples/example-gateway/build/gen-code/github.com/uber/zanzibar/endpoints/foo/foo"
)

// suppress unused package warning
Expand Down Expand Up @@ -37,9 +39,25 @@ func easyjsonCa6a3ed2DecodeGithubComUberZanzibarExamplesExampleGatewayBuildEndpo
}
switch key {
case "Request":
(out.Request).UnmarshalEasyJSON(in)
if in.IsNull() {
in.Skip()
out.Request = nil
} else {
if out.Request == nil {
out.Request = new(bar.BarRequest)
}
(*out.Request).UnmarshalEasyJSON(in)
}
case "Foo":
(out.Foo).UnmarshalEasyJSON(in)
if in.IsNull() {
in.Skip()
out.Foo = nil
} else {
if out.Foo == nil {
out.Foo = new(foo.FooStruct)
}
(*out.Foo).UnmarshalEasyJSON(in)
}
default:
in.SkipRecursive()
}
Expand All @@ -59,13 +77,21 @@ func easyjsonCa6a3ed2EncodeGithubComUberZanzibarExamplesExampleGatewayBuildEndpo
}
first = false
out.RawString("\"Request\":")
(in.Request).MarshalEasyJSON(out)
if in.Request == nil {
out.RawString("null")
} else {
(*in.Request).MarshalEasyJSON(out)
}
if !first {
out.RawByte(',')
}
first = false
out.RawString("\"Foo\":")
(in.Foo).MarshalEasyJSON(out)
if in.Foo == nil {
out.RawString("null")
} else {
(*in.Foo).MarshalEasyJSON(out)
}
out.RawByte('}')
}

Expand Down Expand Up @@ -112,7 +138,15 @@ func easyjsonCa6a3ed2DecodeGithubComUberZanzibarExamplesExampleGatewayBuildEndpo
}
switch key {
case "Request":
(out.Request).UnmarshalEasyJSON(in)
if in.IsNull() {
in.Skip()
out.Request = nil
} else {
if out.Request == nil {
out.Request = new(bar.BarRequest)
}
(*out.Request).UnmarshalEasyJSON(in)
}
default:
in.SkipRecursive()
}
Expand All @@ -132,7 +166,11 @@ func easyjsonCa6a3ed2EncodeGithubComUberZanzibarExamplesExampleGatewayBuildEndpo
}
first = false
out.RawString("\"Request\":")
(in.Request).MarshalEasyJSON(out)
if in.Request == nil {
out.RawString("null")
} else {
(*in.Request).MarshalEasyJSON(out)
}
out.RawByte('}')
}

Expand Down

0 comments on commit 44ccfea

Please sign in to comment.