github.com/AngusLu/go-swagger@v0.28.0/generator/templates/schema.gotmpl (about) 1 {{- if and .IsBaseType .IsExported (not .IsSuperAlias) }} 2 {{- template "schemaPolymorphic" . }} 3 {{- else if .IsSuperAlias }} 4 type {{ pascalize .Name }} {{ template "typeSchemaType" . }}{{/* For types declared as $ref on some other type, just declare the type as a golang _aliased_ type, e.g. type A = B. No method shall be redeclared. */}} 5 {{- if .IsBaseType }} 6 {{ template "baseTypeSerializer" . }}{{/* When the alias redeclares a polymorphic type, define factory methods with this alias. */}} 7 {{- end }} 8 {{- else if .IsEmbedded }} 9 {{- template "schemaEmbedded" . }} 10 {{- else }} 11 {{- if or .IsComplexObject .IsTuple .IsAdditionalProperties }}{{/* TODO(fred): handle case of subtype inheriting from base type with AdditionalProperties, issue #2220 */}} 12 {{ if .Name }}type {{ if not .IsExported }}{{ .Name }}{{ else }}{{ pascalize .Name }}{{ end }}{{ end }} {{ template "schemaBody" . }} 13 {{- range .Properties }} 14 {{- if .IsBaseType }} 15 // {{ pascalize .Name}} gets the {{ humanize .Name }} of this base type{{/* all properties which are of a base type propagate its interface */}} 16 func ({{ $.ReceiverName}} *{{ pascalize $.Name}}) {{ pascalize .Name}}() {{ template "schemaType" . }}{ 17 {{- if eq $.DiscriminatorField .Name }} 18 return {{ printf "%q" $.DiscriminatorValue }} 19 {{- else }} 20 return {{ $.ReceiverName }}.{{camelize .Name}}Field 21 {{- end }} 22 } 23 24 // Set{{ pascalize .Name}} sets the {{ humanize .Name }} of this base type 25 func ({{ $.ReceiverName}} *{{ pascalize $.Name}}) Set{{ pascalize .Name}}(val {{ template "schemaType" . }}) { 26 {{- if ne $.DiscriminatorField .Name }} 27 {{ $.ReceiverName }}.{{camelize .Name}}Field = val 28 {{- end }} 29 } 30 {{- end }} 31 {{- end }} 32 {{- if .Default }}{{/* TODO(fred) - issue #2189 */}} 33 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(b []byte) error { 34 type {{ pascalize .Name }}Alias {{ pascalize .Name }} 35 var t {{ pascalize .Name }}Alias 36 if err := json.Unmarshal([]byte({{printf "%q" (json .Default)}}), &t); err != nil { 37 return err 38 } 39 if err := json.Unmarshal(b, &t); err != nil { 40 return err 41 } 42 *{{.ReceiverName}} = {{ pascalize .Name }}(t) 43 return nil 44 } 45 {{- end }} 46 {{- else }} 47 type {{ pascalize .Name }} {{ template "typeSchemaType" . }} 48 {{- end }} 49 {{- if (and .IsPrimitive .IsAliased .IsCustomFormatter (not (stringContains .Zero "(\""))) }} 50 {{ template "aliasedSerializer" . }} 51 {{- end }} 52 {{- if .IsSubType }} 53 {{ range .AllOf }} 54 {{ range .Properties }} 55 {{- if .IsBaseType }} 56 57 // {{ pascalize .Name}} gets the {{ humanize .Name }} of this subtype 58 func ({{$.ReceiverName}} *{{ pascalize $.Name}}) {{ pascalize .Name}}() {{ template "schemaType" . }}{ 59 {{- if eq $.DiscriminatorField .Name }} 60 return {{ printf "%q" $.DiscriminatorValue }} 61 {{- else }} 62 return {{ $.ReceiverName }}.{{camelize .Name}}Field 63 {{- end }} 64 } 65 66 // Set{{ pascalize .Name}} sets the {{ humanize .Name }} of this subtype 67 func ({{$.ReceiverName}} *{{ pascalize $.Name}}) Set{{ pascalize .Name}}(val {{ template "schemaType" . }}) { 68 {{- if ne $.DiscriminatorField .Name }} 69 {{ $.ReceiverName }}.{{camelize .Name}}Field = val 70 {{- end }} 71 } 72 {{- end }} 73 {{- end }}{{/* TODO(fred): handle AdditionalProperties in base type */}} 74 {{- end }} 75 {{ template "mapOrSliceGetter" . }} 76 {{- end }} 77 {{ template "schemaSerializer" . }} 78 {{- end }} 79 {{- if and .IncludeValidator (not .IsSuperAlias) (not .IsEmbedded) }}{{/* aliased types type A = B do not redefine methods */}} 80 {{- if and (not (or .IsInterface .IsStream)) (or .Required .HasValidations .HasBaseType) }} 81 {{- if (eq .SwaggerType "string") }}{{/* Enum factory for enums for which we generate const (atm, only strings)*/}} 82 {{- if .Enum }} 83 84 func New{{ pascalize .Name }}(value {{ .GoType }}) *{{ .GoType }} { 85 v := value 86 return &v 87 } 88 {{- end }} 89 {{- end }} 90 {{ template "schemavalidator" . }} 91 {{- else if not (or .IsInterface .IsStream) }} 92 // Validate validates this {{ humanize .Name }}{{/* this schema implements the runtime.Validatable interface but has no validations to check */}} 93 func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) Validate(formats strfmt.Registry) error { 94 return nil 95 } 96 {{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}} 97 {{- end }} 98 {{- if and (not (or .IsInterface .IsStream)) (or .HasContextValidations) }} 99 {{ template "schemacontextvalidator" . }} 100 {{- else if not (or .IsInterface .IsStream) }} 101 // ContextValidate validates this {{ humanize .Name }} based on context it is used {{/* this schema implements the runtime.ContextValidatable interface but has no validations to check */}} 102 func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) ContextValidate(ctx context.Context, formats strfmt.Registry) error { 103 return nil 104 } 105 {{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}} 106 {{- end }} 107 {{- end }} 108 {{- if .WantsMarshalBinary }} 109 {{ template "marshalBinarySerializer" . }} 110 {{- end }} 111 {{- define "mapOrSliceGetter" }}{{/* signature for AdditionalProperties and AdditionalItems getter funcs */}} 112 {{- if not .IsBaseType }} 113 {{- if .HasAdditionalProperties }} 114 {{- with .AdditionalProperties }} 115 // {{- template "docstring" . }}{{- template "propertyValidationDocString" . }} 116 {{ pascalize .Name }}() map[string]{{ template "schemaType" . }} 117 {{- end }} 118 {{- end }} 119 {{- with .AdditionalItems }} 120 // {{- template "docstring" . }}{{- template "propertyValidationDocString" . }} 121 {{ pascalize .Name }}() []{{ template "schemaType" . }} 122 {{- end }} 123 {{- else }} 124 // AdditionalProperties in base type shoud be handled just like regular properties{{/* TODO(fred): add full support for AdditionalProperties in base type */}} 125 // At this moment, the base type property is pushed down to the subtype 126 {{- end }} 127 {{- end }}