Skip to content

Commit

Permalink
fix: format-able spec (#237)
Browse files Browse the repository at this point in the history
* fix: format-able spec
* fix: handle const as string

---------

Co-authored-by: Nick Chen <[email protected]>
  • Loading branch information
nickchen and nginx-nickc authored Jun 20, 2024
1 parent 81fbfc2 commit fe9359f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pkg/asyncapi/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func (s *Schema) generateMetadata(name string, isRequired bool) error {

// Set IsRequired
s.IsRequired = isRequired

if s.Type == "" {
if _, ok := s.Const.(string); ok {
s.Type = "string"
}
}
return nil
}

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 @@ -13,7 +13,7 @@ type {{ namify .Name }} struct {
{{if $value.Description}}
// Description: {{multiLineComment $value.Description}}
{{else if and $value.ReferenceTo $value.ReferenceTo.Description}}
// Description: {{$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}}`
{{end -}}
Expand Down
3 changes: 2 additions & 1 deletion pkg/codegen/generators/v2/templates/schema_name.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ struct {
{{- else if .ReferenceTo -}}
{{ namify .Follow.Name }}

{{- /* ----------------------- Unsupported usecase ---------------------- */ -}}
{{- /* ----------------------- Unsupported use case ---------------------- */ -}}
{{- else -}}
interface{}
// WARNING: potential error in AsyncAPI generation
// Infos on type: {{ describeStruct . }}
{{- end -}}
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 @@ -13,7 +13,7 @@ type {{ namify .Name }} struct {
{{if $value.Description}}
// Description: {{multiLineComment $value.Description}}
{{else if and $value.ReferenceTo $value.ReferenceTo.Description}}
// Description: {{$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}}`
{{end -}}
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/generators/v3/templates/schema_name.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct {

{{- /* ----------------------- Unsupported usecase ---------------------- */ -}}
{{- else -}}
interface{}
// WARNING: potential error in AsyncAPI generation
// Infos on type: {{ describeStruct . }}
{{- end -}}
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ func HasField(v any, name string) bool {
return rv.FieldByName(name).IsValid()
}

// DescribeStruct will describe a struct in a human readable way using `%+v`
// DescribeStruct will describe a struct in a human-readable way using `%+v`
// format from the standard library.
func DescribeStruct(st any) string {
return fmt.Sprintf("%+v", st)
return MultiLineComment(fmt.Sprintf("%+v", st))
}

// MultiLineComment will prefix each line of a comment with "// " in order to
Expand Down

0 comments on commit fe9359f

Please sign in to comment.