Beispiel #1
0
// Type sets the expected reflect.Kind type an option will accept.
func (f *Option) Type(kind reflect.Kind) *Option {
	invalidKinds := []reflect.Kind{
		reflect.Uintptr,
		reflect.Complex64,
		reflect.Complex128,
		reflect.Array,
		reflect.Chan,
		reflect.Func,
		reflect.Interface,
		reflect.Map,
		reflect.Ptr,
		reflect.Slice,
		reflect.Struct,
		reflect.UnsafePointer,
	}

	for _, bad := range invalidKinds {
		if kind == bad {
			panic(fmt.Sprintf("Cannot use kind: '%s' as a valid type", kind.String()))
		}
	}

	f.ExpectedType = kind
	return f
}
Beispiel #2
0
// KindOf asserts that the matched value of the kind.
func (m *Matcher) KindOf(kind reflect.Kind) *Matcher {
	rv := reflect.ValueOf(m.value)
	if rv.Kind() != kind {
		m.t.Errorf("expected kind %s, got %s", kind.String(), rv.Kind().String())
	}
	return m
}
Beispiel #3
0
func cannotConvert(L *lua.State, idx int, msg string, kind reflect.Kind, t reflect.Type) {
	types := ""
	if t != nil {
		types = t.String()
	} else {
		types = kind.String()
	}
	RaiseError(L, sprintf("arg #%d cannot convert %s to %s", idx, msg, types))
}
func (this *typeScriptClassBuilder) AddSimpleArrayField(fieldName, fieldType string, kind reflect.Kind) error {
	if typeScriptType, ok := this.types[kind]; ok {
		if len(fieldName) > 0 {
			this.fields += fmt.Sprintf("%s%s: %s[];\n", this.indent, fieldName, typeScriptType)
			this.createFromMethodBody += fmt.Sprintf("%s%sresult.%s = source[\"%s\"];\n", this.indent, this.indent, fieldName, fieldName)
			return nil
		}
	}
	return errors.New(fmt.Sprintf("Cannot find type for %s (%s/%s)", kind.String(), fieldName, fieldType))
}
Beispiel #5
0
// convert string to reflect.Value acoring to kind( use base 0 to parse numeric )
func Atok(str string, k reflect.Kind) (output reflect.Value, err error) {
	switch k {
	case reflect.Bool:
		b, e := strconv.ParseBool(str)
		return reflect.ValueOf(b), e
	case reflect.Int:
		i, e := strconv.Atoi(str)
		return reflect.ValueOf(i), e
	case reflect.Int8:
		i, e := strconv.ParseInt(str, 0, 8)
		return reflect.ValueOf(int8(i)), e
	case reflect.Int16:
		i, e := strconv.ParseInt(str, 0, 16)
		return reflect.ValueOf(int16(i)), e
	case reflect.Int32:
		i, e := strconv.ParseInt(str, 0, 32)
		return reflect.ValueOf(int32(i)), e
	case reflect.Int64:
		i, e := strconv.ParseInt(str, 0, 64)
		return reflect.ValueOf(i), e
	case reflect.Uint:
		i, e := strconv.ParseUint(str, 0, 0)
		return reflect.ValueOf(uint(i)), e
	case reflect.Uint8:
		i, err := strconv.ParseUint(str, 0, 8)
		return reflect.ValueOf(uint8(i)), err
	case reflect.Uint16:
		i, err := strconv.ParseUint(str, 0, 16)
		return reflect.ValueOf(uint16(i)), err
	case reflect.Uint32:
		i, err := strconv.ParseUint(str, 0, 32)
		return reflect.ValueOf(uint32(i)), err
	case reflect.Uint64:
		i, err := strconv.ParseUint(str, 0, 64)
		return reflect.ValueOf(i), err
	case reflect.Float32:
		i, err := strconv.ParseFloat(str, 32)
		return reflect.ValueOf(float32(i)), err
	case reflect.Float64:
		i, err := strconv.ParseFloat(str, 64)
		return reflect.ValueOf(i), err
	case reflect.String:
		return reflect.ValueOf(str), nil
	default:
	}
	err = newTypeErr(methodName(), "can not convert string to "+k.String(), nil)
	return
}
Beispiel #6
0
func getSetting(config map[string]interface{}, setting string, kind reflect.Kind,
	mandatory bool, fallbackValue interface{}) (interface{}, error) {
	if _, ok := config[setting]; !ok {
		if mandatory {
			return fallbackValue, fmt.Errorf("missing `%s' mandatory setting", setting)
		}

		return fallbackValue, nil
	}

	if reflect.ValueOf(config[setting]).Kind() != kind {
		return fallbackValue, fmt.Errorf("setting `%s' value should be a %s", setting, kind.String())
	}

	return config[setting], nil
}
Beispiel #7
0
func (t *Type) setPrim(k reflect.Kind) {
	t.Kind = k
	if t.Name == "" {
		t.Name = k.String()
	}
}
Beispiel #8
0
func parseAs(str string, kind reflect.Kind, dst reflect.Value) error {
	// Handle strings
	if kind == reflect.String {
		dst.SetString(str)
		return nil
	}

	// Handle booleans
	if kind == reflect.Bool {
		if strings.ToLower(str) == "true" ||
			strings.ToLower(str) == "yes" ||
			str == "1" {
			dst.SetBool(true)
			return nil
		}
		if strings.ToLower(str) == "false" ||
			strings.ToLower(str) == "no" ||
			str == "0" {
			dst.SetBool(false)
			return nil
		}
		return fmt.Errorf("String '%s' not recognized as a boolean value", str)
	}

	// Handle integers
	if kind >= reflect.Int && kind <= reflect.Int64 {
		size := 0
		if kind == reflect.Int8 {
			size = 8
		}
		if kind == reflect.Int16 {
			size = 16
		}
		if kind == reflect.Int32 {
			size = 32
		}
		if kind == reflect.Int64 {
			size = 64
		}
		i, err := strconv.ParseInt(str, 10, size)
		if err != nil {
			return fmt.Errorf("Error parsing '%s' as an integer: %v", str, err)
		}
		dst.SetInt(i)
		return nil
	}

	// Handle unsigned integers
	if kind >= reflect.Uint && kind <= reflect.Uint64 {
		size := 0
		if kind == reflect.Uint8 {
			size = 8
		}
		if kind == reflect.Uint16 {
			size = 16
		}
		if kind == reflect.Uint32 {
			size = 32
		}
		if kind == reflect.Uint64 {
			size = 64
		}
		i, err := strconv.ParseUint(str, 10, size)
		if err != nil {
			return fmt.Errorf("Error parsing '%s' as an unsigned integer: %v",
				str, err)
		}
		dst.SetUint(i)
		return nil
	}

	// Handle floats
	if kind >= reflect.Float32 || kind <= reflect.Float64 {
		size := 32
		if kind == reflect.Float64 {
			size = 64
		}
		i, err := strconv.ParseFloat(str, size)
		if err != nil {
			return fmt.Errorf("Error parsing '%s' as a float: %v", str, err)
		}
		dst.SetFloat(i)
		return nil
	}

	return fmt.Errorf("Unable to convert strings to %s", kind.String())
}