Skip to content

Commit

Permalink
custom json tag
Browse files Browse the repository at this point in the history
  • Loading branch information
liov committed Sep 14, 2020
1 parent c2bd0c6 commit dbfc007
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions patch/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go/types"
"log"
"path/filepath"
"reflect"
"regexp"
"strings"

Expand Down Expand Up @@ -531,6 +532,22 @@ func (p *Patcher) patchIdent(id *ast.Ident, obj types.Object) {
if v.Tag == nil {
v.Tag = &ast.BasicLit{}
}

if jsonTag, exist := reflect.StructTag(tags).Lookup("json"); exist {
newTags := strings.Split(strings.TrimSpace(tags), " ")
for i, tag := range newTags {
if strings.HasPrefix(tag, `json:`) {
newTags = append(newTags[:i], newTags[i+1:]...)
break
}
}
tags = strings.Join(newTags, " ")

odlTags := strings.Split(strings.TrimSpace(strings.Trim(v.Tag.Value, "` ")), " ")
tag := strings.Join(append(odlTags[:len(odlTags)-1], `json:"`+jsonTag+`"`), " ")
v.Tag.Value = "`" + tag + "`"
}

v.Tag.Value = "`" + strings.TrimSpace(strings.Trim(v.Tag.Value, "` ")+" "+tags) + "`"
log.Printf("Add tags:\t%q.%s %s", obj.Pkg().Path(), id.Name, v.Tag.Value)
}
Expand Down

0 comments on commit dbfc007

Please sign in to comment.