Skip to content

Commit

Permalink
Extract setDefaults method
Browse files Browse the repository at this point in the history
Summary:
I need this functionality extracted.
It will be invoked downstream by the reflection decoder to apply defaults to a blank struct.

Reviewed By: leoleovich

Differential Revision: D63505752

fbshipit-source-id: 6371e0d295facc44f21a8372aa5a42a9a6d0f58f
  • Loading branch information
echistyakov authored and facebook-github-bot committed Sep 27, 2024
1 parent 883b971 commit 574f160
Show file tree
Hide file tree
Showing 98 changed files with 5,695 additions and 2,307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (x *{{struct:go_name}}) Get{{field:go_name}}() {{#field:type}}{{#type:struc
{{/type:struct?}}
{{/field:type}}
}

{{/field:nilable?}}
return {{#field:non_struct_pointer?}}*{{/field:non_struct_pointer?}}x.{{field:go_name}}
}
Expand Down
3 changes: 3 additions & 0 deletions thrift/compiler/generate/templates/go/types/struct.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

{{! string }}
{{> types/struct_method_string}}

{{! setDefaults }}
{{> types/struct_method_set_defaults}}
{{#struct:exception?}}

{{! error }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,6 @@
}}
func {{struct:go_qualified_new_func}}() *{{struct:go_name}} {
return (&{{struct:go_name}}{}){{!
}}{{^struct:union?}}{{!
}}{{#struct:fields_sorted}}{{!
// Fields with given default values.
}}{{#field:value}}.
{{field:go_setter_name}}{{#program:compat_setters?}}NonCompat{{/program:compat_setters?}}({{!
}}{{#field:type}}{{!
}}{{^type:base?}}
{{> const/value}},
{{/type:base?}}{{!
}}{{#type:base?}}{{!
}}{{> const/value}}{{!
}}{{/type:base?}}{{!
}}{{/field:type}}{{!
}}){{/field:value}}{{!
// Non-optional fields without given default values.
}}{{^field:value}}{{!
}}{{^field:optional?}}.
{{field:go_setter_name}}{{#program:compat_setters?}}NonCompat{{/program:compat_setters?}}({{!
}}{{#field:type}}{{!
}}{{> common/natural_default_value}}{{!
}}{{/field:type}}{{!
}}){{/field:optional?}}{{!
}}{{/field:value}}{{!
}}{{/struct:fields_sorted}}{{!
}}{{/struct:union?}}
return (&{{struct:go_name}}{}).setDefaults()
}
{{!newline}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{{!
Copyright (c) Meta Platforms, Inc. and affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}}{{!
This template creates a setDefaults method for Thrift structs.
}}
func (x *{{struct:go_name}}) setDefaults() *{{struct:go_name}} {
return x{{!
}}{{^struct:union?}}{{!
}}{{#struct:fields_sorted}}{{!
// Fields with given default values.
}}{{#field:value}}.
{{field:go_setter_name}}{{#program:compat_setters?}}NonCompat{{/program:compat_setters?}}({{!
}}{{#field:type}}{{!
}}{{^type:base?}}
{{> const/value}},
{{/type:base?}}{{!
}}{{#type:base?}}{{!
}}{{> const/value}}{{!
}}{{/type:base?}}{{!
}}{{/field:type}}{{!
}}){{/field:value}}{{!
// Non-optional fields without given default values.
}}{{^field:value}}{{!
}}{{^field:optional?}}.
{{field:go_setter_name}}{{#program:compat_setters?}}NonCompat{{/program:compat_setters?}}({{!
}}{{#field:type}}{{!
}}{{> common/natural_default_value}}{{!
}}{{/field:type}}{{!
}}){{/field:optional?}}{{!
}}{{/field:value}}{{!
}}{{/struct:fields_sorted}}{{!
}}{{/struct:union?}}
}
{{!newline}}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func (x *{{struct:go_name}}) Write(p thrift.Encoder) error {
if err := x.writeField{{field:key_str}}(p); err != nil {
return err
}

{{/struct:fields_sorted}}

if err := p.WriteFieldStop(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", x), err)
}
Expand Down
52 changes: 35 additions & 17 deletions thrift/compiler/test/fixtures/adapter/out/go/gen-go/module/svcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ var _ thrift.Struct = (*reqServiceFunc)(nil)
type ServiceFuncArgsDeprecated = reqServiceFunc

func newReqServiceFunc() *reqServiceFunc {
return (&reqServiceFunc{}).
SetArg1NonCompat(NewStringWithAdapter_7208()).
SetArg2NonCompat("").
SetArg3NonCompat(NewFoo())
return (&reqServiceFunc{}).setDefaults()
}

func (x *reqServiceFunc) GetArg1() StringWithAdapter_7208 {
Expand All @@ -130,7 +127,6 @@ func (x *reqServiceFunc) GetArg3() *Foo {
if !x.IsSetArg3() {
return nil
}

return x.Arg3
}

Expand Down Expand Up @@ -282,11 +278,9 @@ func (x *reqServiceFunc) Write(p thrift.Encoder) error {
if err := x.writeField1(p); err != nil {
return err
}

if err := x.writeField2(p); err != nil {
return err
}

if err := x.writeField3(p); err != nil {
return err
}
Expand Down Expand Up @@ -359,6 +353,13 @@ func (x *reqServiceFunc) String() string {

return sb.String()
}
func (x *reqServiceFunc) setDefaults() *reqServiceFunc {
return x.
SetArg1NonCompat(NewStringWithAdapter_7208()).
SetArg2NonCompat("").
SetArg3NonCompat(NewFoo())
}

type respServiceFunc struct {
Success *MyI32_4873 `thrift:"success,0,optional" json:"success,omitempty" db:"success"`
}
Expand All @@ -370,14 +371,13 @@ var _ thrift.WritableResult = (*respServiceFunc)(nil)
type ServiceFuncResultDeprecated = respServiceFunc

func newRespServiceFunc() *respServiceFunc {
return (&respServiceFunc{})
return (&respServiceFunc{}).setDefaults()
}

func (x *respServiceFunc) GetSuccess() MyI32_4873 {
if !x.IsSetSuccess() {
return NewMyI32_4873()
}

return *x.Success
}

Expand Down Expand Up @@ -511,6 +511,10 @@ func (x *respServiceFunc) String() string {

return sb.String()
}
func (x *respServiceFunc) setDefaults() *respServiceFunc {
return x
}



type ServiceProcessor struct {
Expand Down Expand Up @@ -717,7 +721,7 @@ var _ thrift.Struct = (*reqAdapterServiceCount)(nil)
type AdapterServiceCountArgsDeprecated = reqAdapterServiceCount

func newReqAdapterServiceCount() *reqAdapterServiceCount {
return (&reqAdapterServiceCount{})
return (&reqAdapterServiceCount{}).setDefaults()
}


Expand All @@ -727,6 +731,7 @@ func (x *reqAdapterServiceCount) Write(p thrift.Encoder) error {
return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", x), err)
}


if err := p.WriteFieldStop(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", x), err)
}
Expand Down Expand Up @@ -786,6 +791,10 @@ func (x *reqAdapterServiceCount) String() string {

return sb.String()
}
func (x *reqAdapterServiceCount) setDefaults() *reqAdapterServiceCount {
return x
}

type respAdapterServiceCount struct {
Success *CountingStruct `thrift:"success,0,optional" json:"success,omitempty" db:"success"`
}
Expand All @@ -797,14 +806,13 @@ var _ thrift.WritableResult = (*respAdapterServiceCount)(nil)
type AdapterServiceCountResultDeprecated = respAdapterServiceCount

func newRespAdapterServiceCount() *respAdapterServiceCount {
return (&respAdapterServiceCount{})
return (&respAdapterServiceCount{}).setDefaults()
}

func (x *respAdapterServiceCount) GetSuccess() *CountingStruct {
if !x.IsSetSuccess() {
return nil
}

return x.Success
}

Expand Down Expand Up @@ -942,6 +950,10 @@ func (x *respAdapterServiceCount) String() string {

return sb.String()
}
func (x *respAdapterServiceCount) setDefaults() *respAdapterServiceCount {
return x
}

type reqAdapterServiceAdaptedTypes struct {
Arg *HeapAllocated `thrift:"arg,1" json:"arg" db:"arg"`
}
Expand All @@ -952,15 +964,13 @@ var _ thrift.Struct = (*reqAdapterServiceAdaptedTypes)(nil)
type AdapterServiceAdaptedTypesArgsDeprecated = reqAdapterServiceAdaptedTypes

func newReqAdapterServiceAdaptedTypes() *reqAdapterServiceAdaptedTypes {
return (&reqAdapterServiceAdaptedTypes{}).
SetArgNonCompat(NewHeapAllocated())
return (&reqAdapterServiceAdaptedTypes{}).setDefaults()
}

func (x *reqAdapterServiceAdaptedTypes) GetArg() *HeapAllocated {
if !x.IsSetArg() {
return nil
}

return x.Arg
}

Expand Down Expand Up @@ -1094,6 +1104,11 @@ func (x *reqAdapterServiceAdaptedTypes) String() string {

return sb.String()
}
func (x *reqAdapterServiceAdaptedTypes) setDefaults() *reqAdapterServiceAdaptedTypes {
return x.
SetArgNonCompat(NewHeapAllocated())
}

type respAdapterServiceAdaptedTypes struct {
Success *HeapAllocated `thrift:"success,0,optional" json:"success,omitempty" db:"success"`
}
Expand All @@ -1105,14 +1120,13 @@ var _ thrift.WritableResult = (*respAdapterServiceAdaptedTypes)(nil)
type AdapterServiceAdaptedTypesResultDeprecated = respAdapterServiceAdaptedTypes

func newRespAdapterServiceAdaptedTypes() *respAdapterServiceAdaptedTypes {
return (&respAdapterServiceAdaptedTypes{})
return (&respAdapterServiceAdaptedTypes{}).setDefaults()
}

func (x *respAdapterServiceAdaptedTypes) GetSuccess() *HeapAllocated {
if !x.IsSetSuccess() {
return nil
}

return x.Success
}

Expand Down Expand Up @@ -1250,6 +1264,10 @@ func (x *respAdapterServiceAdaptedTypes) String() string {

return sb.String()
}
func (x *respAdapterServiceAdaptedTypes) setDefaults() *respAdapterServiceAdaptedTypes {
return x
}



type AdapterServiceProcessor struct {
Expand Down
Loading

0 comments on commit 574f160

Please sign in to comment.