Skip to content

Commit

Permalink
feat: Support ignore fields
Browse files Browse the repository at this point in the history
  • Loading branch information
fynntang committed Jul 14, 2023
1 parent 6f66862 commit 43bd370
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions protoc-go-inject-tags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ import (
var (
tags string
folder string
ignore string
relativeFolder string
pbFiles []string
fset *token.FileSet
tagsValue []string
ignoreFields []ignoreField
)

type ignoreField struct {
tag string
field string
}

func init() {
flag.StringVar(&tags, "tags", "", "tags for struct")
flag.StringVar(&folder, "folder", "", "paths for struct")
flag.StringVar(&tags, "tags", "", "tags for struct eg: json,xml")
flag.StringVar(&folder, "folder", "", "paths for struct eg: ./pb")
flag.StringVar(&ignore, "ignore", "", "ignore fields for struct eg: form:file,json:file")
fset = token.NewFileSet()
}
func main() {
Expand Down Expand Up @@ -57,6 +65,16 @@ func checkArgs() {
}

tagsValue = strings.Split(tags, ",")

if ignore != "" {
for _, s := range strings.Split(ignore, ",") {
ignore := strings.Split(s, ":")
if len(ignore) != 2 {
log.Fatalf("ignore field %s format error", s)
}
ignoreFields = append(ignoreFields, ignoreField{tag: ignore[0], field: ignore[1]})
}
}
}

func walkFolder(folderPath string) {
Expand Down Expand Up @@ -140,6 +158,17 @@ func handleTags(fields *ast.FieldList) {
if _, ok := fieldTags[tag]; ok {
continue
}

if len(ignoreFields) > 0 {
for _, igno := range ignoreFields {
if igno.tag == tag && igno.field == newName {
fieldTags[tag] = &fieldTag{tagName: tag, tagValue: "-"}
sortTags = append(sortTags, tag)
continue
}
}
}

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

0 comments on commit 43bd370

Please sign in to comment.