Skip to content

Commit

Permalink
feat(codegen): generate json struct tags with omitempty (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
unicod3 authored and lerenn committed Jul 21, 2024
1 parent 209e450 commit 157fa74
Show file tree
Hide file tree
Showing 58 changed files with 1,455 additions and 125 deletions.
4 changes: 2 additions & 2 deletions examples/ping/v2/kafka/app/app.gen.go

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

4 changes: 2 additions & 2 deletions examples/ping/v2/kafka/user/user.gen.go

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

4 changes: 2 additions & 2 deletions examples/ping/v2/nats-jetstream/app/app.gen.go

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

4 changes: 2 additions & 2 deletions examples/ping/v2/nats-jetstream/user/user.gen.go

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

4 changes: 2 additions & 2 deletions examples/ping/v2/nats/app/app.gen.go

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

4 changes: 2 additions & 2 deletions examples/ping/v2/nats/user/user.gen.go

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

8 changes: 4 additions & 4 deletions examples/ping/v3/kafka/app/app.gen.go

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

8 changes: 4 additions & 4 deletions examples/ping/v3/kafka/user/user.gen.go

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

8 changes: 4 additions & 4 deletions examples/ping/v3/nats-jetstream/app/app.gen.go

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

8 changes: 4 additions & 4 deletions examples/ping/v3/nats-jetstream/user/user.gen.go

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

8 changes: 4 additions & 4 deletions examples/ping/v3/nats/app/app.gen.go

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

8 changes: 4 additions & 4 deletions examples/ping/v3/nats/user/user.gen.go

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

14 changes: 14 additions & 0 deletions pkg/codegen/generators/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/lerenn/asyncapi-codegen/pkg/asyncapi"
"github.com/lerenn/asyncapi-codegen/pkg/utils/template"
)

func appendDirectiveIfDefined(directives []string, tagName string, value float64) []string {
Expand All @@ -14,6 +15,19 @@ func appendDirectiveIfDefined(directives []string, tagName string, value float64
return directives
}

// GenerateJSONTags returns the "json" tag for a given field in a struct, based on the asyncapi contract.
func GenerateJSONTags[T any](schema asyncapi.Validations[T], field string) string {
directives := []string{
template.ConvertKey(field),
}

if !schema.IsRequired {
directives = append(directives, "omitempty")
}

return fmt.Sprintf("json:\"%s\"", strings.Join(directives, ","))
}

// GenerateValidateTags returns the "validate" tag for a given field in a struct, based on the asyncapi contract.
// This tag can then be used by go-playground/validator/v10 to validate the struct's content.
func GenerateValidateTags[T any](schema asyncapi.Validations[T]) string {
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/generators/v2/templates/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,6 @@ func HelpersFunctions() template.FuncMap {
"operationName": OperationName,
"referenceToTypeName": ReferenceToTypeName,
"generateValidateTags": generators.GenerateValidateTags[asyncapi.Schema],
"generateJSONTags": generators.GenerateJSONTags[asyncapi.Schema],
}
}
2 changes: 1 addition & 1 deletion pkg/codegen/generators/v2/templates/schema_definition.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type {{ namify .Name }} struct {
{{else if and $value.ReferenceTo $value.ReferenceTo.Description}}
// Description: {{multiLineComment $value.ReferenceTo.Description}}
{{end -}}
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `json:"{{convertKey $key}}"{{generateValidateTags $value.Validations}}`
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `{{generateJSONTags $value.Validations $key}}{{generateValidateTags $value.Validations}}`
{{end -}}

{{- if .AdditionalProperties}}
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/generators/v3/templates/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,6 @@ func HelpersFunctions() template.FuncMap {
"generateChannelAddrFromOp": GenerateChannelAddrFromOp,
"referenceToStructAttributePath": ReferenceToStructAttributePath,
"generateValidateTags": generators.GenerateValidateTags[asyncapi.Schema],
"generateJSONTags": generators.GenerateJSONTags[asyncapi.Schema],
}
}
2 changes: 1 addition & 1 deletion pkg/codegen/generators/v3/templates/schema_definition.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type {{ namify .Name }} struct {
{{else if and $value.ReferenceTo $value.ReferenceTo.Description}}
// Description: {{multiLineComment $value.ReferenceTo.Description}}
{{end -}}
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `json:"{{convertKey $key}}"{{generateValidateTags $value.Validations}}`
{{namify $key}} {{if and (not (isRequired $ $key)) (ne $value.Type "array")}}*{{end}}{{template "schema-name" $value}} `{{generateJSONTags $value.Validations $key}}{{generateValidateTags $value.Validations}}`
{{end -}}

{{- if .AdditionalProperties}}
Expand Down
2 changes: 1 addition & 1 deletion test/v2/issues/129/camel/asyncapi.gen.go

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

2 changes: 1 addition & 1 deletion test/v2/issues/129/kebab/asyncapi.gen.go

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

2 changes: 1 addition & 1 deletion test/v2/issues/129/none/asyncapi.gen.go

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

2 changes: 1 addition & 1 deletion test/v2/issues/129/snake/asyncapi.gen.go

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

Loading

0 comments on commit 157fa74

Please sign in to comment.