Skip to content

Commit d0b3c13

Browse files
author
Alex Boten
committed
feat: add support for --tags flag
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>
1 parent 1720613 commit d0b3c13

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

cmd/gojsonschema/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var (
2929
capitalizations []string
3030
resolveExtensions []string
3131
yamlExtensions []string
32+
tags []string
3233
structNameFromTitle bool
3334

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

167171
abortWithErr(rootCmd.Execute())
168172
}

pkg/generator/generate.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Config struct {
2424
DefaultOutputName string
2525
StructNameFromTitle bool
2626
Warner func(string)
27+
Tags []string
2728
}
2829

2930
type SchemaMapping struct {
@@ -804,13 +805,20 @@ func (g *schemaGenerator) generateStructType(
804805
SchemaType: prop,
805806
}
806807

808+
tags := ""
809+
807810
if isRequired {
808-
structField.Tags = fmt.Sprintf(`json:"%s" yaml:"%s" mapstructure:"%s"`, name, name, name)
811+
for _, tag := range g.config.Tags {
812+
tags += fmt.Sprintf(`%s:"%s" `, tag, name)
813+
}
809814
} else {
810-
structField.Tags = fmt.Sprintf(`json:"%s,omitempty" yaml:"%s,omitempty" mapstructure:"%s,omitempty"`,
811-
name, name, name)
815+
for _, tag := range g.config.Tags {
816+
tags += fmt.Sprintf(`%s:"%s,omitempty" `, tag, name)
817+
}
812818
}
813819

820+
structField.Tags = strings.TrimSpace(tags)
821+
814822
if structField.Comment == "" {
815823
structField.Comment = fmt.Sprintf("%s corresponds to the JSON schema field %q.",
816824
structField.Name, name)

tests/data/misc/tags.go.output

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.
2+
3+
package test
4+
5+
type Tags struct {
6+
// Html corresponds to the JSON schema field "html".
7+
Html *string `yaml:"html,omitempty"`
8+
9+
// Id corresponds to the JSON schema field "id".
10+
Id *string `yaml:"id,omitempty"`
11+
12+
// Url corresponds to the JSON schema field "url".
13+
Url *string `yaml:"url,omitempty"`
14+
}

tests/data/misc/tags.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"id": "https://example.com/case",
4+
"type": "object",
5+
"properties": {
6+
"url": {
7+
"type": "string"
8+
},
9+
"id": {
10+
"type": "string"
11+
},
12+
"html": {
13+
"type": "string"
14+
}
15+
}
16+
}

tests/integration_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
Warner: func(message string) {
2828
log.Printf("[from warner] %s", message)
2929
},
30+
Tags: []string{"json", "yaml", "mapstructure"},
3031
}
3132
)
3233

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

144+
func TestTags(t *testing.T) {
145+
t.Parallel()
146+
147+
cfg := basicConfig
148+
cfg.Tags = []string{"yaml"}
149+
testExampleFile(t, cfg, "./data/misc/tags.json")
150+
}
151+
143152
func testExamples(t *testing.T, cfg generator.Config, dataDir string) {
144153
t.Helper()
145154

0 commit comments

Comments
 (0)