Skip to content

Commit

Permalink
existing tags shouldn't be overwritten. Fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketawati committed May 17, 2019
1 parent 6477da0 commit ef6c0dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
28 changes: 15 additions & 13 deletions easytags.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func main() {
return
}
for _, f := range files {
GenerateTags(f, tagNames)
GenerateTags(f, tagNames,*remove)
}
}
}

// GenerateTags generates snake case json tags so that you won't need to write them. Can be also extended to xml or sql tags
func GenerateTags(fileName string, tagNames []string) {
func GenerateTags(fileName string, tagNames []string, remove bool) {
fset := token.NewFileSet() // positions are relative to fset
// Parse the file given in arguments
f, err := parser.ParseFile(fset, fileName, nil, parser.ParseComments)
Expand All @@ -76,7 +76,7 @@ func GenerateTags(fileName string, tagNames []string) {
ast.Inspect(f, func(n ast.Node) bool {
switch t := n.(type) {
case *ast.StructType:
processTags(t, tagNames)
processTags(t, tagNames, remove)
return false
}
return true
Expand All @@ -101,30 +101,27 @@ func GenerateTags(fileName string, tagNames []string) {
func parseTags(field *ast.Field, tags []string) string {
var tagValues []string
fieldName := field.Names[0].String()

for _, tag := range tags {
var value string
existingTagReg := regexp.MustCompile(fmt.Sprintf("%s:\"[^\"]+\"", tag))
existingTag := existingTagReg.FindString(field.Tag.Value)
if existingTag != "" {
value = existingTag
} else {
if existingTag == "" {
value = fmt.Sprintf("%s:\"%s\"", tag, ToSnake(fieldName))
tagValues = append(tagValues, value)
}

tagValues = append(tagValues, value)
}
updatedTags := strings.Fields(strings.Trim(field.Tag.Value,"`"))

if len(tagValues) == 0 {
return ""
if len(tagValues) > 0 {
updatedTags = append(updatedTags,tagValues...)
}

newValue := "`" + strings.Join(tagValues, " ") + "`"
newValue := "`" + strings.Join(updatedTags," ") + "`"

return newValue
}

func processTags(x *ast.StructType, tagNames []string) {
func processTags(x *ast.StructType, tagNames []string, remove bool) {
for _, field := range x.Fields.List {
if len(field.Names) == 0 {
continue
Expand All @@ -134,6 +131,11 @@ func processTags(x *ast.StructType, tagNames []string) {
continue
}

if remove {
field.Tag = nil
continue
}

if field.Tag == nil {
field.Tag = &ast.BasicLit{}
field.Tag.ValuePos = field.Type.Pos() + 1
Expand Down
20 changes: 17 additions & 3 deletions easytags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ func TestGenerateTags(t *testing.T) {
t.Errorf("Error reading file %v", err)
}
defer ioutil.WriteFile("testfile.go", testCode, 0644)
GenerateTags("testfile.go", []string{"json"})
GenerateTags("testfile.go", []string{"json"}, false)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "testfile.go", nil, parser.ParseComments)
if err != nil {
t.Errorf("Error parsing generated file %v", err)
genFile, _ := ioutil.ReadFile("testfile.go")
t.Errorf("\n%s",genFile)
return
}

Expand Down Expand Up @@ -56,6 +58,12 @@ func TestGenerateTags(t *testing.T) {
} else if field.Tag.Value != "`json:\"test_field2\"`" {
t.Error("Snake case tag should be generated for TestField2")
}
} else if name == "ExistingTag" {
if field.Tag == nil {
t.Error("Tag should be generated for TestFiled2")
} else if field.Tag.Value != "`custom:\"\" json:\"etag\"`" {
t.Error("existing tag should not be modified, instead found ", field.Tag.Value)
}
}

}
Expand All @@ -68,7 +76,7 @@ func TestGenerateTags_Multiple(t *testing.T) {
t.Errorf("Error reading file %v", err)
}
defer ioutil.WriteFile("testfile.go", testCode, 0644)
GenerateTags("testfile.go", []string{"json", "xml"})
GenerateTags("testfile.go", []string{"json", "xml"}, false)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "testfile.go", nil, parser.ParseComments)
if err != nil {
Expand Down Expand Up @@ -110,6 +118,12 @@ func TestGenerateTags_Multiple(t *testing.T) {
} else if field.Tag.Value != "`json:\"test_field2\" xml:\"test_field2\"`" {
t.Error("Snake case tag should be generated for TestField2")
}
} else if name == "ExistingTag" {
if field.Tag == nil {
t.Error("Tag should be generated for TestFiled2")
} else if field.Tag.Value != "`custom:\"\" json:\"etag\" xml:\"existing_tag\"`" {
t.Error("new tag should be appended to existing tag, instead found ", field.Tag.Value)
}
}

}
Expand All @@ -122,7 +136,7 @@ func TestGenerateTags_RemoveAll(t *testing.T) {
t.Errorf("Error reading file %v", err)
}
defer ioutil.WriteFile("testfile.go", testCode, 0644)
GenerateTags("testfile.go", []string{})
GenerateTags("testfile.go", []string{}, true)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "testfile.go", nil, parser.ParseComments)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions testfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
type TestStruct struct {
Field1 int `json:"-"`
TestField2 string
ExistingTag string `custom:"" json:"etag"`
Embed
}

Expand Down

0 comments on commit ef6c0dd

Please sign in to comment.