github.com/mailru/activerecord@v1.12.2/internal/pkg/generator/tmpl/tarantool/fixturestore.tmpl (about) 1 package {{ .FixturePkg }} 2 3 {{ $fields := .FieldList }} 4 5 {{ if $fields }} 6 7 import ( 8 _ "embed" 9 "context" 10 "fmt" 11 "log" 12 "sync" 13 14 "gopkg.in/yaml.v3" 15 16 "github.com/mailru/activerecord/pkg/activerecord" 17 18 {{- range $ind, $imp := .Imports }} 19 {{ if ne $imp.ImportName "" }}{{- $imp.ImportName }} {{ end }}"{{ $imp.Path }}" 20 {{- end }} 21 ) 22 23 {{ $serializers := .Serializers -}} 24 {{ $PackageName := .ARPkg -}} 25 {{ $PublicStructName := .ARPkgTitle -}} 26 {{ $typePK := "" -}} 27 {{ $fieldNamePK := "" -}} 28 29 {{ range $num, $ind := .Indexes -}} 30 {{ $lenfld := len $ind.Fields -}} 31 {{ if $ind.Primary }} 32 {{ if ne $lenfld 1 }} 33 {{ $typePK = print $PackageName "." $ind.Type }} 34 {{ else }} 35 {{- $typePK = $ind.Type -}} 36 {{ end }} 37 {{- $fieldNamePK = $ind.Name -}} 38 {{ end }} 39 {{ end }} 40 41 var {{$PackageName}}Once sync.Once 42 var {{$PackageName}}Store map[{{$typePK}}]int 43 var {{$PackageName}}Fixtures []*{{$PackageName}}.{{$PublicStructName}} 44 45 //go:embed data/{{$PackageName}}.yaml 46 var {{$PackageName}}Source []byte 47 48 func init{{$PublicStructName}}() { 49 {{$PackageName}}Once.Do(func() { 50 {{$PackageName}}Fixtures = {{$PackageName}}.UnmarshalFromYaml({{$PackageName}}Source) 51 52 {{$PackageName}}Store = map[{{$typePK}}]int{} 53 for i, f := range {{$PackageName}}Fixtures { 54 if _, ok := {{$PackageName}}Store[f.Primary()]; ok { 55 log.Fatalf("{{$PackageName}} fixture with {{$fieldNamePK}} %v is duplicated", f.Primary()) 56 } 57 58 {{$PackageName}}Store[f.Primary()] = i 59 } 60 }) 61 } 62 63 64 func Get{{$PublicStructName}}By{{$fieldNamePK}}({{$fieldNamePK}} {{$typePK}}) *{{$PackageName}}.{{$PublicStructName}} { 65 init{{$PublicStructName}}() 66 67 idx, ex := {{$PackageName}}Store[{{$fieldNamePK}}] 68 if !ex { 69 log.Fatalf("{{$PublicStructName}} fixture with {{$fieldNamePK}} %v not found", {{$fieldNamePK}}) 70 } 71 72 res := {{$PackageName}}Fixtures[idx] 73 74 return res 75 } 76 77 func {{$PublicStructName}}StoreIterator() func(it func(any) error) error { 78 return func(it func(e any) error) error { 79 init{{$PublicStructName}}() 80 81 for _, e := range {{$PackageName}}Fixtures { 82 if err := it(e); err != nil { 83 return err 84 } 85 } 86 87 return nil 88 } 89 } 90 91 {{ end }}