Exemplo n.º 1
0
Arquivo: entity.go Projeto: klenin/orc
func (this *Entity) LoadWherePart(data map[string]interface{}) *Entity {
	if data == nil || len(data) == 0 {
		return this
	}

	rv := reflect.ValueOf(this.wherePart)
	rt := reflect.ValueOf(this.fields).Type()

	for key, val := range data {
		for i := 0; i < rt.Elem().NumField(); i++ {

			if rt.Elem().Field(i).Tag.Get("name") != key {
				continue
			}

			if val != nil && reflect.TypeOf(val).Name() == "" { // hope that v is array of interfaces
				rv.Interface().(map[string]interface{})[key] = make([]interface{}, 0)
				arr := make([]interface{}, 0)
				for _, vv := range val.([]interface{}) {
					v_ := utils.CheckTypeValue(rt.Elem().Field(i).Tag.Get("type"), vv)
					if v_ == nil {
						continue
					}
					arr = append(arr, v_)
				}
				rv.Interface().(map[string]interface{})[key] = arr
				continue
			}
			rv.Interface().(map[string]interface{})[key] = utils.CheckTypeValue(rt.Elem().Field(i).Tag.Get("type"), val)
		}
	}

	return this
}
Exemplo n.º 2
0
Arquivo: entity.go Projeto: klenin/orc
func (this *Entity) LoadModelData(data map[string]interface{}) *Entity {
	refOfValue := reflect.ValueOf(this.fields)
	refOfType := refOfValue.Type()
	for key, val := range data {
		for i := 0; i < refOfType.Elem().NumField(); i++ {
			tag := refOfType.Elem().Field(i).Tag.Get("name")
			if tag == key {
				method := "Set" + strings.ToUpper(string(refOfType.Elem().Field(i).Name[0])) + string(refOfType.Elem().Field(i).Name[1:])
				refMethod := refOfValue.MethodByName(method)
				log.Println("method: ", method)
				if !refMethod.IsValid() {
					log.Println("Method is not exists!")
					continue
				}
				value := utils.CheckTypeValue(refOfType.Elem().Field(i).Tag.Get("type"), val)
				if value == nil {
					continue
				}
				refOfValue.MethodByName(method).Call([]reflect.Value{reflect.ValueOf(value)})
			}
		}
	}

	return this
}