Skip to content

Commit

Permalink
Merge pull request #271 from pace/enable-nullable-and-omitempty-strin…
Browse files Browse the repository at this point in the history
…gs-only

Enable nullable and omitempty strings only
  • Loading branch information
Ferlonas authored Jul 12, 2021
2 parents 96cd30f + 540d3a7 commit 13c1804
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 68 deletions.
48 changes: 41 additions & 7 deletions http/jsonapi/generator/generate_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ func (g *typeGenerator) invoke() error { // nolint: gocyclo
}
case "uuid":
addValidator(g.tags, "uuid")
g.stmt.String()
if g.schema.Nullable {
g.stmt.Op("*").String()
} else {
g.stmt.String()
}
case "decimal":
addValidator(g.tags, "matches(^(\\d*\\.)?\\d+$)")
if g.isParam {
Expand All @@ -76,33 +80,63 @@ func (g *typeGenerator) invoke() error { // nolint: gocyclo
g.stmt.Op("*").Qual(pkgDecimal, "Decimal")
}
default:
g.stmt.String()
if g.schema.Nullable {
g.stmt.Op("*").String()
} else {
g.stmt.String()
}
}
case "integer":
removeOmitempty(g.tags)
switch g.schema.Format {
case "int32":
g.stmt.Int32()
if g.schema.Nullable {
g.stmt.Op("*").Int32()
} else {
g.stmt.Int32()
}
default:
g.stmt.Int64()
if g.schema.Nullable {
g.stmt.Op("*").Int64()
} else {
g.stmt.Int64()
}
}
case "number":
switch g.schema.Format {
case "decimal":
if g.isParam {
removeOmitempty(g.tags)
g.stmt.Qual(pkgDecimal, "Decimal")
} else {
g.stmt.Op("*").Qual(pkgDecimal, "Decimal")
}
case "float":
g.stmt.Float32()
removeOmitempty(g.tags)
if g.schema.Nullable {
g.stmt.Op("*").Float32()
} else {
g.stmt.Float32()
}
case "double":
fallthrough
default:
g.stmt.Float64()
removeOmitempty(g.tags)
if g.schema.Nullable {
g.stmt.Op("*").Float64()
} else {
g.stmt.Float64()
}
}
case "boolean":
g.stmt.Bool()
removeOmitempty(g.tags)
if g.schema.Nullable {
g.stmt.Op("*").Bool()
} else {
g.stmt.Bool()
}
case "array": // nolint: goconst
removeOmitempty(g.tags)
err := g.g.goType(g.stmt.Index(), g.schema.Items.Value, g.tags).invoke()
if err != nil {
return err
Expand Down
10 changes: 10 additions & 0 deletions http/jsonapi/generator/generate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package generator
import (
"fmt"
"sort"
"strings"

"github.com/dave/jennifer/jen"
"github.com/getkin/kin-openapi/openapi3"
Expand Down Expand Up @@ -439,3 +440,12 @@ func addJSONAPITags(tags map[string]string, kind, name string) {
tags["jsonapi"] = fmt.Sprintf("%s,%s,omitempty", kind, name)
tags["json"] = fmt.Sprintf("%s,omitempty", name)
}

func removeOmitempty(tags map[string]string) {
if v, ok := tags["jsonapi"]; ok {
tags["jsonapi"] = strings.ReplaceAll(v, ",omitempty", "")
}
if v, ok := tags["json"]; ok {
tags["json"] = strings.ReplaceAll(v, ",omitempty", "")
}
}
6 changes: 4 additions & 2 deletions http/jsonapi/generator/internal/fueling/open-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1086,11 +1086,13 @@
"longitude": {
"type": "number",
"format": "float",
"example": 8.425
"example": 8.425,
"nullable": true
},
"stationName": {
"type": "string",
"example": "PACE Station"
"example": "PACE Station",
"nullable": true
},
"address": {
"type": "object",
Expand Down
60 changes: 30 additions & 30 deletions http/jsonapi/generator/internal/fueling/open-api_test.go

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

8 changes: 4 additions & 4 deletions http/jsonapi/generator/internal/pay/open-api_test.go

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

Loading

0 comments on commit 13c1804

Please sign in to comment.