Skip to content

Commit

Permalink
Added custom tag functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnadratowski committed Feb 21, 2017
1 parent 54d23b6 commit 78fbe8d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 34 deletions.
8 changes: 8 additions & 0 deletions fixtures/codegen/todolist.models.yml
Expand Up @@ -49,6 +49,7 @@ definitions:
minLength: 2
maxLength: 50
Pattern: "[A-Za-z0-9][\\w- ]+"

Notable:
type: object
properties:
Expand Down Expand Up @@ -676,6 +677,13 @@ definitions:
metadata:
$ref: "#/definitions/DynamicMetaData"

WithCustomTag:
type: object
properties:
customtag:
type: string
x-go-custom-tag: mytag:"foo,bar"

HasSpecialCharProp:
type: object
properties:
Expand Down
60 changes: 30 additions & 30 deletions generator/bindata.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions generator/model.go
Expand Up @@ -726,6 +726,10 @@ func (sg *schemaGenContext) buildProperties() error {
sg.GenSchema.HasBaseType = true
}
sg.MergeResult(emprop, false)

if customTag, found := emprop.Schema.Extensions["x-go-custom-tag"]; found {
emprop.GenSchema.CustomTag = customTag.(string)
}
sg.GenSchema.Properties = append(sg.GenSchema.Properties, emprop.GenSchema)
}
sort.Sort(sg.GenSchema.Properties)
Expand Down
24 changes: 22 additions & 2 deletions generator/model_test.go
Expand Up @@ -157,9 +157,10 @@ func TestGenerateModel_SchemaField(t *testing.T) {
gmp.Name = "some name"
gmp.resolvedType = resolvedType{GoType: "string", IsPrimitive: true}
gmp.Title = "The title of the property"
gmp.CustomTag = "mytag:\"foobar,foobaz\""

tt.assertRender(gmp, `// The title of the property
`+"SomeName string `json:\"some name,omitempty\"`\n")
`+"SomeName string `json:\"some name,omitempty\" mytag:\"foobar,foobaz\"`\n")

var fl float64 = 10
var in1 int64 = 20
Expand Down Expand Up @@ -191,7 +192,7 @@ func TestGenerateModel_SchemaField(t *testing.T) {
// Max Items: 30
// Min Items: 30
// Unique: true
`+"SomeName string `json:\"some name\"`\n")
`+"SomeName string `json:\"some name\" mytag:\"foobar,foobaz\"`\n")
}

var schTypeGenDataSimple = []struct {
Expand Down Expand Up @@ -413,6 +414,25 @@ func TestGenerateModel_NotaWithRefRegistry(t *testing.T) {
}
}

func TestGenerateModel_WithCustomTag(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.models.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "WithCustomTag"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
res := buf.String()
assertInCode(t, "mytag:\"foo,bar\"", res)
}
}
}
}

func TestGenerateModel_NotaWithMetaRegistry(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.models.yml")
if assert.NoError(t, err) {
Expand Down
1 change: 1 addition & 0 deletions generator/structs.go
Expand Up @@ -54,6 +54,7 @@ type GenSchema struct {
AdditionalItems *GenSchema
Object *GenSchema
XMLName string
CustomTag string
Properties GenSchemaList
AllOf []GenSchema
HasAdditionalProperties bool
Expand Down
6 changes: 4 additions & 2 deletions generator/templates/structfield.gotmpl
Expand Up @@ -10,15 +10,17 @@
{{- .Name }}
{{- if not .Required }}{{ if not .IsArray }},omitempty{{ end }}{{ end }}
{{- end }}"
{{- if .XMLName }} xml:"{{ .XMLName }}"{{ end }}`
{{- if .XMLName }} xml:"{{ .XMLName }}"{{ end }}
{{- if .CustomTag }} {{ .CustomTag }}{{ end }}`
{{ end }}

{{- define "tuplefield" }}
{{- if not $.IsBaseType -}}
// {{ template "docstring" . }}
{{- template "propertyValidationDocString" .}}
{{ end }}
{{- pascalize .Name}} {{ template "schemaType" . }} `json:"-"` // custom serializer
{{- pascalize .Name}} {{ template "schemaType" . }} `json:"-"
{{- if .CustomTag }} {{ .CustomTag }}{{ end }}` // custom serializer
{{ end }}

{{- define "structfieldIface" }}
Expand Down

0 comments on commit 78fbe8d

Please sign in to comment.