diff --git a/reflect.go b/reflect.go new file mode 100644 index 0000000..4b2d26e --- /dev/null +++ b/reflect.go @@ -0,0 +1,168 @@ +package q5 + +import ( + "reflect" +) + +func IsSimpleReflectKind(kind reflect.Kind) bool { + switch kind { + case reflect.Bool, + reflect.Int, + reflect.Int8, + reflect.Int16, + reflect.Int32, + reflect.Int64, + reflect.Uint, + reflect.Uint8, + reflect.Uint16, + reflect.Uint32, + reflect.Uint64, + reflect.Float32, + reflect.Float64, + reflect.String: + { + return true + } + default: + { + return false + } + } +} + +func StrToSimpleReflectValue(val string, kind reflect.Kind) *reflect.Value { + switch kind { + case reflect.Bool: + { + v := reflect.ValueOf(val == "1" || val == "true") + return &v + } + case reflect.Int, + reflect.Int8, + reflect.Int16, + reflect.Int32, + reflect.Int64: + { + v := reflect.ValueOf(ToInt64(val)) + return &v + } + case reflect.Uint, + reflect.Uint8, + reflect.Uint16, + reflect.Uint32, + reflect.Uint64: + { + v := reflect.ValueOf(uint64(ToInt64(val))) + return &v + } + case reflect.Float32, + reflect.Float64: + { + v := reflect.ValueOf(ToFloat64(val)) + return &v + } + case reflect.String: + { + v := reflect.ValueOf(val) + return &v + } + default: + { + return nil + } + } +} + +func StrToSimplePtrReflectValue(val string, kind reflect.Kind) *reflect.Value { + result := reflect.Value{} + switch kind { + case reflect.Bool: + { + v := val == "1" || val == "true" + result = reflect.ValueOf(&v) + return &result + } + case reflect.Int: + { + v := int(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Int8: + { + v := int8(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Int16: + { + v := int16(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Int32: + { + v := int32(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Int64: + { + v := ToInt64(val) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Uint: + { + v := uint(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Uint8: + { + v := uint8(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Uint16: + { + v := uint16(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Uint32: + { + v := uint32(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Uint64: + { + v := uint64(ToInt64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Float32: + { + v := float32(ToFloat64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.Float64: + { + v := float64(ToFloat64(val)) + result = reflect.ValueOf(&v) + return &result + } + case reflect.String: + { + v := val + result = reflect.ValueOf(&v) + return &result + } + default: + { + return nil + } + } +}