github.com/mailru/activerecord@v1.12.2/internal/pkg/generator/fixture.go (about) 1 package generator 2 3 import ( 4 "bufio" 5 "bytes" 6 _ "embed" 7 8 "github.com/mailru/activerecord/internal/pkg/arerror" 9 "github.com/mailru/activerecord/internal/pkg/ds" 10 "golang.org/x/tools/imports" 11 ) 12 13 type FixturePkgData struct { 14 FixturePkg string 15 ARPkg string 16 ARPkgTitle string 17 FieldList []ds.FieldDeclaration 18 FieldMap map[string]int 19 FieldObject map[string]ds.FieldObject 20 ProcInFieldList []ds.ProcFieldDeclaration 21 ProcOutFieldList []ds.ProcFieldDeclaration 22 Container ds.NamespaceDeclaration 23 Indexes []ds.IndexDeclaration 24 Serializers map[string]ds.SerializerDeclaration 25 Mutators map[string]ds.MutatorDeclaration 26 Imports []ds.ImportDeclaration 27 AppInfo string 28 } 29 30 type FixtureMetaData struct { 31 MetaData 32 FixturePkg string 33 } 34 35 //nolint:revive 36 //go:embed tmpl/fixture_meta.tmpl 37 var fixtureMetaTmpl string 38 39 func generateFixtureMeta(params FixtureMetaData) (*bytes.Buffer, *arerror.ErrGeneratorFile) { 40 metaWriter := new(bytes.Buffer) 41 metaFile := bufio.NewWriter(metaWriter) 42 43 if err := GenerateByTmpl(metaFile, params, "fixture_meta", fixtureMetaTmpl); err != nil { 44 return nil, &arerror.ErrGeneratorFile{Name: "repository.go", Backend: "fixture_meta", Filename: "repository.go", Err: err} 45 } 46 47 metaFile.Flush() 48 49 return metaWriter, nil 50 } 51 52 func GenerateFixtureMeta(packageNamespaces map[string][]*ds.RecordPackage, appInfo, pkgFixture string) ([]GenerateFile, error) { 53 var ret = make([]GenerateFile, 0, len(packageNamespaces)) 54 55 for backend, namespaces := range packageNamespaces { 56 metaData := FixtureMetaData{ 57 MetaData: MetaData{ 58 Namespaces: namespaces, 59 AppInfo: appInfo, 60 }, 61 FixturePkg: pkgFixture, 62 } 63 var generated *bytes.Buffer 64 65 switch backend { 66 case "tarantool15": 67 fallthrough 68 case "octopus": 69 fallthrough 70 case "tarantool16": 71 fallthrough 72 case "tarantool2": 73 var err *arerror.ErrGeneratorFile 74 75 generated, err = generateFixtureMeta(metaData) 76 if err != nil { 77 err.Name = "fixture_meta" 78 return nil, err 79 } 80 case "postgres": 81 return nil, &arerror.ErrGeneratorFile{Name: "fixture_meta", Backend: backend, Err: arerror.ErrGeneratorBackendNotImplemented} 82 default: 83 return nil, &arerror.ErrGeneratorFile{Name: "fixture_meta", Backend: backend, Err: arerror.ErrGeneratorBackendUnknown} 84 } 85 86 genRes := GenerateFile{ 87 Dir: pkgFixture, 88 Name: "stores.go", 89 Backend: "fixture_meta", 90 } 91 92 genData := generated.Bytes() 93 94 var err error 95 96 genRes.Data, err = imports.Process("", genData, nil) 97 if err != nil { 98 return nil, &arerror.ErrGeneratorFile{Name: "repository.go", Backend: "fixture_meta", Filename: genRes.Name, Err: ErrorLine(err, string(genData))} 99 } 100 101 ret = append(ret, genRes) 102 } 103 104 return ret, nil 105 }