github.com/6543-forks/go-swagger@v0.26.0/generator/templates/serializers/allofserializer.gotmpl (about)

     1  {{ define "allOfSerializer" }}
     2    {{- $receiverName := .ReceiverName }}
     3  // UnmarshalJSON unmarshals this object from a JSON structure
     4  func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(raw []byte) error {
     5    {{- range .AllOf }}
     6      // {{ pascalize .Name }}
     7      {{- if and .IsAnonymous .Properties }}{{/* unmarshalling properties in all of anonymous objects */}}
     8        {{- $part :=  pascalize .Name }}
     9        var data{{ $part }} struct {
    10        {{- range .Properties }}
    11          {{- if not .IsBaseType }}
    12            {{- if not $.IsExported }}
    13              {{ template "privstructfield" . }}
    14            {{- else }}
    15              {{ pascalize .Name}} {{ template "schemaType" . }} `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
    16            {{- end }}
    17          {{ else }}
    18            {{ if not $.IsExported }}
    19              {{ template "privstructfield" . }}
    20            {{ else }}
    21              {{ pascalize .Name}} json.RawMessage `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
    22            {{ end }}
    23          {{ end }}
    24        {{- end }}
    25        {{- if .HasAdditionalProperties }}
    26          {{ pascalize .AdditionalProperties.Name }}{{ if not .IsExported }}Field{{ end }} map[string]{{ template "schemaType" .AdditionalProperties }} `json:"-"`
    27        {{- end }}
    28        {{- if .AdditionalItems }}
    29          {{ pascalize .AdditionalItems.Name }}{{ if or (not .IsExported) .IsSubType }}Field{{ end }} []{{ template "schemaType" .AdditionalItems }} `json:"-"`
    30        {{- end }}
    31    }
    32    if err := swag.ReadJSON(raw, &data{{ $part }}); err != nil {
    33      return err
    34    }
    35        {{ range .Properties }}
    36    {{ $receiverName }}.{{ pascalize .Name }} = data{{ $part }}.{{ pascalize .Name }}
    37        {{ end }}
    38      {{- else if .IsAnonymous }}
    39    var {{ varname .Name }} {{ .GoType }}
    40    if err := {{ if .IsBaseType}}Unmarshal{{ .GoType }}(bytes.NewBuffer(raw), &{{ varname .Name }}){{ else }} swag.ReadJSON(raw, &{{ varname .Name }}){{ end }}; err != nil {
    41      return err
    42    }
    43    {{ .ValueExpression }} = {{ varname .Name }}
    44      {{- end }}
    45      {{- if not .IsAnonymous }}{{/* unmarshalling allOf named objects */}}
    46    var {{ varname .Name }} {{ .GoType }}
    47    if err := {{ if .IsBaseType}}Unmarshal{{ .GoType }}(bytes.NewBuffer(raw), &{{ varname .Name }}){{ else }} swag.ReadJSON(raw, &{{ varname .Name }}){{ end }}; err != nil {
    48      return err
    49    }
    50    {{ .ReceiverName }}.{{ dropPackage .GoType }} = {{ varname .Name }}
    51      {{ end }}
    52    {{ end }}
    53    {{- if .Properties }}
    54      // now for regular properties
    55      {{- $part :=  pascalize .Name }}
    56      var props{{ $part }} struct {
    57      {{- range .Properties }}
    58        {{- if not .IsBaseType }}
    59          {{- if not $.IsExported }}
    60            {{ template "privstructfield" . }}
    61          {{- else }}
    62            {{ pascalize .Name}} {{ template "schemaType" . }} `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
    63          {{- end }}
    64        {{- else }}
    65          {{- if not $.IsExported }}
    66            {{ template "privstructfield" . }}
    67          {{- else }}
    68            {{ pascalize .Name}} json.RawMessage `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
    69          {{- end }}
    70        {{- end }}
    71      {{ end }}
    72      }
    73      if err := swag.ReadJSON(raw, &props{{ $part }}); err != nil {
    74         return err
    75      }
    76      {{- range .Properties }}
    77        {{ $receiverName }}.{{ pascalize .Name }} = props{{ $part }}.{{ pascalize .Name }}
    78      {{ end }}
    79    {{- end }}
    80    {{ if .HasAdditionalProperties }}
    81    // TODO: AdditionalProperties
    82    {{- end }}
    83    {{- if .AdditionalItems }}
    84    // TODO: AdditionalItems
    85    {{- end }}
    86    return nil
    87  }
    88  
    89  // MarshalJSON marshals this object to a JSON structure
    90  func ({{.ReceiverName}} {{ pascalize .Name }}) MarshalJSON() ([]byte, error) {
    91    _parts := make([][]byte, 0, {{ len .AllOf }})
    92    {{ range .AllOf }}
    93      {{- if and .IsAnonymous .Properties }}
    94        {{- $part :=  pascalize .Name }}
    95    var data{{ $part }} struct {
    96        {{- range .Properties }}
    97          {{- if not .IsBaseType }}
    98            {{- if not $.IsExported }}
    99              {{ template "privstructfield" . }}
   100            {{- else }}
   101              {{ pascalize .Name}} {{ template "schemaType" . }} `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
   102            {{- end }}
   103          {{- else }}
   104            {{- if not $.IsExported }}
   105              {{ template "privstructfield" . }}
   106            {{- else }}
   107              {{ pascalize .Name}} json.RawMessage `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
   108              {{- end }}
   109          {{- end }}
   110        {{ end }}
   111        {{- if .HasAdditionalProperties }}
   112          {{ pascalize .AdditionalProperties.Name }}{{ if not .IsExported }}Field{{ end }} map[string]{{ template "schemaType" .AdditionalProperties }} `json:"-"`
   113        {{- end }}
   114        {{- if .AdditionalItems }}
   115          {{ pascalize .AdditionalItems.Name }}{{ if or (not .IsExported) .IsSubType }}Field{{ end }} []{{ template "schemaType" .AdditionalItems }} `json:"-"`
   116        {{- end }}
   117    }
   118  
   119        {{ range .Properties }}
   120    data{{ $part }}.{{ pascalize .Name }} = {{ $receiverName }}.{{ pascalize .Name }}
   121        {{ end }}
   122  
   123    jsonData{{ $part }}, err{{ $part }} := swag.WriteJSON(data{{ $part }})
   124    if err{{ $part }} != nil {
   125      return nil, err{{ $part }}
   126    }
   127    _parts = append(_parts, jsonData{{ $part }})
   128      {{- else if .IsAnonymous }}{{/* unmarshalling anonymous type composition */}}
   129    {{ varname .Name }}, err := swag.WriteJSON({{ .ValueExpression }})
   130    if err != nil {
   131      return nil, err
   132    }
   133    _parts = append(_parts, {{ varname .Name }})
   134      {{- end }}
   135      {{- if not .IsAnonymous }}
   136  
   137    {{ varname .Name }}, err := swag.WriteJSON({{ $receiverName }}.{{ dropPackage .GoType }})
   138    if err != nil {
   139      return nil, err
   140    }
   141    _parts = append(_parts, {{ varname .Name }})
   142      {{- end }}
   143    {{- end }}
   144    {{- if .Properties }}
   145  
   146      // now for regular properties
   147      {{- $part :=  pascalize .Name }}
   148      var props{{ $part }} struct {
   149      {{- range .Properties }}
   150        {{- if not .IsBaseType }}
   151          {{- if not $.IsExported }}
   152            {{ template "privstructfield" . }}
   153          {{- else }}
   154            {{ pascalize .Name}} {{ template "schemaType" . }} `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
   155          {{- end }}
   156        {{- else }}
   157          {{- if not $.IsExported }}
   158            {{ template "privstructfield" . }}
   159          {{- else }}
   160            {{ pascalize .Name}} json.RawMessage `json:"{{ .OriginalName }}{{ if and (not .Required) .IsEmptyOmitted }},omitempty{{ end }}{{ if .IsJSONString }},string{{ end }}"`
   161          {{- end }}
   162        {{- end }}
   163      {{ end }}
   164      }
   165      {{- range .Properties }}
   166      props{{ $part }}.{{ pascalize .Name }} = {{ $receiverName }}.{{ pascalize .Name }}
   167      {{ end }}
   168      jsonDataProps{{ $part }}, err{{ $part }} := swag.WriteJSON(props{{ $part }})
   169      if err{{ $part }} != nil {
   170          return nil, err{{ $part }}
   171      }
   172      _parts = append(_parts, jsonDataProps{{ $part }})
   173    {{- end }}
   174    {{- if .HasAdditionalProperties }}
   175    {{- end }}
   176    {{- if .HasAdditionalItems }}
   177    {{- end }}
   178    return swag.ConcatJSON(_parts...), nil
   179  }
   180  {{- end }}