Skip to content

Commit

Permalink
Merge pull request #19 from twpayne/string-annotation
Browse files Browse the repository at this point in the history
feat: add support for ,string annotations
  • Loading branch information
twpayne committed Mar 6, 2024
2 parents 2b88864 + bd71258 commit d670417
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 78 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ To learn about more about the available options, run:
input, making it suitable for incorporation into build pipelines or detecting
schema changes.
* Generates `omitempty` when possible.
* Generates `,string` tags.
* Uses the standard library's `time.Time` when possible.
* Gracefully handles properties with spaces that [cannot be unmarshalled by
`encoding/json`](https://github.com/golang/go/issues/18531).
Expand Down
2 changes: 2 additions & 0 deletions cmd/gojsonstruct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
packageComment = pflag.String("package-comment", "", "package comment")
packageName = pflag.String("package-name", "main", "package name")
skipUnparsableProperties = pflag.Bool("skip-unparsable-properties", true, "skip unparsable properties")
stringTags = pflag.Bool("string-tags", false, "generate ,string tags")
structTagName = pflag.String("struct-tag-name", "", "struct tag name")
typeComment = pflag.String("type-comment", "", "type comment")
typeName = pflag.String("typename", "T", "type name")
Expand All @@ -41,6 +42,7 @@ func run() error {
options := []jsonstruct.GeneratorOption{
jsonstruct.WithOmitEmpty(omitEmptyOption[*omitempty]),
jsonstruct.WithSkipUnparsableProperties(*skipUnparsableProperties),
jsonstruct.WithStringTags(*stringTags),
jsonstruct.WithUseJSONNumber(*useJSONNumber),
jsonstruct.WithGoFormat(*goFormat),
}
Expand Down
13 changes: 11 additions & 2 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Generator struct {
packageComment string
packageName string
skipUnparsableProperties bool
stringTags bool
structTagNames []string
typeComment string
typeName string
Expand Down Expand Up @@ -127,6 +128,13 @@ func WithSkipUnparsableProperties(skipUnparsableProperties bool) GeneratorOption
}
}

// WithStringTags sets whether ",string" tags should be used.
func WithStringTags(stringTags bool) GeneratorOption {
return func(g *Generator) {
g.stringTags = stringTags
}
}

// WithStructTagName sets the struct tag name.
func WithStructTagName(structTagName string) GeneratorOption {
return func(g *Generator) {
Expand Down Expand Up @@ -216,12 +224,13 @@ func (g *Generator) Generate() ([]byte, error) {
}
fmt.Fprintf(buffer, "package %s\n", g.packageName)
imports := maps.Clone(g.imports)
goType, _ := g.value.goType(0, &generateOptions{
goType := g.value.goType(0, &generateOptions{
exportNameFunc: g.exportNameFunc,
imports: imports,
intType: g.intType,
omitEmptyOption: g.omitEmptyOption,
skipUnparsableProperties: g.skipUnparsableProperties,
stringTags: g.stringTags,
structTagNames: g.structTagNames,
useJSONNumber: g.useJSONNumber,
})
Expand All @@ -236,7 +245,7 @@ func (g *Generator) Generate() ([]byte, error) {
if g.typeComment != "" {
fmt.Fprintf(buffer, "// %s\n", g.typeComment)
}
fmt.Fprintf(buffer, "type %s %s\n", g.typeName, goType)
fmt.Fprintf(buffer, "type %s %s\n", g.typeName, goType.typeStr)
if !g.goFormat {
return buffer.Bytes(), nil
}
Expand Down
Loading

0 comments on commit d670417

Please sign in to comment.