refactor: move custom tags to gotsrpc

This commit is contained in:
Kevin Franklin Kim 2022-03-04 08:22:29 +01:00
parent 3c097ca255
commit 2c7ea1849b
3 changed files with 38 additions and 27 deletions

View File

@ -4,8 +4,8 @@ type ErrSimple string
type ( type (
ErrMulti struct { ErrMulti struct {
A ErrMultiA `json:"a,omitempty,union"` A ErrMultiA `json:"a,omitempty" gotsrpc:"union"`
B ErrMultiB `json:"b,omitempty,union"` B ErrMultiB `json:"b,omitempty" gotsrpc:"union"`
} }
ErrMultiA string ErrMultiA string
ErrMultiB string ErrMultiB string

View File

@ -30,9 +30,9 @@ type (
type ( type (
UnionString struct { UnionString struct {
A *UnionStringA `json:"a,omitempty,union"` A *UnionStringA `json:"a,omitempty" gotsrpc:"union"`
B *UnionStringB `json:"b,omitempty,union"` B *UnionStringB `json:"b,omitempty" gotsrpc:"union"`
c string c string
} }
UnionStringA string UnionStringA string
@ -48,22 +48,22 @@ const (
type ( type (
UnionStructA struct { UnionStructA struct {
Kind string `json:"kind,type:'UnionStructA'"` Kind string `json:"kind" gotsrpc:"type:'UnionStructA'"`
Value UnionStructAValueA `json:"value"` Value UnionStructAValueA `json:"value"`
Bar string `json:"bar"` Bar string `json:"bar"`
} }
UnionStructAValueA string UnionStructAValueA string
UnionStructB struct { UnionStructB struct {
Kind string `json:"kind,type:'UnionStructB'"` Kind string `json:"kind" gotsrpc:"type:'UnionStructB'"`
Value UnionStructAValueB `json:"value"` Value UnionStructAValueB `json:"value"`
Foo string `json:"foo"` Foo string `json:"foo"`
} }
UnionStructAValueB string UnionStructAValueB string
UnionStruct struct { UnionStruct struct {
A *UnionStructA `json:"a,omitempty,union"` A *UnionStructA `json:"a,omitempty" gotsrpc:"union"`
B *UnionStructB `json:"b,omitempty,union"` B *UnionStructB `json:"b,omitempty" gotsrpc:"union"`
} }
) )

View File

@ -57,12 +57,20 @@ func traceData(args ...interface{}) {
} }
func extractJSONInfo(tag string) *JSONInfo { func extractJSONInfo(tag string) *JSONInfo {
t := reflect.StructTag(tag) structTag := reflect.StructTag(tag)
jsonTagString := t.Get("json")
if len(jsonTagString) == 0 { jsonTags := strings.Split(structTag.Get("json"), ",")
gotsrpcTags := strings.Split(structTag.Get("gotsrpc"), ",")
if len(jsonTags) == 0 && len(gotsrpcTags) == 0 {
return nil return nil
} }
jsonTagParts := strings.Split(jsonTagString, ",")
for k, value := range jsonTags {
jsonTags[k] = strings.TrimSpace(value)
}
for k, value := range gotsrpcTags {
gotsrpcTags[k] = strings.TrimSpace(value)
}
name := "" name := ""
tsType := "" tsType := ""
@ -70,35 +78,38 @@ func extractJSONInfo(tag string) *JSONInfo {
union := false union := false
inline := false inline := false
ignore := false ignore := false
var cleanParts []string
for _, jsonTagPart := range jsonTagParts { if len(jsonTags) > 0 {
cleanParts = append(cleanParts, strings.TrimSpace(jsonTagPart)) switch jsonTags[0] {
}
if len(cleanParts) > 0 {
switch cleanParts[0] {
case "": case "":
// do nothing // do nothing
case "-": case "-":
ignore = true ignore = true
default: default:
name = cleanParts[0] name = jsonTags[0]
} }
} }
if len(cleanParts) > 1 { if len(jsonTags) > 1 {
for _, cleanPart := range cleanParts[1:] { for _, value := range jsonTags[1:] {
switch { switch {
case cleanPart == "union": case value == "inline":
union = true
case cleanPart == "inline":
inline = true inline = true
case cleanPart == "omitempty": case value == "omitempty":
omit = true omit = true
case strings.HasPrefix(cleanPart, "type:"):
tsType = strings.TrimPrefix(cleanPart, "type:")
} }
} }
} }
for _, value := range gotsrpcTags {
switch {
case value == "union":
union = true
case strings.HasPrefix(value, "type:"):
tsType = strings.TrimPrefix(value, "type:")
}
}
// TODO split up gotsrpc info
return &JSONInfo{ return &JSONInfo{
Name: name, Name: name,
Type: tsType, Type: tsType,