github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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 return &value 86 } 87 88 // Pointer returns a pointer to a freshly-allocated {{ .GoType }}. 89 func ({{ .ReceiverName }} {{ .GoType }}) Pointer() *{{ .GoType }} { 90 return &{{ .ReceiverName }} 91 } 92 {{- end }} 93 {{- end }} 94 {{ template "schemavalidator" . }} 95 {{- else if not (or .IsInterface .IsStream) }} 96 // Validate validates this {{ humanize .Name }}{{/* this schema implements the runtime.Validatable interface but has no validations to check */}} 97 func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) Validate(formats strfmt.Registry) error { 98 return nil 99 } 100 {{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}} 101 {{- end }} 102 {{- if and (not (or .IsInterface .IsStream)) (or .HasContextValidations) }} 103 {{ template "schemacontextvalidator" . }} 104 {{- else if not (or .IsInterface .IsStream) }} 105 // ContextValidate validates this {{ humanize .Name }} based on context it is used {{/* this schema implements the runtime.ContextValidatable interface but has no validations to check */}} 106 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 { 107 return nil 108 } 109 {{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}} 110 {{- end }} 111 {{- end }} 112 {{- if .WantsMarshalBinary }} 113 {{ template "marshalBinarySerializer" . }} 114 {{- end }} 115 {{- define "mapOrSliceGetter" }}{{/* signature for AdditionalProperties and AdditionalItems getter funcs */}} 116 {{- if not .IsBaseType }} 117 {{- if .HasAdditionalProperties }} 118 {{- with .AdditionalProperties }} 119 // {{- template "docstring" . }}{{- template "propertyValidationDocString" . }} 120 {{ pascalize .Name }}() map[string]{{ template "schemaType" . }} 121 {{- end }} 122 {{- end }} 123 {{- with .AdditionalItems }} 124 // {{- template "docstring" . }}{{- template "propertyValidationDocString" . }} 125 {{ pascalize .Name }}() []{{ template "schemaType" . }} 126 {{- end }} 127 {{- else }} 128 // AdditionalProperties in base type shoud be handled just like regular properties{{/* TODO(fred): add full support for AdditionalProperties in base type */}} 129 // At this moment, the base type property is pushed down to the subtype 130 {{- end }} 131 {{- end }}