Ejemplo n.º 1
0
func (mo *Model) finalize(m map[interface{}]interface{}) error {
	// types := mo.Database.Driver.Types()
	// for _, f := range mo.Fields {
	// 	f.Type = types[f.TypeInDB]
	// }

	createTimestamp := func(name string) {
		f := &Field{}
		f.def()
		f.Model = mo
		f.Name = name + "At"
		f.NameInEncoding = util.Snakecase(f.Name)
		f.NameInDB = f.Model.Table.Name + "_" + f.NameInEncoding
		f.TypeInDB = "timestamp"
		f.Type = f.Model.Database.Driver.Type(f.TypeInDB)
		f.Primitive = util.Primitive(f.Type)
		mo.Fields = append(mo.Fields, f)
	}

	if mo.CreatedAt {
		createTimestamp("Created")
	}
	if mo.UpdatedAt {
		createTimestamp("Updated")
	}
	if mo.DeletedAt {
		createTimestamp("Deleted")
	}

	createBy := func(name string) {
		f := &Field{}
		f.def()
		f.Model = mo
		f.Name = name + "By"
		f.NameInEncoding = util.Snakecase(f.Name)
		f.NameInDB = f.Model.Table.Name + "_" + f.NameInEncoding
		f.TypeInDB = "timestamp" // get timestamp type from driver
		f.Type = f.Model.Database.Driver.Type(f.TypeInDB)
		f.Primitive = util.Primitive(f.Type)
		mo.Fields = append(mo.Fields, f)
	}

	if mo.CreatedBy {
		createBy("Created")
	}
	if mo.UpdatedBy {
		createBy("Updated")
	}
	if mo.DeletedBy {
		createBy("Deleted")
	}

	// finalize relations

	return nil
}
Ejemplo n.º 2
0
func (f *Field) parseType(m map[interface{}]interface{}) error {
	// check if type is valid
	avi, ok := m[fieldType]
	if !ok {
		return fmt.Errorf("'%s' is not defined for '%s' field", fieldType, f.Name)
	}
	av, ok := avi.(string)
	if !ok {
		return fmt.Errorf("could not parse '%s' for '%s' field", fieldType, f.Name)
	}
	f.TypeInDB = av
	f.Type = f.Model.Database.Driver.Type(f.TypeInDB)
	f.Primitive = util.Primitive(f.Type)
	return nil
}