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 (
ErrMulti struct {
A ErrMultiA `json:"a,omitempty,union"`
B ErrMultiB `json:"b,omitempty,union"`
A ErrMultiA `json:"a,omitempty" gotsrpc:"union"`
B ErrMultiB `json:"b,omitempty" gotsrpc:"union"`
}
ErrMultiA string
ErrMultiB string

View File

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

View File

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