Skip to content

Commit

Permalink
refactor: move custom tags to gotsrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Mar 4, 2022
1 parent 3c097ca commit 2c7ea18
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
4 changes: 2 additions & 2 deletions example/errors/service/frontend/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions example/union/service/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
}
)

Expand Down
49 changes: 30 additions & 19 deletions typereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,48 +57,59 @@ 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 := ""
omit := false
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,
Expand Down

0 comments on commit 2c7ea18

Please sign in to comment.