github.com/Desuuuu/genqlient@v0.5.3/generate/marshal.go.tmpl (about)

     1  {{/* We generate MarshalJSON for much the same reasons as UnmarshalJSON -- see
     2       unmarshal.go.tmpl for details.  (Note we generate both even if genqlient
     3       itself needs only UnmarshalJSON, for the benefit of callers who want to,
     4       for example, put genqlient responses in a cache.)  But our implementation
     5       for marshaling is quite different.
     6  
     7       Specifically, the treatment of field-visibility with embedded fields must
     8       differ from both ordinary encoding/json and unmarshaling: we need to
     9       choose exactly one conflicting field in all cases (whereas Go chooses at
    10       most one and when unmarshaling we choose them all).  See
    11       goStructType.FlattenedFields in types.go for more discussion of embedding
    12       and visibility.
    13  
    14       To accomplish that, we essentially flatten out all the embedded fields
    15       when marshaling, following those precedence rules.  Then we basically
    16       follow what we do in unmarshaling, but in reverse order: first we marshal
    17       the special fields, then we glue everything together with the ordinary
    18       fields.
    19  
    20       We do one other thing differently, for the benefit of the marshal-helper
    21       in marshal_helper.go.tmpl.  While when unmarshaling it's easy to unmarshal
    22       out the __typename field, then unmarshal out everything else, with
    23       marshaling we can't do the same (at least not without some careful
    24       JSON-stitching; the considerations are basically the same as those
    25       discussed in FlattenedFields).  So we write out a helper method
    26       __premarshalJSON() which basically does all but the final JSON-marshal.
    27       (Then the real MarshalJSON() just calls that, and then marshals.)
    28       Thus a marshal-helper for this type, if any, can call __premarshalJSON()
    29       directly, and embed its result. */}}
    30  
    31  type __premarshal{{.GoName}} struct{
    32      {{range .FlattenedFields -}}
    33      {{if .NeedsMarshaling -}}
    34      {{.GoName}} {{repeat .GoType.SliceDepth "[]"}}{{ref "encoding/json.RawMessage"}} `json:"{{.JSONName}}"`
    35      {{else}}
    36      {{.GoName}} {{.GoType.Reference}} `json:"{{.JSONName}}"`
    37      {{end}}
    38      {{end}}
    39  }
    40  
    41  func (v *{{.GoName}}) MarshalJSON() ([]byte, error) {
    42      premarshaled, err := v.__premarshalJSON()
    43      if err != nil {
    44          return nil, err
    45      }
    46      return json.Marshal(premarshaled)
    47  }
    48  
    49  func (v *{{.GoName}}) __premarshalJSON() (*__premarshal{{.GoName}}, error) {
    50      var retval __premarshal{{.GoName}}
    51  
    52      {{range $field := .FlattenedFields -}}
    53      {{if $field.NeedsMarshaling -}}
    54      {
    55          {{/* Here dst is the json.RawMessage, and src is the Go type. */}}
    56          dst := &retval.{{$field.GoName}}
    57          src := v.{{$field.Selector}}
    58          {{range $i := intRange $field.GoType.SliceDepth -}}
    59          *dst = make(
    60              {{repeat (sub $field.GoType.SliceDepth $i) "[]"}}{{ref "encoding/json.RawMessage"}},
    61              len(src))
    62          for i, src := range src {
    63              dst := &(*dst)[i]
    64          {{end -}}
    65          {{/* src now has type <GoType>; dst is json.RawMessage */ -}}
    66          {{if $field.GoType.IsPointer -}}
    67          {{/* If you passed a pointer, and it's nil, don't call the
    68               marshaler.  This matches json.Marshal's behavior. */ -}}
    69          if src != nil {
    70          {{end -}}
    71          var err error
    72          *dst, err = {{$field.Marshaler $.Generator}}(
    73              {{/* src is the struct-field (or field-element, etc.).
    74                   We want to pass a pointer to the type you specified, so if
    75                   there's a pointer on the field that's exactly what we want,
    76                   and if not we need to take the address. */ -}}
    77              {{if not $field.GoType.IsPointer}}&{{end}}src)
    78          if err != nil {
    79              return nil, fmt.Errorf(
    80                  "Unable to marshal {{$.GoName}}.{{$field.Selector}}: %w", err)
    81          }
    82          {{if $field.GoType.IsPointer -}}
    83          }{{/* end if src != nil */}}
    84          {{end -}}
    85          {{range $i := intRange $field.GoType.SliceDepth -}}
    86          }
    87          {{end -}}
    88      }
    89      {{else -}}
    90      retval.{{$field.GoName}} = v.{{$field.Selector}}
    91      {{end -}}
    92      {{end -}}
    93  
    94      return &retval, nil
    95  }