Skip to content

Commit

Permalink
patch,tests: logic extract and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liov committed Sep 17, 2020
1 parent 8337b3a commit b2089c6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
15 changes: 1 addition & 14 deletions patch/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"go/types"
"log"
"path/filepath"
"reflect"
"regexp"
"strings"

Expand Down Expand Up @@ -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)
}
}
Expand Down
17 changes: 17 additions & 0 deletions patch/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"os/exec"
"reflect"
"strings"

"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -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), " ")
}
46 changes: 46 additions & 0 deletions patch/plugin_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit b2089c6

Please sign in to comment.