From 2c7ea1849b36c0f74dad41921aa6998531c0f8e7 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Fri, 4 Mar 2022 08:22:29 +0100 Subject: [PATCH] refactor: move custom tags to gotsrpc --- example/errors/service/frontend/vo.go | 4 +-- example/union/service/vo.go | 12 +++---- typereader.go | 49 ++++++++++++++++----------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/example/errors/service/frontend/vo.go b/example/errors/service/frontend/vo.go index 9ac53c6..e6f91b3 100644 --- a/example/errors/service/frontend/vo.go +++ b/example/errors/service/frontend/vo.go @@ -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 diff --git a/example/union/service/vo.go b/example/union/service/vo.go index 7942396..026f55c 100644 --- a/example/union/service/vo.go +++ b/example/union/service/vo.go @@ -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"` } ) diff --git a/typereader.go b/typereader.go index 9a30b1a..cfc4e91 100644 --- a/typereader.go +++ b/typereader.go @@ -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,