Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codegen): Add flag to ignore string format for type generation #257

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions cmd/asyncapi-codegen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type Flags struct {
// NamingScheme defines the naming case for generated golang structs
// Supported values: camel, none
NamingScheme string

// IgnoreStringFormat states whether the properties' format (date, date-time) should impact the type in types
IgnoreStringFormat bool
}

// SetToCommand adds the flags to a cobra command.
Expand All @@ -58,16 +61,19 @@ func (f *Flags) SetToCommand(cmd *cobra.Command) {
"Schema property key names conversion strategy.\nSupported values: snake, camel, kebab, none.")
cmd.Flags().StringVarP(&f.NamingScheme, "naming-scheme", "n", "none",
"Naming scheme for generated golang elements.\nSupported values: camel, none.")
cmd.Flags().BoolVar(&f.IgnoreStringFormat, "ignore-string-format", false,
"Ignores the format (date, date-time) on string properties, generating golang string, instead of dates")
}

// ToCodegenOptions processes command line flags structure to code generation tool options.
func (f Flags) ToCodegenOptions() (options.Options, error) {
opt := options.Options{
OutputPath: f.OutputPath,
PackageName: f.PackageName,
DisableFormatting: f.DisableFormatting,
ConvertKeys: f.ConvertKeys,
NamingScheme: f.NamingScheme,
OutputPath: f.OutputPath,
PackageName: f.PackageName,
DisableFormatting: f.DisableFormatting,
ConvertKeys: f.ConvertKeys,
NamingScheme: f.NamingScheme,
IgnoreStringFormat: f.IgnoreStringFormat,
}

if f.Generate != "" {
Expand Down
4 changes: 4 additions & 0 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (cg CodeGen) Generate(opt options.Options) error {
return err
}

if opt.IgnoreStringFormat {
template.DisableDateOrTimeGeneration()
}

// Process specification
if err := cg.specification.Process(); err != nil {
return err
Expand Down
12 changes: 6 additions & 6 deletions pkg/codegen/generators/v2/templates/message.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func brokerMessageTo{{namify .Name}}(bMsg extensions.BrokerMessage) ({{namify .N
{{- /* Handle payload based on type */}}
{{- if eq $payload.Type "string"}}
// Convert to string
{{- if and $payload.Format (or (eq $payload.Format "date") (eq $payload.Format "date-time"))}}
{{- if isDateOrDateTimeGenerated $payload.Format }}
t, err := time.Parse(time.RFC3339, string(bMsg.Payload))
if err != nil {
return {{namify .Name}}{}, err
Expand Down Expand Up @@ -115,7 +115,7 @@ func brokerMessageTo{{namify .Name}}(bMsg extensions.BrokerMessage) ({{namify .N
if err != nil {
return msg, err
}
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
t, err := time.Parse(time.RFC3339, string(v))
if err != nil {
return msg, err
Expand All @@ -130,7 +130,7 @@ func brokerMessageTo{{namify .Name}}(bMsg extensions.BrokerMessage) ({{namify .N
if err != nil {
return msg, err
}
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
t, err := time.Parse(time.RFC3339, string(v))
if err != nil {
return msg, err
Expand Down Expand Up @@ -188,7 +188,7 @@ func (msg {{namify .Name}}) toBrokerMessage() (extensions.BrokerMessage, error)
payload := make([]byte, 8)
binary.BigEndian.PutUint64(payload, math.Float64bits(msg.Payload))
{{- end}}
{{- else if and (eq $payload.Type "string") (and $payload.Format (or (eq $payload.Format "date") (eq $payload.Format "date-time")))}}
{{- else if and (eq $payload.Type "string") (isDateOrDateTimeGenerated $payload.Format) }}
// Convert to RFC3339 and to []byte
payload := []byte(msg.Payload.Format(time.RFC3339))
{{- else}}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (msg {{namify .Name}}) toBrokerMessage() (extensions.BrokerMessage, error)
return extensions.BrokerMessage{}, err
}
headers["{{$key}}"] = h
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
headers["{{$key}}"] = []byte(msg.Headers.{{namify $key}}.Format(time.RFC3339))
{{- else }}
headers["{{$key}}"] = []byte(msg.Headers.{{namify $key}})
Expand All @@ -231,7 +231,7 @@ func (msg {{namify .Name}}) toBrokerMessage() (extensions.BrokerMessage, error)
return extensions.BrokerMessage{}, err
}
headers["{{$key}}"] = h
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
headers["{{$key}}"] = []byte(msg.Headers.{{namify $key}}.Format(time.RFC3339))
{{- else }}
headers["{{$key}}"] = []byte(*msg.Headers.{{namify $key}})
Expand Down
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 @@ -35,7 +35,7 @@ type {{ namify .Name }} struct {
type {{ .Name }} {{template "schema-name" .}}

{{/* Create specific marshaling for time */ -}}
{{- if or (eq .Format "date") (eq .Format "date-time") -}}
{{- if isDateOrDateTimeGenerated .Format -}}
{{template "marshaling-time" .}}
{{- end -}}

Expand Down
4 changes: 2 additions & 2 deletions pkg/codegen/generators/v2/templates/schema_name.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ bool

{{- /* --------------------------- Type String -------------------------- */ -}}
{{- else if eq .Type "string" -}}
{{- if and .Format (eq .Format "date") -}}
{{- if and (isDateOrDateTimeGenerated .Format) (eq .Format "date") -}}
civil.Date
{{- else if and .Format (eq .Format "date-time") -}}
{{- else if and (isDateOrDateTimeGenerated .Format) (eq .Format "date-time") -}}
time.Time
{{- else -}}
string
Expand Down
12 changes: 6 additions & 6 deletions pkg/codegen/generators/v3/templates/message.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func brokerMessageTo{{namify .Name}}(bMsg extensions.BrokerMessage) ({{namify .N
{{- /* Handle payload based on type */}}
{{- if eq $payload.Type "string"}}
// Convert to string
{{- if and $payload.Format (or (eq $payload.Format "date") (eq $payload.Format "date-time"))}}
{{- if isDateOrDateTimeGenerated $payload.Format }}
t, err := time.Parse(time.RFC3339, string(bMsg.Payload))
if err != nil {
return {{namify .Name}}{}, err
Expand Down Expand Up @@ -124,7 +124,7 @@ func brokerMessageTo{{namify .Name}}(bMsg extensions.BrokerMessage) ({{namify .N
if err != nil {
return msg, err
}
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
t, err := time.Parse(time.RFC3339, string(v))
if err != nil {
return msg, err
Expand All @@ -143,7 +143,7 @@ func brokerMessageTo{{namify .Name}}(bMsg extensions.BrokerMessage) ({{namify .N
if err != nil {
return msg, err
}
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
t, err := time.Parse(time.RFC3339, string(v))
if err != nil {
return msg, err
Expand Down Expand Up @@ -205,7 +205,7 @@ func (msg {{namify .Name}}) toBrokerMessage() (extensions.BrokerMessage, error)
payload := make([]byte, 8)
binary.BigEndian.PutUint64(payload, math.Float64bits(msg.Payload))
{{- end}}
{{- else if and (eq $payload.Type "string") (and $payload.Format (or (eq $payload.Format "date") (eq $payload.Format "date-time")))}}
{{- else if and (eq $payload.Type "string") (isDateOrDateTimeGenerated $payload.Format) }}
// Convert to RFC3339 and to []byte
payload := []byte(msg.Payload.Format(time.RFC3339))
{{- else}}
Expand Down Expand Up @@ -235,7 +235,7 @@ func (msg {{namify .Name}}) toBrokerMessage() (extensions.BrokerMessage, error)
return extensions.BrokerMessage{}, err
}
headers["{{$key}}"] = h
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
headers["{{$key}}"] = []byte(msg.Headers.{{namify $key}}.Format(time.RFC3339))
{{- else }}
headers["{{$key}}"] = []byte(msg.Headers.{{namify $key}})
Expand All @@ -248,7 +248,7 @@ func (msg {{namify .Name}}) toBrokerMessage() (extensions.BrokerMessage, error)
return extensions.BrokerMessage{}, err
}
headers["{{$key}}"] = h
{{- else if or (eq $value.Format "date") (eq $value.Format "date-time")}}
{{- else if isDateOrDateTimeGenerated $value.Format }}
headers["{{$key}}"] = []byte(msg.Headers.{{namify $key}}.Format(time.RFC3339))
{{- else }}
headers["{{$key}}"] = []byte(*msg.Headers.{{namify $key}})
Expand Down
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 @@ -35,7 +35,7 @@ type {{ namify .Name }} struct {
type {{ .Name }} {{template "schema-name" .}}

{{/* Create specific marshaling for time */ -}}
{{- if or (eq .Format "date") (eq .Format "date-time") -}}
{{- if isDateOrDateTimeGenerated .Format -}}
{{template "marshaling-time" .}}
{{- end -}}

Expand Down
4 changes: 2 additions & 2 deletions pkg/codegen/generators/v3/templates/schema_name.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ bool

{{- /* --------------------------- Type String -------------------------- */ -}}
{{- else if eq .Type "string" -}}
{{- if and .Format (eq .Format "date") -}}
{{- if and (isDateOrDateTimeGenerated .Format) (eq .Format "date") -}}
civil.Date
{{- else if and .Format (eq .Format "date-time") -}}
{{- else if and (isDateOrDateTimeGenerated .Format) (eq .Format "date-time") -}}
time.Time
{{- else -}}
string
Expand Down
3 changes: 3 additions & 0 deletions pkg/codegen/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ type Options struct {
// NamingScheme defines the naming scheme for generated golang structs
// Supported values: camel, none
NamingScheme string

// IgnoreStringFormat states whether the properties' format (date, date-time) should impact the type in types
IgnoreStringFormat bool
}
28 changes: 19 additions & 9 deletions pkg/utils/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,28 @@ func CutSuffix(s, suffix string) string {
return s
}

var isDateOrDateTimeGenerated = func(format string) bool {
return format == "date" || format == "date-time"
}

// DisableDateOrTimeGeneration is used to disable the generation of date/date-time formats within types.
func DisableDateOrTimeGeneration() {
isDateOrDateTimeGenerated = func(_ string) bool { return false }
}

// HelpersFunctions returns the functions that can be used as helpers
// in a golang template.
func HelpersFunctions() template.FuncMap {
return template.FuncMap{
"namifyWithoutParam": NamifyWithoutParams,
"namify": Namify,
"convertKey": ConvertKey,
"snakeCase": strcase.ToSnake,
"hasField": HasField,
"describeStruct": DescribeStruct,
"multiLineComment": MultiLineComment,
"cutSuffix": CutSuffix,
"args": Args,
"namifyWithoutParam": NamifyWithoutParams,
"namify": Namify,
"isDateOrDateTimeGenerated": isDateOrDateTimeGenerated,
"convertKey": ConvertKey,
"snakeCase": strcase.ToSnake,
"hasField": HasField,
"describeStruct": DescribeStruct,
"multiLineComment": MultiLineComment,
"cutSuffix": CutSuffix,
"args": Args,
}
}
17 changes: 17 additions & 0 deletions test/v2/issues/255/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
asyncapi: 2.6.0
info:
title: Sample App
version: 1.2.3

components:
messages:
Test:
payload:
type: object
properties:
DateProp:
type: string
format: date
DateTimeProp:
type: string
format: date-time
Loading