Skip to content

Commit

Permalink
feat: add support for --tags flag
Browse files Browse the repository at this point in the history
This feature allows users to configure which struct tags to generate. By
default it generates json, yaml, mapstructure.

Fix omissis#79

Signed-off-by: Alex Boten <aboten@lightstep.com>
  • Loading branch information
Alex Boten committed May 25, 2023
1 parent 1720613 commit d0b3c13
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/gojsonschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
capitalizations []string
resolveExtensions []string
yamlExtensions []string
tags []string
structNameFromTitle bool

errFlagFormat = errors.New("flag must be in the format URI=PACKAGE")
Expand Down Expand Up @@ -73,6 +74,7 @@ var (
ResolveExtensions: resolveExtensions,
YAMLExtensions: yamlExtensions,
StructNameFromTitle: structNameFromTitle,
Tags: tags,
}
for _, id := range allKeys(schemaPackageMap, schemaOutputMap, schemaRootTypeMap) {
mapping := generator.SchemaMapping{SchemaID: id}
Expand Down Expand Up @@ -163,6 +165,8 @@ also look for foo.json if --resolve-extension json is provided.`)
`Add a file extension that should be recognized as YAML. Default are .yml, .yaml.`)
rootCmd.PersistentFlags().BoolVarP(&structNameFromTitle, "struct-name-from-title", "t", false,
"Use the schema title as the generated struct name")
rootCmd.PersistentFlags().StringSliceVar(&tags, "tags", []string{"json", "yaml", "mapstructure"},
`Specify which struct tags to generate. Defaults are json, yaml, mapstructure`)

abortWithErr(rootCmd.Execute())
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
DefaultOutputName string
StructNameFromTitle bool
Warner func(string)
Tags []string
}

type SchemaMapping struct {
Expand Down Expand Up @@ -804,13 +805,20 @@ func (g *schemaGenerator) generateStructType(
SchemaType: prop,
}

tags := ""

if isRequired {
structField.Tags = fmt.Sprintf(`json:"%s" yaml:"%s" mapstructure:"%s"`, name, name, name)
for _, tag := range g.config.Tags {
tags += fmt.Sprintf(`%s:"%s" `, tag, name)
}
} else {
structField.Tags = fmt.Sprintf(`json:"%s,omitempty" yaml:"%s,omitempty" mapstructure:"%s,omitempty"`,
name, name, name)
for _, tag := range g.config.Tags {
tags += fmt.Sprintf(`%s:"%s,omitempty" `, tag, name)
}
}

structField.Tags = strings.TrimSpace(tags)

if structField.Comment == "" {
structField.Comment = fmt.Sprintf("%s corresponds to the JSON schema field %q.",
structField.Name, name)
Expand Down
14 changes: 14 additions & 0 deletions tests/data/misc/tags.go.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.

package test

type Tags struct {
// Html corresponds to the JSON schema field "html".
Html *string `yaml:"html,omitempty"`

// Id corresponds to the JSON schema field "id".
Id *string `yaml:"id,omitempty"`

// Url corresponds to the JSON schema field "url".
Url *string `yaml:"url,omitempty"`
}
16 changes: 16 additions & 0 deletions tests/data/misc/tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/case",
"type": "object",
"properties": {
"url": {
"type": "string"
},
"id": {
"type": "string"
},
"html": {
"type": "string"
}
}
}
9 changes: 9 additions & 0 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
Warner: func(message string) {
log.Printf("[from warner] %s", message)
},
Tags: []string{"json", "yaml", "mapstructure"},
}
)

Expand Down Expand Up @@ -140,6 +141,14 @@ func TestExtraImportsAnotherYAML(t *testing.T) {
testExampleFile(t, cfg, "./data/extraImports/gopkgYAMLv2.json")
}

func TestTags(t *testing.T) {
t.Parallel()

cfg := basicConfig
cfg.Tags = []string{"yaml"}
testExampleFile(t, cfg, "./data/misc/tags.json")
}

func testExamples(t *testing.T, cfg generator.Config, dataDir string) {
t.Helper()

Expand Down

0 comments on commit d0b3c13

Please sign in to comment.