Exemple #1
0
// Projects
func (self *Database) Create(colName string, item interface{}) error {
	/*s := structs.New(item)
	colName := ""
	for _, f := range s.Fields() {
		colName = f.Tag("col")
		if colName != "" {
			break
		}

	}

	if colName == "" {
		return errors.New("col name")
	}*/

	col := self.db.Use(colName)
	m := structs.Map(item)
	i, e := col.Insert(m)
	if e == nil {
		for _, field := range structs.Fields(item) {
			if field.Name() == "Id" || field.Name() == "ID" {
				field.Set(fmt.Sprintf("%d", i))
			}
		}
	}
	return e
}
Exemple #2
0
func (self *Resource) ToMessage() *messages.Resource {
	m := structs.Map(self)
	m["Id"] = self.Id.Hex()
	m["ProjectId"] = self.ProjectId.Hex()
	b, _ := json.Marshal(self.Fields)

	delete(m, "Fields")

	m["Fields"] = b
	//m["Type"] = self.Type.String()

	var out messages.Resource
	mapstructure.Decode(m, &out)

	return &out
}
Exemple #3
0
func NewResourceFromMsg(resource *messages.Resource) (*Resource, error) {

	m := structs.Map(resource)

	fields := m["Fields"]
	fmt.Printf("fields %v", fields)

	delete(m, "Fields")

	var out Resource
	err := mapstructure.Decode(m, &out)

	if err != nil {
		return nil, err
	}
	return &out, nil

}
Exemple #4
0
func ToMessage(args interface{}) Message {
	return structs.Map(args)
}