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

     1  package generator
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	_ "embed"
     7  	"log"
     8  	"strings"
     9  	"text/template"
    10  
    11  	"github.com/mailru/activerecord/internal/pkg/arerror"
    12  	"github.com/mailru/activerecord/internal/pkg/ds"
    13  	"github.com/mailru/activerecord/pkg/iproto/util/text"
    14  	"github.com/mailru/activerecord/pkg/octopus"
    15  )
    16  
    17  //nolint:revive
    18  //go:embed tmpl/tarantool/main.tmpl
    19  var tarantoolRootRepositoryTmpl string
    20  
    21  //nolint:revive
    22  //go:embed tmpl/tarantool/procedure.tmpl
    23  var tarantoolProcRepositoryTmpl string
    24  
    25  //nolint:revive
    26  //go:embed tmpl/tarantool/fixture.tmpl
    27  var tarantoolFixtureRepositoryTmpl string
    28  
    29  func GenerateTarantool(params PkgData) (map[string]bytes.Buffer, *arerror.ErrGeneratorPhases) {
    30  	ret := map[string]bytes.Buffer{}
    31  
    32  	mainWriter := bytes.Buffer{}
    33  
    34  	var repositoryTmpl string
    35  	if len(params.FieldList) > 0 {
    36  		repositoryTmpl = tarantoolRootRepositoryTmpl
    37  	} else if len(params.ProcOutFieldList) > 0 {
    38  		repositoryTmpl = tarantoolProcRepositoryTmpl
    39  	} else {
    40  		return nil, &arerror.ErrGeneratorPhases{Backend: "tarantool", Err: arerror.ErrGeneratorTemplateUnkhown}
    41  	}
    42  
    43  	octopusFile := bufio.NewWriter(&mainWriter)
    44  
    45  	err := GenerateByTmpl(octopusFile, params, "tarantool", repositoryTmpl, Tarantool2TmplFunc)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	octopusFile.Flush()
    51  
    52  	ret["tarantool"] = mainWriter
    53  
    54  	if len(params.FieldList) > 0 {
    55  		fixtureWriter := bytes.Buffer{}
    56  
    57  		fixtureFile := bufio.NewWriter(&fixtureWriter)
    58  
    59  		err = GenerateByTmpl(fixtureFile, params, "tarantool", tarantoolFixtureRepositoryTmpl, Tarantool2TmplFunc)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  
    64  		fixtureFile.Flush()
    65  
    66  		ret["fixture"] = fixtureWriter
    67  	}
    68  
    69  	return ret, nil
    70  }
    71  
    72  //go:embed tmpl/tarantool/fixturestore.tmpl
    73  var tarantoolFixtureStoreTmpl string
    74  
    75  func GenerateTarantoolFixtureStore(params FixturePkgData) (map[string]bytes.Buffer, *arerror.ErrGeneratorPhases) {
    76  	fixtureWriter := bytes.Buffer{}
    77  
    78  	file := bufio.NewWriter(&fixtureWriter)
    79  
    80  	templatePackage, err := template.New(TemplateName).Funcs(funcs).Parse(disclaimer + tarantoolFixtureStoreTmpl)
    81  	if err != nil {
    82  		tmplLines, errgetline := getTmplErrorLine(strings.SplitAfter(disclaimer+tarantoolFixtureStoreTmpl, "\n"), err.Error())
    83  		if errgetline != nil {
    84  			tmplLines = errgetline.Error()
    85  		}
    86  
    87  		return nil, &arerror.ErrGeneratorPhases{Backend: "fixture", Phase: "parse", TmplLines: tmplLines, Err: err}
    88  	}
    89  
    90  	err = templatePackage.Execute(file, params)
    91  	if err != nil {
    92  		tmplLines, errgetline := getTmplErrorLine(strings.SplitAfter(disclaimer+tarantoolFixtureStoreTmpl, "\n"), err.Error())
    93  		if errgetline != nil {
    94  			tmplLines = errgetline.Error()
    95  		}
    96  
    97  		return nil, &arerror.ErrGeneratorPhases{Backend: "fixture", Phase: "execute", TmplLines: tmplLines, Err: err}
    98  	}
    99  
   100  	file.Flush()
   101  
   102  	ret := map[string]bytes.Buffer{
   103  		"fixture": fixtureWriter,
   104  	}
   105  
   106  	return ret, nil
   107  }
   108  
   109  var Tarantool2TmplFunc = template.FuncMap{
   110  	"packerParam": func(format octopus.Format) TarantoolFormatParam {
   111  		ret, ex := PrimitiveTypeFormatConverter[format]
   112  		if !ex {
   113  			log.Fatalf("packer for type `%s` not found", format)
   114  		}
   115  
   116  		return ret
   117  	},
   118  	"addImport": func(flds []ds.FieldDeclaration) (imports []string) {
   119  		var needStrconv bool
   120  
   121  		for _, fld := range flds {
   122  			if fld.PrimaryKey && fld.Format != "string" {
   123  				needStrconv = true
   124  			}
   125  		}
   126  
   127  		if needStrconv {
   128  			imports = append(imports, "strconv")
   129  		}
   130  
   131  		return
   132  	},
   133  	"trimPrefix":       strings.TrimPrefix,
   134  	"hasPrefix":        strings.HasPrefix,
   135  	"lowerCase":        strings.ToLower,
   136  	"snakeToCamelCase": text.SnakeToCamelCase,
   137  }
   138  
   139  type TarantoolFormatParam string
   140  
   141  func (p TarantoolFormatParam) ToString() []string {
   142  	return strings.SplitN(string(p), `%%`, 2)
   143  }
   144  
   145  var PrimitiveTypeFormatConverter = map[octopus.Format]TarantoolFormatParam{
   146  	octopus.Bool:    "strconv.FormatBool(%%)",
   147  	octopus.Uint8:   "strconv.FormatUint(uint64(%%), 10)",
   148  	octopus.Uint16:  "strconv.FormatUint(uint64(%%), 10)",
   149  	octopus.Uint32:  "strconv.FormatUint(uint64(%%), 10)",
   150  	octopus.Uint64:  "strconv.FormatUint(%%, 10)",
   151  	octopus.Uint:    "strconv.FormatUint(uint64(%%), 10)",
   152  	octopus.Int8:    "strconv.FormatInt(int64(%%), 10)",
   153  	octopus.Int16:   "strconv.FormatInt(int64(%%), 10)",
   154  	octopus.Int32:   "strconv.FormatInt(int64(%%), 10)",
   155  	octopus.Int64:   "strconv.FormatInt(%%, 10)",
   156  	octopus.Int:     "strconv.FormatInt(int64(%%), 10)",
   157  	octopus.Float32: "strconv.FormatFloat(%%, 32)",
   158  	octopus.Float64: "strconv.FormatFloat(%%, 64)",
   159  	octopus.String:  "%%",
   160  }