From b5ef551512a640838a9e88cb137f3d82117c393f Mon Sep 17 00:00:00 2001 From: Keyi Xie Date: Fri, 1 Dec 2023 11:25:45 +0800 Subject: [PATCH] feat: support new generate flag generate_json_str (#120) --- generator/golang/imports.go | 1 + generator/golang/option.go | 5 +++- generator/golang/templates/slim/slim.go | 6 +++++ generator/golang/templates/struct.go | 7 ++++++ generator/golang/util.go | 1 + test/golang/cases_and_options/run_test.sh | 1 + utils/json_utils/json_utils.go | 29 +++++++++++++++++++++++ 7 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 utils/json_utils/json_utils.go diff --git a/generator/golang/imports.go b/generator/golang/imports.go index 70ebd923..8b70a008 100644 --- a/generator/golang/imports.go +++ b/generator/golang/imports.go @@ -87,6 +87,7 @@ func (im *importManager) init(cu *CodeUtils, ast *parser.Thrift) { "unknown": DefaultUnknownLib, "meta": DefaultMetaLib, "thrift_reflection": ThriftReflectionLib, + "json_utils": ThriftJSONUtilLib, "fieldmask": ThriftFieldMaskLib, } for pkg, path := range std { diff --git a/generator/golang/option.go b/generator/golang/option.go index 0bbb4943..45553456 100644 --- a/generator/golang/option.go +++ b/generator/golang/option.go @@ -55,7 +55,9 @@ type Features struct { CodeRef bool `code_ref:"Genenerate code ref by given idl-ref.yaml"` KeepCodeRefName bool `keep_code_ref_name:"Genenerate code ref but still keep file name."` TrimIDL bool `trim_idl:"Simplify IDL to the most concise form before generating code."` - WithFieldMask bool `with_field_mask:"Support field-mask for generated code."` + + JSONStringer bool `json_stringer:"Generate the JSON marshal method in String() method."` + WithFieldMask bool `with_field_mask:"Support field-mask for generated code."` } var defaultFeatures = Features{ @@ -86,6 +88,7 @@ var defaultFeatures = Features{ GenerateReflectionInfo: false, EnumAsINT32: false, TrimIDL: false, + JSONStringer: false, WithFieldMask: false, } diff --git a/generator/golang/templates/slim/slim.go b/generator/golang/templates/slim/slim.go index f3c4cac2..74d61381 100644 --- a/generator/golang/templates/slim/slim.go +++ b/generator/golang/templates/slim/slim.go @@ -92,11 +92,17 @@ func (p *{{$TypeName}}) CarryingUnknownFields() bool { {{template "FieldIsSet" .}} func (p *{{$TypeName}}) String() string { + {{- if Features.JSONStringer}} + {{- UseStdLibrary "json_utils"}} + JsonBytes , _ := json_utils.JSONFunc(p) + return string(JsonBytes) + {{- else}} if p == nil { return "" } {{- UseStdLibrary "fmt"}} return fmt.Sprintf("{{$TypeName}}(%+v)", *p) + {{- end}} } {{- if eq .Category "exception"}} diff --git a/generator/golang/templates/struct.go b/generator/golang/templates/struct.go index 6b02c23c..b19ce7a6 100644 --- a/generator/golang/templates/struct.go +++ b/generator/golang/templates/struct.go @@ -107,11 +107,18 @@ var fieldIDToName_{{$TypeName}} = map[int16]string{ {{template "StructLikeWriteField" .}} func (p *{{$TypeName}}) String() string { + {{- if Features.JSONStringer}} + {{- UseStdLibrary "json_utils"}} + JsonBytes , _ := json_utils.JSONFunc(p) + return string(JsonBytes) + {{- else}} if p == nil { return "" } {{- UseStdLibrary "fmt"}} return fmt.Sprintf("{{$TypeName}}(%+v)", *p) + {{- end}} + } {{- if eq .Category "exception"}} diff --git a/generator/golang/util.go b/generator/golang/util.go index 327ed54e..6ddced83 100644 --- a/generator/golang/util.go +++ b/generator/golang/util.go @@ -46,6 +46,7 @@ const ( ThriftFieldMaskLib = "github.com/cloudwego/thriftgo/fieldmask" ThriftOptionLib = "github.com/cloudwego/thriftgo/option" defaultTemplate = "default" + ThriftJSONUtilLib = "github.com/cloudwego/thriftgo/utils/json_utils" ) var escape = regexp.MustCompile(`\\.`) diff --git a/test/golang/cases_and_options/run_test.sh b/test/golang/cases_and_options/run_test.sh index 10334292..b670bee1 100755 --- a/test/golang/cases_and_options/run_test.sh +++ b/test/golang/cases_and_options/run_test.sh @@ -44,6 +44,7 @@ features=(\ nil_safe \ frugal_tag \ unescape_double_quote \ + json_stringer \ ) run_cases() { diff --git a/utils/json_utils/json_utils.go b/utils/json_utils/json_utils.go new file mode 100644 index 00000000..f8233704 --- /dev/null +++ b/utils/json_utils/json_utils.go @@ -0,0 +1,29 @@ +// Copyright 2023 CloudWeGo Authors +// +// 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. + +package json_utils + +import "encoding/json" + +// jsonMarshaler customize json.Marshal as you like +type jsonMarshaler func(v interface{}) ([]byte, error) + +var JSONFunc jsonMarshaler = json.Marshal + +// ResetJSONMarshalFunc replace the JSON marshal func +// to all code which are generated by thriftgo. +// Usually, it is used in String() method. +func ResetJSONMarshalFunc(jn jsonMarshaler) { + JSONFunc = jn +}