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 4 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": "encoding/json",
}
for pkg, path := range std {
ns.Add(pkg, path)
Expand Down
53 changes: 28 additions & 25 deletions generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,37 @@ type Features struct {
CodeRefSlim bool `code_ref_slim:"Genenerate code ref by given idl-ref.yaml with less refs to avoid conflict"`
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()."`
HeyJavaBean marked this conversation as resolved.
Show resolved Hide resolved
}

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,
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,
}

type param struct {
Expand Down
10 changes: 10 additions & 0 deletions generator/golang/templates/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ 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) {
HeyJavaBean marked this conversation as resolved.
Show resolved Hide resolved
jsonFunc = jn
}
{{- end}}

{{template "Constant" .}}

{{- range .Enums}}
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.GenerateJSONStringMethod }}
{{- UseStdLibrary "json"}}
JsonBytes , _ := 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 @@ -94,11 +94,18 @@ var fieldIDToName_{{$TypeName}} = map[int16]string{
{{template "StructLikeWriteField" .}}

func (p *{{$TypeName}}) String() string {
{{- if Features.GenerateJSONStringMethod }}
{{- UseStdLibrary "json"}}
JsonBytes , _ := 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 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 \
generate_json_string_method \
)

run_cases() {
Expand Down