github.com/mailru/activerecord@v1.12.2/internal/pkg/generator/tmpl/tarantool/fixture.tmpl (about)

     1  package {{ .ARPkg }}
     2  
     3  import (
     4      "context"
     5      "log"
     6  
     7      "gopkg.in/vmihailenco/msgpack.v2"
     8      "gopkg.in/yaml.v3"
     9  {{- range $ind, $imp := .Imports }}
    10  {{ if ne $imp.ImportName "" }}{{ $imp.ImportName }} {{ end }}"{{ $imp.Path }}"
    11  {{- end }}
    12  {{- range $i, $imp := addImport .FieldList }}
    13      "{{ $imp }}"
    14  {{- end }}
    15  )
    16  
    17  {{ $PublicStructName := .ARPkgTitle -}}
    18  {{ $serializers := .Serializers -}}
    19  {{ $fields := .FieldList }}
    20  {{ $procfields := .ProcOutFieldList }}
    21  
    22  
    23  type {{ $PublicStructName }}FT struct {
    24      {{- range $ind, $fstruct := .FieldList -}}
    25      {{ $rtype := $fstruct.Format -}}
    26      {{ $serlen := len $fstruct.Serializer -}}
    27      {{ if ne $serlen 0 -}}
    28      {{ $sname := index $fstruct.Serializer 0 -}}
    29      {{ $serializer := index $serializers $sname -}}
    30      {{ $rtype = $serializer.Type -}}
    31      {{ end }}
    32      {{ $fstruct.Name }} {{ $rtype -}} `yaml:"{{ $fstruct.Name | snakeCase -}}" mapstructure:"{{ $fstruct.Name | snakeCase -}}" json:"{{ $fstruct.Name | snakeCase -}}"`
    33      {{- end }}
    34  }
    35  
    36  func UnmarshalFixtures(source []byte) ([]{{$PublicStructName}}FT, error) {
    37      var fts []{{$PublicStructName}}FT
    38      if err := msgpack.Unmarshal(source, &fts); err != nil {
    39          return nil, err
    40      }
    41  
    42      return fts, nil
    43  }
    44  
    45  func (obj *{{ $PublicStructName }}FT) DecodeMsgpack(dec *msgpack.Decoder) error {
    46      l, err := dec.DecodeArrayLen()
    47      if err != nil {
    48          return err
    49      }
    50  
    51      if l != {{ len .FieldList }} {
    52          return fmt.Errorf("unexpected count of fields")
    53      }
    54  
    55      {{ range $ind, $fstruct := .FieldList -}}
    56      {{ $rtype := $fstruct.Format -}}
    57      {{ $sname := $fstruct.Serializer.Name -}}
    58      {{ if ne $sname "" -}}
    59      {{ $serializer := index $serializers $sname -}}
    60      {{ $serparams := $fstruct.Serializer.Params }}
    61      var v{{ $fstruct.Name }}  {{ $rtype }}
    62      if err := dec.Decode(&v{{ $fstruct.Name }}); err != nil {
    63          return fmt.Errorf("can't decode '{{ $fstruct.Name }}' field: %w", err)
    64      }
    65  
    66      if err := {{ $serializer.ImportName }}.{{ $serializer.Unmarshaler }}({{ $serparams }}v{{ $fstruct.Name }}, &obj.{{ $fstruct.Name }}); err != nil {
    67          return fmt.Errorf("can't unpack '{{ $fstruct.Name }}' field: %w", err)
    68      }
    69      {{- else }}
    70      if err := dec.Decode(&obj.{{ $fstruct.Name }}); err != nil {
    71          return fmt.Errorf("can't decode field '{{ $fstruct.Name }}': %w", err)
    72      }
    73      {{ end }}
    74      {{ end }}
    75  
    76      return nil
    77  }
    78  
    79  func UnmarshalFromYaml(source []byte) []*{{$PublicStructName}} {
    80      var fixtures []{{$PublicStructName}}FT
    81  
    82      if err := yaml.Unmarshal(source, &fixtures); err != nil {
    83          log.Fatalf("unmarshal {{$PublicStructName}}FT fixture: %v", err)
    84      }
    85  
    86      objs := make([]*{{$PublicStructName}}, 0, len(fixtures))
    87  
    88      for _, ft := range fixtures {
    89  
    90          o := New(context.Background())
    91          {{- range $ind, $fstruct := .FieldList }}
    92          if err := o.Set{{$fstruct.Name}}(ft.{{$fstruct.Name}}); err != nil {
    93              log.Fatalf("can't set value %v to field {{$fstruct.Name}} of {{$PublicStructName}} fixture: %s", ft.{{$fstruct.Name}}, err)
    94          }
    95          {{- end }}
    96  
    97          objs = append(objs, o)
    98      }
    99  
   100      return objs
   101  }