Exemple #1
0
// materializeEntityType writes the data from eav_entity_type into a Go file and transforms
// Magento classes and config strings into Go functions.
// Depends on generated code from tableToStruct.
func materializeEntityType(ctx *context) {
	defer ctx.wg.Done()
	type dataContainer struct {
		ETypeData     eav.TableEntityTypeSlice
		ImportPaths   []string
		Package, Tick string
	}

	etData, err := getEntityTypeData(ctx.dbc.NewSession())
	codegen.LogFatal(err)

	tplData := &dataContainer{
		ETypeData:   etData,
		ImportPaths: getImportPaths(),
		Package:     codegen.ConfigMaterializationEntityType.Package,
		Tick:        "`",
	}

	addFM := template.FuncMap{
		"extractFuncType": codegen.ExtractFuncType,
	}

	formatted, err := codegen.GenerateCode(codegen.ConfigMaterializationEntityType.Package, tplEav, tplData, addFM)
	if err != nil {
		fmt.Printf("\n%s\n", formatted)
		codegen.LogFatal(err)
	}

	codegen.LogFatal(ioutil.WriteFile(codegen.ConfigMaterializationEntityType.OutputFile, formatted, 0600))
}
Exemple #2
0
func (g *generator) appendToFile(tpl string, data interface{}, addFM template.FuncMap) {
	formatted, err := codegen.GenerateCode(g.tts.Package, tpl, data, addFM)
	if err != nil {
		fmt.Printf("\n%s\n", formatted)
		codegen.LogFatal(err)
	}

	if _, err := g.outfile.Write(formatted); err != nil {
		codegen.LogFatal(err)
	}
	codegen.LogFatal(g.outfile.Sync()) // flush immediately to disk to prevent a race condition
}
Exemple #3
0
func main() {

	fmt.Println("TODO refactor")
	os.Exit(-1)

	gen.Init()

	// Read the CLDR zip file. Autodownloading if file not found
	r := gen.OpenCLDRCoreZip()
	defer r.Close()

	d := &cldr.Decoder{}
	d.SetDirFilter("main", "supplemental")
	d.SetSectionFilter("localeDisplayNames", "numbers")
	data, err := d.DecodeZip(r)
	codegen.LogFatal(err)

	curW := &bytes.Buffer{}
	for _, loc := range data.Locales() {

		if false == codegen.ConfigLocalization.EnabledLocale.Include(loc) {
			continue
		}

		ldml, err := data.LDML(loc)
		codegen.LogFatal(err)
		fmt.Fprintf(os.Stdout, "Generating: %s\n", loc)

		curB := curBuilder{
			w:      curW,
			locale: loc,
			data:   ldml,
		}
		curB.generate()
	}

	tplData := map[string]interface{}{
		"Package":       codegen.ConfigLocalization.Package,
		"CurrencyDicts": curW.String(),
	}

	formatted, err := codegen.GenerateCode(codegen.ConfigLocalization.Package, tplCode, tplData, nil)
	if err != nil {
		fmt.Printf("\n\n%s\n\n", formatted)
		codegen.LogFatal(err)
	}

	codegen.LogFatal(ioutil.WriteFile(codegen.ConfigLocalization.OutputFile, formatted, 0600))
}
Exemple #4
0
func generateStructures(tStruct *codegen.TableToStruct, db *sql.DB, dbrConn *dbr.Connection) {
	tplData := &dataContainer{
		Tables:  make([]map[string]interface{}, 0, 200),
		Package: tStruct.Package,
		Tick:    "`",
	}

	tables, err := codegen.GetTables(db, codegen.ReplaceTablePrefix(tStruct.SQLQuery))
	codegen.LogFatal(err)

	if len(tStruct.EntityTypeCodes) > 0 && tStruct.EntityTypeCodes[0] != "" {
		tplData.TypeCodeValueTables, err = codegen.GetEavValueTables(dbrConn, tStruct.EntityTypeCodes)
		codegen.LogFatal(err)

		for _, vTables := range tplData.TypeCodeValueTables {
			for t := range vTables {
				if false == isDuplicate(tables, t) {
					tables = append(tables, t)
				}
			}
		}
	}

	for _, table := range tables {

		columns, err := codegen.GetColumns(db, table)
		codegen.LogFatal(err)
		codegen.LogFatal(columns.MapSQLToGoDBRType())
		var name = table
		if mappedName, ok := codegen.TableMapMagento1To2[strings.Replace(table, codegen.TablePrefix, "", 1)]; ok {
			name = mappedName
		}
		tplData.Tables = append(tplData.Tables, map[string]interface{}{
			"name":    name,
			"table":   table,
			"columns": columns,
		})
	}

	formatted, err := codegen.GenerateCode(tStruct.Package, tplCode, tplData, nil)
	if err != nil {
		fmt.Printf("\n%s\n", formatted)
		codegen.LogFatal(err)
	}

	codegen.LogFatal(ioutil.WriteFile(tStruct.OutputFile, formatted, 0600))
}
Exemple #5
0
func attrCollection(ctx *context, data map[string]interface{}) ([]byte, error) {

	aws := getAttributeValuesForWebsites(ctx)

	fmt.Printf("\n%s : %#v\n\n", ctx.et.EntityTypeCode, aws)

	/*
		1. codegen: tplAttrWebsiteEavAttribute
			Need: values from eav_attribute and check from website table of an entity
		2. codegen: tplAttrWebsiteEntityAttribute and use the code from 1 to embed
		3. codegen: tplAttrCollection
	*/

	funcMap := template.FuncMap{
		// isEavAttr checks if the attribute/column name belongs to table eav_attribute
		"isEavAttr": func(a string) bool { return codegen.EAVAttributeCoreColumns.Include(a) },
		// isEavEntityAttr checks if the attribute/column belongs to (customer|catalog|etc)_eav_attribute
		"isEavEntityAttr": func(a string) bool {
			if et, ok := codegen.ConfigEntityType[ctx.et.EntityTypeCode]; ok {
				return false == codegen.EAVAttributeCoreColumns.Include(a) && et.AttributeCoreColumns.Include(a)
			}
			return false
		},
		"isUnknownAttr": func(a string) bool {
			if et, ok := codegen.ConfigEntityType[ctx.et.EntityTypeCode]; ok {
				return false == codegen.EAVAttributeCoreColumns.Include(a) && false == et.AttributeCoreColumns.Include(a)
			}
			return false
		},
		"setAttrIdx": func(value, constName string) string {
			return strings.Replace(value, "{{.AttributeIndex}}", constName, -1)
		},
		"printWebsiteEavAttribute": func(attrID string) string {
			if _, ok := aws[attrID]; ok {
				//				fmt.Printf("\n%#v\n\n", cols)
				return "/* found " + attrID + " */ nil"
			}
			return "nil"
		},
	}

	return codegen.GenerateCode("", tplAttrCollection, data, funcMap)
}
Exemple #6
0
func attrImport(ctx *context, data map[string]interface{}) ([]byte, error) {
	return codegen.GenerateCode("", tplAttrImport, data, nil)
}