Exemplo n.º 1
0
/*
The RailsConfig tries to replicate Rails practices for
naming.

Table names are pluralized lowercase versions of struct
names and field names are lowercased versions of field names.
Foreign key names are the lowercased name of field with '_id'
appended.

Ids are identified with the field name "Id" and the database
column "id", while the timestamp fields are "CreatedAt" and
"UpdatedAt".
*/
func NewRailsConfig() *Config {
	c := new(Config)
	c.StructToTable = pluralizeStruct
	c.FieldToColumn = func(s, f string) string {
		return inflections.Underscore(f)
	}
	c.ForeignKeyName = func(fn, sn string) string {
		return strings.ToLower(fn) + "_id"
	}
	c.IdName = func(s string) (string, string) {
		return "Id", "id"
	}
	c.CreatedColumn = "CreatedAt"
	c.UpdatedColumn = "UpdatedAt"

	return c
}
Exemplo n.º 2
0
func run(context *cli.Context) {
	debug("run")
	name := getOpts(context)

	vars := templateVars{
		ProjectName: name,
		PROJECTNAME: strings.ToUpper(inflections.Underscore(name)),
	}

	for _, assetName := range AssetNames() {
		asset := parseTemplate(vars, assetName)

		dir := filepath.Dir(assetName)
		os.MkdirAll(dir, 0755)
		ioutil.WriteFile(assetName, []byte(asset), 0644)
	}

	os.Chmod("build.sh", 0755)
	fmt.Println("To restore dependencies, run: \n\ngvt restore")
}
Exemplo n.º 3
0
func Load(tree *parse.Node) *Table {
	table := new(Table)

	// local map of indexes, used for quick
	// lookups and de-duping.
	indexs := map[string]*Index{}

	// pluralizes the table name and then
	// formats in snake case.
	table.Name = inflections.Underscore(tree.Type)
	table.Name = inflections.Pluralize(table.Name)

	// each edge node in the tree is a column
	// in the table. Convert each edge node to
	// a Field structure.
	for _, node := range tree.Edges() {

		field := new(Field)

		// Lookup the SQL column type
		// TODO: move this to a function
		t, ok := parse.Types[node.Type]
		if ok {
			tt, ok := types[t]
			if !ok {
				tt = BLOB
			}
			field.Type = tt
		} else {
			field.Type = BLOB
		}

		// substitute tag variables
		if node.Tags != nil {

			if node.Tags.Skip {
				continue
			}

			// default ID and int64 to primary key
			// with auto-increment
			if node.Name == "ID" && node.Kind == parse.Int64 {
				node.Tags.Primary = true
				node.Tags.Auto = true
			}

			field.Auto = node.Tags.Auto
			field.Primary = node.Tags.Primary
			field.Size = node.Tags.Size

			if node.Tags.Primary {
				table.Primary = append(table.Primary, field)
			}

			if node.Tags.Index != "" {
				index, ok := indexs[node.Tags.Index]
				if !ok {
					index = new(Index)
					index.Name = node.Tags.Index
					indexs[index.Name] = index
					table.Index = append(table.Index, index)
				}
				index.Fields = append(index.Fields, field)
			}

			if node.Tags.Unique != "" {
				index, ok := indexs[node.Tags.Index]
				if !ok {
					index = new(Index)
					index.Name = node.Tags.Unique
					index.Unique = true
					indexs[index.Name] = index
					table.Index = append(table.Index, index)
				}
				index.Fields = append(index.Fields, field)
			}

			if node.Tags.Type != "" {
				t, ok := sqlTypes[node.Tags.Type]
				if ok {
					field.Type = t
				}
			}
		}

		// get the full path name
		path := node.Path()
		var parts []string
		for _, part := range path {
			if part.Tags != nil && part.Tags.Name != "" {
				parts = append(parts, part.Tags.Name)
				continue
			}

			parts = append(parts, part.Name)
		}
		field.Name = strings.Join(parts, "_")
		field.Name = inflections.Underscore(field.Name)

		table.Fields = append(table.Fields, field)
	}

	return table
}