Ejemplo n.º 1
0
func (g *generator) runTable() {
	defer log.WhenDone().Info("Stats", "Package", g.tts.Package, "Step", "RunTable")
	type OneTable struct {
		Package          string
		Tick             string
		Name             string
		TableName        string
		Struct           string
		Slice            string
		Table            string
		GoColumns        codegen.Columns
		Columns          csdb.Columns
		MethodRecvPrefix string
		FindByPk         string
	}

	for _, table := range g.tables {

		columns, err := codegen.GetColumns(g.dbrConn.DB, table)
		codegen.LogFatal(err)
		codegen.LogFatal(columns.MapSQLToGoDBRType())

		name := g.getConsistentName(table)
		data := OneTable{
			Package:   g.tts.Package,
			Tick:      "`",
			Name:      name,
			TableName: g.getMagento2TableName(table), // original table name!
			Struct:    TypePrefix + name,             // getTableConstantName
			Slice:     TypePrefix + name + "Slice",   // getTableConstantName
			Table:     table,
			GoColumns: columns,
			Columns:   columns.CopyToCSDB(),
		}

		if data.Columns.PrimaryKeys().Len() > 0 {
			data.FindByPk = "FindBy" + utils.UnderscoreCamelize(data.Columns.PrimaryKeys().JoinFields("_"))
		}

		tplFuncs := template.FuncMap{
			"typePrefix": func(name string) string {
				// if the method already exists in package then add the prefix parent
				// to avoid duplicate function names.
				search := data.Slice + name
				if g.existingMethodSets.has(search) {
					return MethodRecvPrefix + name
				}
				return name
			},
			"findBy":  findBy,
			"dbrType": dbrType,
		}

		g.appendToFile(g.getGenericTemplate(table), data, tplFuncs)
	}
}
Ejemplo n.º 2
0
// prepareVar converts a string into a Go code variable. Removes the package name if this string
// starts with the package name. Replaces all illegal characters with an underscore.
func prepareVar(pkg string) func(s string) string {

	return func(str string) string {

		l := len(pkg) + 1
		if len(str) > l && str[:l] == pkg+TableNameSeparator {
			str = str[l:]
		}

		return utils.UnderscoreCamelize(str)
	}
}
Ejemplo n.º 3
0
func TestCamelize(t *testing.T) {
	tests := []struct {
		actual, expected string
	}{
		{"hello", "Hello"},
		{"hello_gopher", "HelloGopher"},
		{"hello_gopher_", "HelloGopher"},
		{"hello_gopher_id", "HelloGopherID"},
		{"hello_gopher_idx", "HelloGopherIDX"},
		{"idx_id", "IDXID"},
		{"idx_eav_id", "IDXEAVID"},
		{"idxeav_id", "IdxeavID"},
		{"idxeav_cs", "IdxeavCS"},
		{"idx_eav_cs", "IDXEAVCS"},
		{"idx_eav_cs_url", "IDXEAVCSURL"},
		{"hello_eav_idx_cs", "HelloEAVIDXCS"},
		{"hello_idx_Tmp_cs", "HelloIDXTMPCS"},
	}
	for _, test := range tests {
		assert.Equal(t, test.expected, utils.UnderscoreCamelize(test.actual))
	}
}
Ejemplo n.º 4
0
// findBy is a template function used in runTable()
func findBy(s string) string {
	return "FindBy" + utils.UnderscoreCamelize(s)
}
Ejemplo n.º 5
0
// BenchmarkCamelize-4                 	  500000	      2906 ns/op	     368 B/op	      15 allocs/op
func BenchmarkCamelize(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		benchCases = utils.UnderscoreCamelize("catalog_____product_entity_")
	}
}
Ejemplo n.º 6
0
func (c column) updateGoPrimitive(useSQL bool) column {
	c.GoName = utils.UnderscoreCamelize(c.Field.String)
	c.GoType = c.GetGoPrimitive(useSQL)
	return c
}