Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support new generate flag generate_json_str #120

Merged
merged 8 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generator/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -86,6 +88,7 @@ var defaultFeatures = Features{
GenerateReflectionInfo: false,
EnumAsINT32: false,
TrimIDL: false,
JSONStringer: false,
WithFieldMask: false,
}

Expand Down
6 changes: 6 additions & 0 deletions generator/golang/templates/slim/slim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<nil>"
}
{{- UseStdLibrary "fmt"}}
return fmt.Sprintf("{{$TypeName}}(%+v)", *p)
{{- end}}
}

{{- if eq .Category "exception"}}
Expand Down
7 changes: 7 additions & 0 deletions generator/golang/templates/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<nil>"
}
{{- UseStdLibrary "fmt"}}
return fmt.Sprintf("{{$TypeName}}(%+v)", *p)
{{- end}}

}

{{- if eq .Category "exception"}}
Expand Down
1 change: 1 addition & 0 deletions generator/golang/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`\\.`)
Expand Down
1 change: 1 addition & 0 deletions test/golang/cases_and_options/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ features=(\
nil_safe \
frugal_tag \
unescape_double_quote \
json_stringer \
)

run_cases() {
Expand Down
29 changes: 29 additions & 0 deletions utils/json_utils/json_utils.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading