Skip to content

Commit

Permalink
Fix test for field_hooks_are_applied to prepend
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Coffman <[email protected]>
  • Loading branch information
StevenACoffman committed Jan 27, 2023
1 parent 06b8609 commit c83aa08
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
14 changes: 6 additions & 8 deletions plugin/modelgen/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,9 @@ func (m *Plugin) generateFields(cfg *config.Config, schemaType *ast.Definition)
return fields, nil
}

// GoTagFieldHook prepend the goTag directive to the generated Field f. When applying the Tag to the field, the field
// name is used when no value argument is present.
// GoTagFieldHook prepends the goTag directive to the generated Field f.
// When applying the Tag to the field, the field
// name is used if no value argument is present.
func GoTagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Field, error) {
args := make([]string, 0)
for _, goTag := range fd.Directives.ForNames("goTag") {
Expand All @@ -412,15 +413,12 @@ func GoTagFieldHook(td *ast.Definition, fd *ast.FieldDefinition, f *Field) (*Fie
}

if len(args) > 0 {
f.Tag = strings.Join(args, " ") + " " + f.Tag
f.Tag = removeDuplicateTags(strings.Join(args, " ") + " " + f.Tag)
}

f.Tag = removeDuplicateTags(f.Tag)

return f, nil
}

// removeDuplicateTags removes duplicate tags
func removeDuplicateTags(t string) string {
processed := make(map[string]bool)
tt := strings.Split(t, " ")
Expand All @@ -434,9 +432,9 @@ func removeDuplicateTags(t string) string {

processed[kv[0]] = true
if len(returnTags) > 0 {
returnTags = returnTags + " "
returnTags += " "
}
returnTags = returnTags + kv[0] + ":" + kv[1]
returnTags += kv[0] + ":" + kv[1]
}

return returnTags
Expand Down
10 changes: 5 additions & 5 deletions plugin/modelgen/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ func TestModelGeneration(t *testing.T) {
fileText := string(file)

expectedTags := []string{
`json:"name" anotherTag:"tag"`,
`json:"enum" yetAnotherTag:"12"`,
`json:"noVal" yaml:"noVal"`,
`json:"repeated" someTag:"value" repeated:"true"`,
`anotherTag:"tag" json:"name"`,
`yetAnotherTag:"12" json:"enum"`,
`yaml:"noVal" repeated:"true" json:"noVal"`,
`someTag:"value" repeated:"true" json:"repeated"`,
}

for _, tag := range expectedTags {
require.True(t, strings.Contains(fileText, tag))
require.True(t, strings.Contains(fileText, tag), tag)
}
})

Expand Down
8 changes: 4 additions & 4 deletions plugin/modelgen/out/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions plugin/modelgen/out_struct_pointers/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c83aa08

Please sign in to comment.