本文整理汇总了Golang中reflect.Value.Convert方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Convert方法的具体用法?Golang Value.Convert怎么用?Golang Value.Convert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Convert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Unmarshal
// Unmarshal fills each field in the givne interface{} with the AMF data on the
// stream in-order.
//
// If a value of amf0.Null or amf0.Undefined is read, then the value will be
// skipped.
//
// If a pointer field type is reached, then the value will be reduced to match
// that same pointer type.
func (u *Unmarshaler) Unmarshal(dest interface{}) error {
v := reflect.ValueOf(dest).Elem()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
next, err := u.decode(u.r)
if err != nil {
return err
}
if u.isBodyless(next) {
if !u.canAssignNil(field) {
return fmt.Errorf(
"amf0: unable to assign nil to type %T",
field.Interface())
}
continue
}
var val reflect.Value
if u.canAssignNil(field) {
val = reflect.ValueOf(next)
} else {
val = reflect.ValueOf(next).Elem()
}
field.Set(val.Convert(field.Type()))
}
return nil
}
示例2: equal
// equal return true when lhsV and rhsV is same value.
func equal(lhsV, rhsV reflect.Value) bool {
lhsIsNil, rhsIsNil := isNil(lhsV), isNil(rhsV)
if lhsIsNil && rhsIsNil {
return true
}
if (!lhsIsNil && rhsIsNil) || (lhsIsNil && !rhsIsNil) {
return false
}
if lhsV.Kind() == reflect.Interface || lhsV.Kind() == reflect.Ptr {
lhsV = lhsV.Elem()
}
if rhsV.Kind() == reflect.Interface || rhsV.Kind() == reflect.Ptr {
rhsV = rhsV.Elem()
}
if !lhsV.IsValid() || !rhsV.IsValid() {
return true
}
if isNum(lhsV) && isNum(rhsV) {
if rhsV.Type().ConvertibleTo(lhsV.Type()) {
rhsV = rhsV.Convert(lhsV.Type())
}
}
if lhsV.CanInterface() && rhsV.CanInterface() {
return reflect.DeepEqual(lhsV.Interface(), rhsV.Interface())
}
return reflect.DeepEqual(lhsV, rhsV)
}
示例3: mapidx
func mapidx(v reflect.Value, nm string) (interface{}, error) {
t := v.Type()
keyt := t.Key()
var k reflect.Value
switch keyt.Kind() {
case reflect.String:
k = reflect.ValueOf(nm)
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
idx, err := strconv.ParseInt(nm, 0, 64)
if err != nil {
return nil, fmt.Errorf("%s not an index", nm)
}
k = reflect.ValueOf(idx)
case reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64:
idx, err := strconv.ParseUint(nm, 0, 64)
if err != nil {
return nil, fmt.Errorf("%s not an index", nm)
}
k = reflect.ValueOf(uint(idx))
default:
return nil, fmt.Errorf("%s: can't handle index type", nm)
}
if !k.Type().ConvertibleTo(keyt) {
return nil, fmt.Errorf("invalid map key type")
}
k = k.Convert(keyt)
return v.MapIndex(k).Interface(), nil
}
示例4: convertAndSet
func convertAndSet(to, from reflect.Value, setMethod reflect.Value) {
var toType reflect.Type
if setMethod.IsValid() {
toType = setMethod.Type().In(0)
} else {
toType = to.Type()
}
fromType := from.Type()
defer func() {
if v := recover(); v != nil {
panic(fmt.Sprintf("cannot use %s as a %s", fromType, toType))
}
}()
if fromType == listType && toType.Kind() == reflect.Slice {
list := from.Interface().(*List)
from = reflect.MakeSlice(toType, len(list.data), len(list.data))
elemType := toType.Elem()
for i, elem := range list.data {
from.Index(i).Set(reflect.ValueOf(elem).Convert(elemType))
}
} else if toType != fromType {
from = from.Convert(toType)
}
if setMethod.IsValid() {
setMethod.Call([]reflect.Value{from})
} else {
to.Set(from)
}
}
示例5: promoteUntypedNumerals
// Only considers untyped kinds produced by our runtime. Assumes input type is unnamed
func promoteUntypedNumerals(x, y reflect.Value) (reflect.Value, reflect.Value) {
switch x.Kind() {
case reflect.Int64:
switch y.Kind() {
case reflect.Int64:
return x, y
case reflect.Float64:
return x.Convert(y.Type()), y
case reflect.Complex128:
return reflect.ValueOf(complex(float64(x.Int()), 0)), y
}
case reflect.Float64:
switch y.Kind() {
case reflect.Int64:
return x, y.Convert(x.Type())
case reflect.Float64:
return x, y
case reflect.Complex128:
return reflect.ValueOf(complex(x.Float(), 0)), y
}
case reflect.Complex128:
switch y.Kind() {
case reflect.Int64:
return x, reflect.ValueOf(complex(float64(y.Int()), 0))
case reflect.Float64:
return x, reflect.ValueOf(complex(y.Float(), 0))
case reflect.Complex128:
return x, y
}
}
panic(fmt.Sprintf("runtime: bad untyped numeras %v and %v", x, y))
}
示例6: encodeMapStringInterfaceValue
func encodeMapStringInterfaceValue(e *Encoder, v reflect.Value) error {
if v.IsNil() {
return e.EncodeNil()
}
if err := e.EncodeMapLen(v.Len()); err != nil {
return err
}
m := v.Convert(mapStringInterfaceType).Interface().(map[string]interface{})
if e.sortMapKeys {
return e.encodeSortedMapStringInterface(m)
}
for mk, mv := range m {
if err := e.EncodeString(mk); err != nil {
return err
}
if err := e.Encode(mv); err != nil {
return err
}
}
return nil
}
示例7: convertToAmfType
func (m *Marshaler) convertToAmfType(val reflect.Value) (amf0.AmfType, error) {
if val.Kind() == reflect.Ptr && val.IsNil() {
return new(amf0.Null), nil
}
amf := m.i.NewMatchingTypeFromValue(val)
if amf == nil {
return nil, noMatchingType{val.Type()}
}
var (
toType reflect.Type
amft = reflect.ValueOf(amf)
isPtr = val.Kind() == reflect.Ptr
)
if isPtr {
toType = amft.Type()
} else {
toType = amft.Type().Elem()
}
if !val.Type().ConvertibleTo(toType) {
return nil, typeUnassignable{val.Type(), amft.Type().Elem()}
}
val = val.Convert(toType)
if isPtr {
val = val.Elem()
}
amft.Elem().Set(val)
return amf, nil
}
示例8: sliceReflect
func (en *encoder) sliceReflect(key uint64, slval reflect.Value) {
if slval.Kind() != reflect.Slice {
panic("no slice passed")
}
sllen := slval.Len()
slelt := slval.Type().Elem()
packed := encoder{}
switch slelt.Kind() {
case reflect.Bool:
for i := 0; i < sllen; i++ {
v := uint64(0)
if slval.Index(i).Bool() {
v = 1
}
packed.uvarint(v)
}
case reflect.Int, reflect.Int32, reflect.Int64:
for i := 0; i < sllen; i++ {
packed.svarint(slval.Index(i).Int())
}
case reflect.Uint32, reflect.Uint64:
for i := 0; i < sllen; i++ {
packed.uvarint(slval.Index(i).Uint())
}
case reflect.Float32:
for i := 0; i < sllen; i++ {
packed.u32(math.Float32bits(
float32(slval.Index(i).Float())))
}
case reflect.Float64:
for i := 0; i < sllen; i++ {
packed.u64(math.Float64bits(slval.Index(i).Float()))
}
case reflect.Uint8: // Write the byte-slice as one key,value pair
en.uvarint(key | 2)
en.uvarint(uint64(sllen))
b := slval.Convert(bytesType).Interface().([]byte)
en.Write(b)
return
default: // Write each element as a separate key,value pair
for i := 0; i < sllen; i++ {
en.value(key, slval.Index(i), TagNone)
}
return
}
// Encode packed representation key/value pair
en.uvarint(key | 2)
b := packed.Bytes()
en.uvarint(uint64(len(b)))
en.Write(b)
}
示例9: CanConvert
func CanConvert(v reflect.Value, t reflect.Type) (val reflect.Value, canConvert bool) {
defer func() {
if r := recover(); r != nil {
}
}()
converted := v.Convert(t)
return converted, true
}
示例10: convertAndSet
func convertAndSet(to, from reflect.Value) {
defer func() {
if v := recover(); v != nil {
// TODO This should be an error. Test and fix.
panic("FIXME attempted to set a field with the wrong type; this should be an error")
}
}()
to.Set(from.Convert(to.Type()))
}
示例11: Convert
// Convert takes a val and converts it into the target type provided if possible.
func Convert(target reflect.Type, val reflect.Value) (reflect.Value, error) {
valType := val.Type()
if !valType.ConvertibleTo(target) {
return reflect.Value{}, errors.New("Can not convert type")
}
return val.Convert(target), nil
}
示例12: convertField
func convertField(src reflect.Value, dstptr *reflect.Value) error {
t1 := src.Type()
t2 := dstptr.Type()
if t1.ConvertibleTo(t2) {
resp := src.Convert(t2)
dstptr.Set(resp)
}
return nil
}
示例13: encodeTime
func encodeTime(v reflect.Value) string {
t := v.Convert(timeType).Interface().(time.Time)
if t.Year() == 0 && (t.Month() == 0 || t.Month() == 1) && (t.Day() == 0 || t.Day() == 1) {
return t.Format("15:04:05.999999999Z07:00")
} else if t.Hour() == 0 && t.Minute() == 0 && t.Second() == 0 && t.Nanosecond() == 0 {
return t.Format("2006-01-02")
}
return t.Format("2006-01-02T15:04:05.999999999Z07:00")
}
示例14: itemsv
func (e *encoder) itemsv(tag string, in reflect.Value) {
e.mappingv(tag, func() {
slice := in.Convert(reflect.TypeOf([]MapItem{})).Interface().([]MapItem)
for _, item := range slice {
e.marshal("", reflect.ValueOf(item.Key))
e.marshal("", reflect.ValueOf(item.Value))
}
})
}
示例15: tryConvert
func tryConvert(in reflect.Value, t reflect.Type) (out reflect.Value, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%s", r)
}
}()
return in.Convert(t), nil
}