Skip to content

Commit

Permalink
Refactor main.go to handle new tag and field names
Browse files Browse the repository at this point in the history
The changes include the renaming of variable 'newName' to 'jsonName' improving overall clarity, and a significant refactoring of the field tag handling logic. New logic is introduced to handle 'protobuf' tags and improve handling of 'json' tags, while also preventing duplicate tags. This refactor enhances code maintainability and readability.
  • Loading branch information
fynntang committed May 22, 2024
1 parent 585a168 commit 15f2aed
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions protoc-go-inject-tags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func init() {
flag.StringVar(&ignore, "ignore", "", "ignore fields for struct eg: form:file,json:file")
fset = token.NewFileSet()
}

func main() {
flag.Parse()

Expand Down Expand Up @@ -131,39 +132,51 @@ func handleTags(fields *ast.FieldList) {
continue
}

var newName string
var jsonName string
for _, name := range field.Names {
newName = strings.ToLower(strings.Join(camelcase.Split(name.Name), "_"))
jsonName = strings.ToLower(strings.Join(camelcase.Split(name.Name), "_"))
}

tv := strings.ReplaceAll(field.Tag.Value, "`", "")
tagList := strings.Split(tv, " ")
fieldTags := make(map[string]*fieldTag, 0)
fieldTags := make(map[string]*fieldTag)
var sortTags []string

for _, t := range tagList {
tf := strings.Split(t, ":")
if len(tf) != 2 {
continue
}
fieldTags[tf[0]] = &fieldTag{
tagName: strings.TrimSpace(tf[0]),
tagValue: strings.Replace(tf[1], "\"", "", -1),
tagName := strings.TrimSpace(tf[0])
tagValue := strings.Replace(tf[1], "\"", "", -1)
fieldTags[tagName] = &fieldTag{
tagName: tagName,
tagValue: tagValue,
}
sortTags = append(sortTags, tagName)

if tagName == "protobuf" {
if jsonIndex := strings.Index(tagValue, "json="); jsonIndex != -1 {
jsonValue := tagValue[jsonIndex+5:]
jsonValue = strings.Split(jsonValue, ",")[0]
jsonName = jsonValue
}
}
sortTags = append(sortTags, tf[0])
}

for _, tag := range tagsValue {
if _, ok := fieldTags[tag]; ok {
continue
}

fieldTags[tag] = &fieldTag{tagName: tag, tagValue: fmt.Sprintf("%s,omitempty", newName)}
fieldTags[tag] = &fieldTag{tagName: tag, tagValue: fmt.Sprintf("%s,omitempty", jsonName)}
sortTags = append(sortTags, tag)
}

fieldTags["json"] = &fieldTag{tagName: "json", tagValue: fmt.Sprintf("%s,omitempty", jsonName)}

var newTags string
seenTags := make(map[string]bool)
for _, tag := range sortTags {
newTags += fmt.Sprintf("%s:\"%s\" ", fieldTags[tag].tagName, fieldTags[tag].tagValue)
if !seenTags[tag] {
newTags += fmt.Sprintf("%s:\"%s\" ", fieldTags[tag].tagName, fieldTags[tag].tagValue)
seenTags[tag] = true
}
}

for _, v := range ignoreFields {
Expand All @@ -172,6 +185,6 @@ func handleTags(fields *ast.FieldList) {
}
}

field.Tag.Value = fmt.Sprintf("`%s`", newTags)
field.Tag.Value = fmt.Sprintf("`%s`", strings.TrimSpace(newTags))
}
}

0 comments on commit 15f2aed

Please sign in to comment.