Example #1
0
func (n *NestedData) GetI(s ...string) (int64, bool) {
	r, ok := n.Get(s...)
	if !ok {
		return 0, false
	}
	val, err := numcon.Int64(r)
	if err != nil {
		return 0, false
	}
	return val, true
}
Example #2
0
func inter(dat interface{}, s Scheme) (interface{}, error) {
	var val int64
	var err error
	switch v := dat.(type) {
	case string:
		val, err = strconv.ParseInt(v, 10, 64)
		if err != nil {
			return nil, err
		}
	default:
		val, err = numcon.Int64(v)
		if err != nil {
			return nil, err
		}
	}
	if s.Min > val {
		return nil, fmt.Errorf("Int value is too small.")
	}
	if s.Max != 0 && s.Max < val {
		return nil, fmt.Errorf("Int value is too large.")
	}
	return val, nil
}
Example #3
0
func toScheme(a interface{}) (Scheme, error) {
	empty := Scheme{}
	s := Scheme{}
	s.Specific = map[string]interface{}{}
	ai, err := numcon.Int(a)
	if err == nil && ai == 1 {
		s.Type = "string"
		return s, nil
	}
	am, ok := a.(map[string]interface{})
	if !ok {
		return empty, fmt.Errorf("Can't interpret scheme.")
	}
	s.SliceMin = -1 // 0 is not good as a default value since it can be a legal one too.
	s.SliceMax = -1
	for i, v := range am {
		switch i {
		case "must":
			s.Must, ok = v.(bool)
			if !ok {
				return empty, fmt.Errorf("Bad scheme: must should be a slice.")
			}
		case "type":
			s.Type, ok = v.(string)
			if !ok {
				return empty, fmt.Errorf("Bad scheme: type should be a string.")
			}
		case "slice":
			s.Slice, ok = v.(bool)
			if !ok {
				return empty, fmt.Errorf("Bad scheme: slice should be a bool.")
			}
		case "sliceMin":
			s.SliceMin, err = numcon.Int(v)
			if err != nil {
				return empty, fmt.Errorf("Bad scheme: sliceMin should be int compatible.")
			}
		case "sliceMax":
			s.SliceMax, err = numcon.Int(v)
			if err != nil {
				return empty, fmt.Errorf("Bad scheme: sliceMax should be int compatible.")
			}
		case "allOrNothing":
			s.AllOrNothing, ok = v.(bool)
			if !ok {
				return empty, fmt.Errorf("Bad scheme: allOrNothing should be a bool.")
			}
		case "min":
			s.Min, err = numcon.Int64(v)
			if err != nil {
				return empty, fmt.Errorf("Bad scheme: min should be int64 compatible.")
			}
		case "max":
			s.Max, err = numcon.Int64(v)
			if err != nil {
				return empty, fmt.Errorf("Bad scheme: max should be int64 compatible.")
			}
		case "regexp":
			s.Regexp, ok = v.(string)
			if !ok {
				return empty, fmt.Errorf("Bad scheme: regexp should be a string.")
			}
		case "ignore":
			s.Ignore, ok = v.(bool)
			if !ok {
				return empty, fmt.Errorf("Bad scheme: ignore should be a string.")
			}
		default:
			s.Specific[i] = v
		}
	}
	return s, nil
}