github.com/6543-forks/go-swagger@v0.26.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      {{ template "schemavalidator" . }}
    82    {{- else if not (or .IsInterface .IsStream) }}
    83  // Validate validates this {{ humanize .Name }}{{/* this schema implements the runtime.Validatable interface but has no validations to check */}}
    84  func ({{.ReceiverName}} {{ if or .IsTuple .IsComplexObject .IsAdditionalProperties }}*{{ end }}{{ if or (not .IsExported) .Discriminates }}{{ camelize .Name }}{{ else }}{{ pascalize .Name }}{{ end }}) Validate(formats strfmt.Registry) error {
    85    return nil
    86  }
    87    {{- else }}{{/* {{ .Name }} does not implement the runtime.Validatable interface: noop */}}
    88    {{- end }}
    89  {{- end }}
    90  {{- if .WantsMarshalBinary }}
    91    {{ template "marshalBinarySerializer" . }}
    92  {{- end }}
    93  {{- define "mapOrSliceGetter" }}{{/* signature for AdditionalProperties and AdditionalItems getter funcs */}}
    94    {{- if not .IsBaseType }}
    95      {{- if .HasAdditionalProperties }}
    96        {{- with .AdditionalProperties }}
    97          // {{- template "docstring" . }}{{- template "propertyValidationDocString" . }}
    98          {{ pascalize .Name }}() map[string]{{ template "schemaType" . }}
    99        {{- end }}
   100      {{- end }}
   101      {{- with .AdditionalItems }}
   102        // {{- template "docstring" . }}{{- template "propertyValidationDocString" . }}
   103        {{ pascalize .Name }}() []{{ template "schemaType" . }}
   104      {{- end }}
   105    {{- else }}
   106    // AdditionalProperties in base type shoud be handled just like regular properties{{/* TODO(fred): add full support for AdditionalProperties in base type */}}
   107    // At this moment, the base type property is pushed down to the subtype
   108    {{- end }}
   109  {{- end }}