Compare commits
4 Commits
revert-595
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
a7cbbad515 | ||
|
b4a189f69c | ||
|
d6dfb1fdf7 | ||
|
86193d253b |
|
@ -24,11 +24,6 @@
|
|||
# go-tests = true
|
||||
# unused-packages = true
|
||||
|
||||
|
||||
[[constraint]]
|
||||
branch = "master"
|
||||
name = "github.com/corpix/reflect"
|
||||
|
||||
[prune]
|
||||
go-tests = true
|
||||
unused-packages = true
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"fmt"
|
||||
"reflect"
|
||||
|
||||
cr "github.com/corpix/reflect"
|
||||
our "git.loafle.net/overflow/util-go/reflect"
|
||||
)
|
||||
|
||||
// SetValueWithJSONStringArray set the value of json string array
|
||||
|
@ -41,7 +41,7 @@ func SetValueWithJSONStringArray(values []string, targets []interface{}) error {
|
|||
case reflect.Ptr:
|
||||
return fmt.Errorf("Type of target[%d] cannot be double ptr, value=%s", indexI, value)
|
||||
default:
|
||||
cv, err := cr.ConvertToType(value, reflect.TypeOf(target).Elem())
|
||||
cv, err := our.ConvertToType(value, reflect.TypeOf(target).Elem())
|
||||
if nil != err {
|
||||
return fmt.Errorf("Type conversion of value[%s] has been failed to %s[%d]", value, reflect.TypeOf(target).Elem().Kind(), indexI)
|
||||
}
|
||||
|
|
1156
reflect/convert.go
Normal file
1156
reflect/convert.go
Normal file
File diff suppressed because it is too large
Load Diff
40
reflect/error.go
Normal file
40
reflect/error.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package reflect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ErrCanNotConvertType struct {
|
||||
value interface{}
|
||||
from Type
|
||||
to Type
|
||||
reason []string
|
||||
}
|
||||
|
||||
func (e ErrCanNotConvertType) Error() string {
|
||||
var (
|
||||
reason = strings.Join(e.reason, ", ")
|
||||
)
|
||||
|
||||
if reason != "" {
|
||||
reason = ", reason: " + reason
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"Can not convert '%#v' of type '%s' to '%s'%s",
|
||||
e.value,
|
||||
e.from,
|
||||
e.to,
|
||||
reason,
|
||||
)
|
||||
}
|
||||
|
||||
func NewErrCanNotConvertType(value interface{}, from Type, to Type, reason ...string) ErrCanNotConvertType {
|
||||
return ErrCanNotConvertType{
|
||||
value: value,
|
||||
from: from,
|
||||
to: to,
|
||||
reason: reason,
|
||||
}
|
||||
}
|
31
reflect/indirect.go
Normal file
31
reflect/indirect.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package reflect
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func Indirect(v interface{}) interface{} {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return IndirectValue(reflect.ValueOf(v)).Interface()
|
||||
}
|
||||
|
||||
func IndirectValue(reflectValue Value) Value {
|
||||
if reflectValue.Kind() == reflect.Ptr {
|
||||
return reflectValue.Elem()
|
||||
}
|
||||
return reflectValue
|
||||
}
|
||||
|
||||
func IndirectType(reflectType Type) Type {
|
||||
if reflectType == TypeInvalid {
|
||||
return TypeInvalid
|
||||
}
|
||||
|
||||
if reflectType.Kind() == reflect.Ptr {
|
||||
return reflectType.Elem()
|
||||
}
|
||||
return reflectType
|
||||
}
|
35
reflect/kind.go
Normal file
35
reflect/kind.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package reflect
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
const (
|
||||
Invalid = reflect.Invalid
|
||||
Bool = reflect.Bool
|
||||
Int = reflect.Int
|
||||
Int8 = reflect.Int8
|
||||
Int16 = reflect.Int16
|
||||
Int32 = reflect.Int32
|
||||
Int64 = reflect.Int64
|
||||
Uint = reflect.Uint
|
||||
Uint8 = reflect.Uint8
|
||||
Uint16 = reflect.Uint16
|
||||
Uint32 = reflect.Uint32
|
||||
Uint64 = reflect.Uint64
|
||||
Uintptr = reflect.Uintptr
|
||||
Float32 = reflect.Float32
|
||||
Float64 = reflect.Float64
|
||||
Complex64 = reflect.Complex64
|
||||
Complex128 = reflect.Complex128
|
||||
Array = reflect.Array
|
||||
Chan = reflect.Chan
|
||||
Func = reflect.Func
|
||||
Interface = reflect.Interface
|
||||
Map = reflect.Map
|
||||
Ptr = reflect.Ptr
|
||||
Slice = reflect.Slice
|
||||
String = reflect.String
|
||||
Struct = reflect.Struct
|
||||
UnsafePointer = reflect.UnsafePointer
|
||||
)
|
|
@ -4,6 +4,53 @@ import (
|
|||
"reflect"
|
||||
)
|
||||
|
||||
var (
|
||||
TypeOf = reflect.TypeOf
|
||||
)
|
||||
|
||||
type Type = reflect.Type
|
||||
|
||||
var (
|
||||
Types = []Type{
|
||||
TypeBool,
|
||||
TypeInt,
|
||||
TypeInt8,
|
||||
TypeInt16,
|
||||
TypeInt32,
|
||||
TypeInt64,
|
||||
TypeUint,
|
||||
TypeUint8,
|
||||
TypeUint16,
|
||||
TypeUint32,
|
||||
TypeUint64,
|
||||
TypeFloat32,
|
||||
TypeFloat64,
|
||||
TypeComplex64,
|
||||
TypeComplex128,
|
||||
TypeUintptr,
|
||||
TypeString,
|
||||
}
|
||||
|
||||
TypeInvalid = Type(nil)
|
||||
TypeBool = reflect.TypeOf(false)
|
||||
TypeInt = reflect.TypeOf(int(0))
|
||||
TypeInt8 = reflect.TypeOf(int8(0))
|
||||
TypeInt16 = reflect.TypeOf(int16(0))
|
||||
TypeInt32 = reflect.TypeOf(int32(0))
|
||||
TypeInt64 = reflect.TypeOf(int64(0))
|
||||
TypeUint = reflect.TypeOf(uint(0))
|
||||
TypeUint8 = reflect.TypeOf(uint8(0))
|
||||
TypeUint16 = reflect.TypeOf(uint16(0))
|
||||
TypeUint32 = reflect.TypeOf(uint32(0))
|
||||
TypeUint64 = reflect.TypeOf(uint64(0))
|
||||
TypeFloat32 = reflect.TypeOf(float32(0))
|
||||
TypeFloat64 = reflect.TypeOf(float64(0))
|
||||
TypeComplex64 = reflect.TypeOf(complex64(0))
|
||||
TypeComplex128 = reflect.TypeOf(complex128(0))
|
||||
TypeUintptr = reflect.TypeOf(uintptr(0))
|
||||
TypeString = reflect.TypeOf(string(""))
|
||||
)
|
||||
|
||||
func IsTypeKind(t reflect.Type, kind reflect.Kind, removePtr bool) bool {
|
||||
if reflect.Ptr == t.Kind() {
|
||||
if removePtr {
|
||||
|
@ -21,3 +68,7 @@ func GetTypeInfo(t reflect.Type) (realType reflect.Type, pkgName string, name st
|
|||
|
||||
return t, t.PkgPath(), t.Name()
|
||||
}
|
||||
|
||||
func ConvertStringToType() {
|
||||
|
||||
}
|
||||
|
|
11
reflect/value.go
Normal file
11
reflect/value.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package reflect
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var (
|
||||
ValueOf = reflect.ValueOf
|
||||
)
|
||||
|
||||
type Value = reflect.Value
|
Loading…
Reference in New Issue
Block a user