From b2089c6d541a2d6250f26de7793b061e517b5a9b Mon Sep 17 00:00:00 2001 From: liov Date: Thu, 17 Sep 2020 10:07:00 +0800 Subject: [PATCH] patch,tests: logic extract and unit tests --- patch/patcher.go | 15 +-------------- patch/plugin.go | 17 ++++++++++++++++ patch/plugin_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 patch/plugin_test.go diff --git a/patch/patcher.go b/patch/patcher.go index 3d1e3f4..7311f46 100644 --- a/patch/patcher.go +++ b/patch/patcher.go @@ -10,7 +10,6 @@ import ( "go/types" "log" "path/filepath" - "reflect" "regexp" "strings" @@ -533,19 +532,7 @@ func (p *Patcher) patchIdent(id *ast.Ident, obj types.Object) { v.Tag = &ast.BasicLit{} } - //tag override - oldTags := strings.Split(strings.TrimSpace(strings.Trim(v.Tag.Value, "` ")), " ") - var newTags []string - for _, tag := range oldTags { - var key string - if kv := strings.Split(tag, ":"); len(kv) > 0 { - key = kv[0] - } - if value, exist := reflect.StructTag(tags).Lookup(key); !exist || value == "" { - newTags = append(newTags, tag) - } - } - v.Tag.Value = "`" + strings.Join(append(newTags, tags), " ") + "`" + v.Tag.Value = "`" + mergeTags(v.Tag.Value, tags) + "`" log.Printf("Add tags:\t%q.%s %s", obj.Pkg().Path(), id.Name, v.Tag.Value) } } diff --git a/patch/plugin.go b/patch/plugin.go index b38c039..867eeca 100644 --- a/patch/plugin.go +++ b/patch/plugin.go @@ -5,6 +5,7 @@ import ( "io" "os" "os/exec" + "reflect" "strings" "google.golang.org/protobuf/proto" @@ -77,3 +78,19 @@ func Write(res *pluginpb.CodeGeneratorResponse, w io.Writer) error { _, err = w.Write(out) return err } + +// Override tags +func mergeTags(oldTag, newTag string) string { + oldTags := strings.Split(strings.TrimSpace(strings.Trim(oldTag, "` ")), " ") + var newTags []string + for _, tag := range oldTags { + var key string + if kv := strings.Split(tag, ":"); len(kv) > 0 { + key = kv[0] + } + if value, exist := reflect.StructTag(newTag).Lookup(key); !exist || value == "" { + newTags = append(newTags, tag) + } + } + return strings.Join(append(newTags, newTag), " ") +} diff --git a/patch/plugin_test.go b/patch/plugin_test.go new file mode 100644 index 0000000..e34e814 --- /dev/null +++ b/patch/plugin_test.go @@ -0,0 +1,46 @@ +package patch + +import ( + "testing" +) + +func TestMergeTags(t *testing.T) { + tests := []struct { + name string + in [2]string + want string + }{ + {in: [2]string{ + `json:"code;omitempty"`, + `json:"-" test:"test"`, + }, + want: `json:"-" test:"test"`, + }, + {in: [2]string{ + `json:"code;omitempty"`, + `test:"test"`, + }, + want: `json:"code;omitempty" test:"test"`, + }, + {in: [2]string{ + `json:"code;omitempty"`, + `test1:"test1" test2:"test2"`, + }, + want: `json:"code;omitempty" test1:"test1" test2:"test2"`, + }, + {in: [2]string{ + `json:"code;omitempty" test1:"test""`, + `test1:"test1" test2:"test2"`, + }, + want: `json:"code;omitempty" test1:"test1" test2:"test2"`, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := mergeTags(test.in[0], test.in[1]); got != test.want { + t.Fatalf(" got: %s\nwant: %s\n", got, test.want) + } + }) + } +}