github.com/livermorium/go-swagger@v0.19.0/generator/templates/schema.gotmpl (about) 1 {{ if .IncludeModel -}} 2 {{ if and .IsBaseType .IsExported (not .IsSuperAlias) -}}type {{ pascalize .Name }} interface { 3 {{- if not (or .IsInterface .IsStream) }}{{/* 4 A base type is always Validatable. 5 Under normal conditions, we can't have a base type rendered a .IsStream or .IsInterface: this check is just for sanity check). 6 7 In the definition of the base type itself, this means that the unexported struct holding 8 the definition of the base type has a Validate() func. 9 */}} 10 runtime.Validatable 11 {{- end }} 12 {{ range .AllOf }} 13 {{ if .IsAnonymous }} 14 {{ range .Properties }} 15 {{ if $.IsTuple }}{{ template "tuplefieldIface" . }}{{ else }}{{template "structfieldIface" . }}{{ end }} 16 {{ end }} 17 {{- if .HasAdditionalProperties }} 18 {{- if .AdditionalProperties -}} 19 // {{- template "docstring" .AdditionalProperties }} 20 {{- template "propertyValidationDocString" .AdditionalProperties}} {{ pascalize .AdditionalProperties.Name }}() map[string]{{ template "schemaType" .AdditionalProperties }} 21 {{end}} 22 {{ end}} 23 {{ if .AdditionalItems -}} 24 // {{- template "docstring" .AdditionalItems }} 25 {{- template "propertyValidationDocString" .AdditionalItems}} {{ pascalize .AdditionalItems.Name }}() []{{ template "schemaType" .AdditionalItems }} 26 {{ end }} 27 {{ else }} 28 {{ .GoType }} 29 {{ end }} 30 {{ end }} 31 {{range .Properties}} 32 {{ if $.IsTuple }} 33 {{ template "tuplefieldIface" . }} 34 {{ else }} 35 {{template "structfieldIface" . }} 36 {{ end }} 37 {{end}} 38 {{ if .HasAdditionalProperties }} 39 {{- if .AdditionalProperties -}} 40 // {{- template "docstring" .AdditionalProperties }} 41 {{- template "propertyValidationDocString" .AdditionalProperties}} {{ pascalize .AdditionalProperties.Name }}() map[string]{{ template "schemaType" .AdditionalProperties }} 42 {{ end }} 43 {{ end }} 44 {{ if .AdditionalItems -}} 45 // {{- template "docstring" .AdditionalItems }} 46 {{- template "propertyValidationDocString" .AdditionalItems}} {{ pascalize .AdditionalItems.Name }}() []{{ template "schemaType" .AdditionalItems }} 47 {{ end }} 48 } 49 50 type {{ camelize .Name }} {{ template "schemaBody" . }} 51 52 {{ range .Properties }} 53 // {{ pascalize .Name}} gets the {{ humanize .Name }} of this polymorphic type 54 func ({{ $.ReceiverName}} *{{ camelize $.Name}}) {{ pascalize .Name}}() {{ template "schemaType" . }}{ 55 {{ if eq $.DiscriminatorField .Name }}return {{ printf "%q" $.DiscriminatorValue }}{{ else }}return {{ $.ReceiverName }}.{{camelize .Name}}Field{{end}} 56 } 57 // Set{{ pascalize .Name}} sets the {{ humanize .Name }} of this polymorphic type 58 func ({{ $.ReceiverName}} *{{ camelize $.Name}}) Set{{ pascalize .Name}}(val {{ template "schemaType" . }}) { 59 {{ if ne $.DiscriminatorField .Name }}{{ $.ReceiverName }}.{{camelize .Name}}Field = val{{end}} 60 } 61 {{ end }} 62 // Unmarshal{{ pascalize .Name }}Slice unmarshals polymorphic slices of {{ pascalize .Name }} 63 func Unmarshal{{ pascalize .Name }}Slice(reader io.Reader, consumer runtime.Consumer) ([]{{ pascalize .Name }}, error) { 64 var elements []json.RawMessage 65 if err := consumer.Consume(reader, &elements); err != nil { 66 return nil, err 67 } 68 69 var result []{{ pascalize .Name }} 70 for _, element := range elements { 71 obj, err := unmarshal{{ pascalize .Name }}(element, consumer) 72 if err != nil { 73 return nil, err 74 } 75 result = append(result, obj) 76 } 77 return result, nil 78 } 79 80 81 // Unmarshal{{ pascalize .Name }} unmarshals polymorphic {{ pascalize .Name }} 82 func Unmarshal{{ pascalize .Name }}(reader io.Reader, consumer runtime.Consumer) ({{ pascalize .Name }}, error) { 83 // we need to read this twice, so first into a buffer 84 data, err := ioutil.ReadAll(reader) 85 if err != nil { 86 return nil, err 87 } 88 return unmarshal{{ pascalize .Name }}(data, consumer) 89 } 90 91 func unmarshal{{ pascalize .Name }}(data []byte, consumer runtime.Consumer) ({{ pascalize .Name }}, error) { 92 buf := bytes.NewBuffer(data) 93 {{ if .Discriminates }} buf2 := bytes.NewBuffer(data) {{ end }} 94 95 // the first time this is read is to fetch the value of the {{ .DiscriminatorField }} property. 96 var getType struct { {{ pascalize .DiscriminatorField }} string `json:{{ printf "%q" .DiscriminatorField }}` } 97 if err := consumer.Consume(buf, &getType); err != nil { 98 return nil, err 99 } 100 101 if err := validate.RequiredString({{ printf "%q" .DiscriminatorField }}, "body", getType.{{ pascalize .DiscriminatorField }}); err != nil { 102 return nil, err 103 } 104 105 // The value of {{ .DiscriminatorField }} is used to determine which type to create and unmarshal the data into 106 switch getType.{{ pascalize .DiscriminatorField }} { {{ range $k, $v := .Discriminates }} 107 case {{ printf "%q" $k }}: 108 var result {{ if eq (upper (pascalize $.Name)) (upper $v) }}{{ camelize $.Name }}{{ else }}{{ $v }}{{ end }} 109 if err := consumer.Consume(buf2, &result); err != nil { 110 return nil, err 111 } 112 return &result, nil 113 {{ end }} 114 } 115 return nil, errors.New(422, "invalid {{ .DiscriminatorField }} value: %q", getType.{{ pascalize .DiscriminatorField }}) 116 117 } 118 {{ else if .IsSuperAlias -}}{{/* 119 For types declared as $ref on some other type, just declare the type as a 120 golang _aliased_ type, e.g. type A = B. No method shall be redeclared. 121 */}} 122 type {{ pascalize .Name }} {{ template "typeSchemaType" . }} 123 {{ if .IsBaseType }}{{/* 124 When the alias redeclares a polymorphic type, define factory methods with this alias. 125 */}} 126 // Unmarshal{{ pascalize .Name }} unmarshals polymorphic {{ pascalize .Name }} 127 func Unmarshal{{ pascalize .Name }}(reader io.Reader, consumer runtime.Consumer) ({{ pascalize .Name }}, error) { 128 return Unmarshal{{ pascalize .GoType }}(reader, consumer) 129 } 130 131 // Unmarshal{{ pascalize .Name }}Slice unmarshals polymorphic slices of {{ pascalize .Name }} 132 func Unmarshal{{ pascalize .Name }}Slice(reader io.Reader, consumer runtime.Consumer) ([]{{ pascalize .Name }}, error) { 133 return Unmarshal{{ pascalize .GoType }}Slice(reader, consumer) 134 } 135 {{ end }} 136 {{ else -}} 137 {{ if or .IsComplexObject .IsTuple .IsAdditionalProperties -}} 138 {{ if .Name }}type {{ if not .IsExported }}{{ .Name }}{{ else }}{{ pascalize .Name }}{{ end }}{{ end }} {{ template "schemaBody" . }} 139 {{ range .Properties }} 140 {{ if .IsBaseType }} 141 // {{ pascalize .Name}} gets the {{ humanize .Name }} of this base type 142 func ({{ $.ReceiverName}} *{{ pascalize $.Name}}) {{ pascalize .Name}}() {{ template "schemaType" . }}{ 143 {{ if eq $.DiscriminatorField .Name }}return {{ printf "%q" $.DiscriminatorValue }}{{ else }}return {{ $.ReceiverName }}.{{camelize .Name}}Field{{end}} 144 } 145 // Set{{ pascalize .Name}} sets the {{ humanize .Name }} of this base type 146 func ({{ $.ReceiverName}} *{{ pascalize $.Name}}) Set{{ pascalize .Name}}(val {{ template "schemaType" . }}) { 147 {{ if ne $.DiscriminatorField .Name }}{{ $.ReceiverName }}.{{camelize .Name}}Field = val{{end}} 148 } 149 {{ end }} 150 {{ end }} 151 {{if .Default}} 152 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(b []byte) error { 153 type {{ pascalize .Name }}Alias {{ pascalize .Name }} 154 var t {{ pascalize .Name }}Alias 155 if err := json.Unmarshal([]byte({{printf "%q" (json .Default)}}), &t); err != nil { 156 return err 157 } 158 if err := json.Unmarshal(b, &t); err != nil { 159 return err 160 } 161 *{{.ReceiverName}} = {{ pascalize .Name }}(t) 162 return nil 163 } 164 {{end}} 165 {{ else -}} 166 type {{ pascalize .Name }} {{ template "typeSchemaType" . }} 167 {{ end -}} 168 {{ if (and .IsPrimitive .IsAliased .IsCustomFormatter (not (stringContains .Zero "(\""))) }} 169 // UnmarshalJSON sets a {{ pascalize .Name }} value from JSON input 170 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(b []byte) error { 171 return ((*{{ .AliasedType }})({{ .ReceiverName}})).UnmarshalJSON(b) 172 } 173 174 // MarshalJSON retrieves a {{ pascalize .Name }} value as JSON output 175 func ({{.ReceiverName}} {{ pascalize .Name }}) MarshalJSON() ([]byte, error) { 176 return ({{ .AliasedType }}({{ .ReceiverName}})).MarshalJSON() 177 } 178 {{ end -}} 179 {{ if .IsSubType }} 180 {{ range .AllOf }} 181 {{ range .Properties }} 182 // {{ pascalize .Name}} gets the {{ humanize .Name }} of this subtype 183 {{ if .IsBaseType }}func ({{$.ReceiverName}} *{{ pascalize $.Name}}) {{ pascalize .Name}}() {{ template "schemaType" . }}{ 184 {{ if eq $.DiscriminatorField .Name }}return {{ printf "%q" $.DiscriminatorValue }}{{ else }}return {{ $.ReceiverName }}.{{camelize .Name}}Field{{end}} 185 } 186 // Set{{ pascalize .Name}} sets the {{ humanize .Name }} of this subtype 187 func ({{$.ReceiverName}} *{{ pascalize $.Name}}) Set{{ pascalize .Name}}(val {{ template "schemaType" . }}) { 188 {{ if ne $.DiscriminatorField .Name }}{{ $.ReceiverName }}.{{camelize .Name}}Field = val{{end}} 189 } 190 {{ end }} 191 {{ end }} 192 {{ end }} 193 {{ if .HasAdditionalProperties }} 194 {{- if .AdditionalProperties -}} 195 // {{- template "docstring" .AdditionalProperties }} 196 {{- template "propertyValidationDocString" .AdditionalProperties}} {{ pascalize .AdditionalProperties.Name }}() map[string]{{ template "schemaType" .AdditionalProperties }}{{end}}{{ end }} 197 {{ if .AdditionalItems -}} 198 // {{ template "docstring" .AdditionalItems }} 199 {{- template "propertyValidationDocString" .AdditionalItems}} {{ pascalize .AdditionalItems.Name }}() []{{ template "schemaType" .AdditionalItems }} 200 {{ end }}{{ end -}} 201 {{ template "schemaSerializer" . }}{{ end }} 202 {{- if and .IncludeValidator (not .IsSuperAlias) -}} 203 {{- if and (not (or .IsInterface .IsStream)) (or .Required .HasValidations .HasBaseType) }} 204 {{ template "schemavalidator" . }} 205 {{- else if not (or .IsInterface .IsStream) -}} 206 // Validate validates this {{ humanize .Name }}{{/* this schema implements the runtime.Validatable interface but has no validations to check */}} 207 func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) Validate(formats strfmt.Registry) error { 208 return nil 209 } 210 {{- else -}} 211 {{- /* {{ .Name }} does not implement the runtime.Validatable interface */ -}} 212 {{- end -}} 213 {{- end }} 214 {{- end }} 215 {{ if and (not (or .IsInterface .IsStream .IsBaseType)) (or .IsTuple .IsComplexObject .IsAdditionalProperties (and .IsPrimitive .IsAliased .IsCustomFormatter (not (stringContains .Zero "(\"")))) }} 216 // MarshalBinary interface implementation 217 func ({{.ReceiverName}} *{{ pascalize .Name }}) MarshalBinary() ([]byte, error) { 218 if {{ .ReceiverName }} == nil { 219 return nil, nil 220 } 221 return swag.WriteJSON({{ .ReceiverName }}) 222 } 223 224 // UnmarshalBinary interface implementation 225 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalBinary(b []byte) error { 226 var res {{ pascalize .Name }} 227 if err := swag.ReadJSON(b, &res); err != nil { 228 return err 229 } 230 *{{ .ReceiverName }} = res 231 return nil 232 } 233 {{ end -}} 234