Skip to content

Commit

Permalink
Address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
xiekeyi98 committed Sep 4, 2023
1 parent c562932 commit b5a7eff
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 45 deletions.
2 changes: 1 addition & 1 deletion generator/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (im *importManager) init(cu *CodeUtils, ast *parser.Thrift) {
"unknown": DefaultUnknownLib,
"meta": DefaultMetaLib,
"thrift_reflection": ThriftReflectionLib,
"json": "encoding/json",
"json_utils": ThriftJSONUtilLib,
}
for pkg, path := range std {
ns.Add(pkg, path)
Expand Down
54 changes: 27 additions & 27 deletions generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,36 @@ type Features struct {
CodeRef bool `code_ref:"Genenerate code ref by given idl-ref.yaml"`
TrimIDL bool `trim_idl:"Simplify IDL to the most concise form before generating code."`

GenerateJSONStringMethod bool `generate_json_string_method:"Generate the JSON marshal method as String()."`
JSONStringer bool `json_stringer:"Generate the JSON marshal method in String() method."`
}

var defaultFeatures = Features{
MarshalEnumToText: false,
GenerateSetter: false,
GenDatabaseTag: false,
GenOmitEmptyTag: true,
TypedefAsTypeAlias: true,
ValidateSet: true,
ValueTypeForSIC: false,
ScanValueForEnum: true,
ReorderFields: false,
TypedEnumString: false,
KeepUnknownFields: false,
GenDeepEqual: false,
CompatibleNames: false,
ReserveComments: false,
NilSafe: false,
FrugalTag: false,
EscapeDoubleInTag: true,
GenerateTypeMeta: false,
GenerateJSONTag: true,
AlwaysGenerateJSONTag: false,
SnakeTyleJSONTag: false,
LowerCamelCaseJSONTag: false,
GenerateReflectionInfo: false,
EnumAsINT32: false,
TrimIDL: false,
GenerateJSONStringMethod: false,
MarshalEnumToText: false,
GenerateSetter: false,
GenDatabaseTag: false,
GenOmitEmptyTag: true,
TypedefAsTypeAlias: true,
ValidateSet: true,
ValueTypeForSIC: false,
ScanValueForEnum: true,
ReorderFields: false,
TypedEnumString: false,
KeepUnknownFields: false,
GenDeepEqual: false,
CompatibleNames: false,
ReserveComments: false,
NilSafe: false,
FrugalTag: false,
EscapeDoubleInTag: true,
GenerateTypeMeta: false,
GenerateJSONTag: true,
AlwaysGenerateJSONTag: false,
SnakeTyleJSONTag: false,
LowerCamelCaseJSONTag: false,
GenerateReflectionInfo: false,
EnumAsINT32: false,
TrimIDL: false,
JSONStringer: false,
}

type param struct {
Expand Down
10 changes: 0 additions & 10 deletions generator/golang/templates/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ import (
{{- if Features.GenerateReflectionInfo}}thriftreflection "github.com/cloudwego/kitex/pkg/reflection/thrift"{{end}}
)
{{- if Features.GenerateJSONStringMethod }}
{{- UseStdLibrary "json"}}
// jsonMarshaler customize json.Marshal as you like
type jsonMarshaler func(v interface{}) ([]byte, error)
var jsonFunc jsonMarshaler = json.Marshal
func ResetJSONMarshalFunc(jn jsonMarshaler) {
jsonFunc = jn
}
{{- end}}
{{template "Constant" .}}
{{- range .Enums}}
Expand Down
6 changes: 3 additions & 3 deletions generator/golang/templates/slim/slim.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func (p *{{$TypeName}}) CarryingUnknownFields() bool {
{{template "FieldIsSet" .}}
func (p *{{$TypeName}}) String() string {
{{- if Features.GenerateJSONStringMethod }}
{{- UseStdLibrary "json"}}
JsonBytes , _ := jsonFunc(p)
{{- if Features.JSONStringer}}
{{- UseStdLibrary "json_utils"}}
JsonBytes , _ := json_utils.JSONFunc(p)
return string(JsonBytes)
{{- else}}
if p == nil {
Expand Down
6 changes: 3 additions & 3 deletions generator/golang/templates/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ var fieldIDToName_{{$TypeName}} = map[int16]string{
{{template "StructLikeWriteField" .}}
func (p *{{$TypeName}}) String() string {
{{- if Features.GenerateJSONStringMethod }}
{{- UseStdLibrary "json"}}
JsonBytes , _ := jsonFunc(p)
{{- if Features.JSONStringer}}
{{- UseStdLibrary "json_utils"}}
JsonBytes , _ := json_utils.JSONFunc(p)
return string(JsonBytes)
{{- else}}
if p == nil {
Expand Down
1 change: 1 addition & 0 deletions generator/golang/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
ThriftReflectionLib = "github.com/cloudwego/thriftgo/thrift_reflection"
ThriftOptionLib = "github.com/cloudwego/thriftgo/option"
defaultTemplate = "default"
ThriftJSONUtilLib = "github.com/cloudwego/thriftgo/utils/json_utils"
)

var escape = regexp.MustCompile(`\\.`)
Expand Down
2 changes: 1 addition & 1 deletion test/golang/cases_and_options/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ features=(\
nil_safe \
frugal_tag \
unescape_double_quote \
generate_json_string_method \
json_stringer \
)

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

0 comments on commit b5a7eff

Please sign in to comment.