Exemplo n.º 1
0
// GetValue returns a form value in FormValidationTarget
func (ovt *FormValidationTarget) GetValue(field string) interface{} {
	values := ovt.values[field]
	typed := ovt.fieldTypes[field]
	if values != nil {
		if len(values) > 0 {
			switch typed {
			case fieldTypeBool:
				return values[0] == "true" || values[0] == "1"
			case fieldTypeInt:
				v, _ := strconv.Atoi(values[0])
				return v
			case fieldTypeDate:
				v, e := wcg.ParseDate(values[0])
				if e != nil {
					return nil
				}
				return v
			case fieldTypeDateTime:
				v, e := wcg.ParseDateTime(values[0])
				if e != nil {
					return nil
				}
				return v
			default: // string
				return values[0]
			}
		}
	}
	return nil
}
Exemplo n.º 2
0
func convertJsonValueToProperties(k string, v interface{}) []datastore.Property {
	var propertyList []datastore.Property
	var value = reflect.ValueOf(v)

	switch value.Kind() {
	case reflect.String:
		p := datastore.Property{Name: k}
		s := v.(string)
		if strings.HasPrefix(s, "[]") {
			p.Value = []byte(strings.TrimPrefix(s, "[]"))
			p.NoIndex = true
			propertyList = append(propertyList, p)
		} else {
			if dt, err := wcg.ParseDateTime(fmt.Sprintf("%s", v)); err == nil {
				p.Value = dt
				propertyList = append(propertyList, p)
			} else if d, err := wcg.ParseDate(fmt.Sprintf("%s", v)); err == nil {
				p.Value = d
				propertyList = append(propertyList, p)
			} else {
				p.Value = s
				propertyList = append(propertyList, p)
			}
		}
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		// reach here from FixtureFromMap since it can contain non floating number.
		var vv int64
		switch v.(type) {
		case int:
			vv = int64(v.(int))
		case int8:
			vv = int64(v.(int8))
		case int16:
			vv = int64(v.(int16))
		case int32:
			vv = int64(v.(int32))
		case int64:
			vv = v.(int64)
		}
		propertyList = append(propertyList, datastore.Property{
			Name:  k,
			Value: vv,
		})
	case reflect.Float32, reflect.Float64:
		str := []byte(fmt.Sprintf("%f", v))
		if _floatRe.Match(str) {
			// should be int.
			propertyList = append(propertyList, datastore.Property{
				Name:  k,
				Value: int64(v.(float64)),
			})
		} else {
			propertyList = append(propertyList, datastore.Property{
				Name:  k,
				Value: v,
			})
		}
	case reflect.Bool:
		propertyList = append(propertyList, datastore.Property{
			Name:  k,
			Value: v,
		})
	case reflect.Map:
		for k1, v1 := range v.(map[string]interface{}) {
			if !strings.HasPrefix(k1, "_") {
				for _, val := range convertJsonValueToProperties(k1, v1) {
					propertyList = append(propertyList, datastore.Property{
						Name:     fmt.Sprintf("%s.%s", k, val.Name),
						Value:    val.Value,
						Multiple: val.Multiple,
					})
				}
			}
		}
		propertyList = append(propertyList)
	case reflect.Slice:
		for i := 0; i < value.Len(); i++ {
			propertyList = append(propertyList, datastore.Property{
				Name:     k,
				Value:    value.Index(i).Interface(),
				Multiple: true,
			})
		}
	default:
		break
	}
	return propertyList
}
Exemplo n.º 3
0
	reflect.TypeOf(time.Duration(0)): func(s string) (reflect.Value, error) {
		v, err := time.ParseDuration(s)
		return reflect.ValueOf(v), err
	},
	reflect.TypeOf(time.Now()): func(s string) (reflect.Value, error) {
		switch s {
		case _ValueMacroTimeNow:
			return reflect.ValueOf(lib.Now()), nil
		case _ValueMacroTimeToday:
			return reflect.ValueOf(lib.Today()), nil
		case "":
			return reflect.ValueOf(time.Time{}), nil
		default:
			if t, err := wcg.ParseDateTime(s); err == nil {
				return reflect.ValueOf(t), err
			} else if t, err := wcg.ParseDate(s); err == nil {
				return reflect.ValueOf(t), err
			} else {
				// as JSON representation
				var t time.Time
				err := json.Unmarshal([]byte(fmt.Sprintf("\"%s\"", s)), &t)
				return reflect.ValueOf(t), err
			}
		}
	},

	// String
	reflect.TypeOf(""): func(s string) (reflect.Value, error) {
		return reflect.ValueOf(s), nil
	},
	reflect.TypeOf([]byte{}): func(s string) (reflect.Value, error) {