本文整理匯總了Golang中reflect.Type.Implements方法的典型用法代碼示例。如果您正苦於以下問題:Golang Type.Implements方法的具體用法?Golang Type.Implements怎麽用?Golang Type.Implements使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類reflect.Type
的用法示例。
在下文中一共展示了Type.Implements方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: typeMarshaler
func typeMarshaler(t reflect.Type) marshalFunc {
if t.Implements(textMarshalerType) {
return textMarshaler
}
switch t.Kind() {
case reflect.Bool:
return boolMarshaler
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intMarshaler
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return uintMarshaler
case reflect.Float32:
return float32Marshaler
case reflect.Float64:
return float64Marshaler
case reflect.String:
return stringMarshaler
case reflect.Slice:
return newSliceMarshaler(t)
case reflect.Array:
return newArrayMarshaler(t)
default:
panic(fmt.Sprintf("dynamodb: %s type is not supported", t.Kind()))
}
}
示例2: makeWriter
// makeWriter creates a writer function for the given type.
func makeWriter(typ reflect.Type) (writer, error) {
kind := typ.Kind()
switch {
case typ.Implements(encoderInterface):
return writeEncoder, nil
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(encoderInterface):
return writeEncoderNoPtr, nil
case kind == reflect.Interface:
return writeInterface, nil
case typ.AssignableTo(reflect.PtrTo(bigInt)):
return writeBigIntPtr, nil
case typ.AssignableTo(bigInt):
return writeBigIntNoPtr, nil
case isUint(kind):
return writeUint, nil
case kind == reflect.Bool:
return writeBool, nil
case kind == reflect.String:
return writeString, nil
case kind == reflect.Slice && isByte(typ.Elem()):
return writeBytes, nil
case kind == reflect.Array && isByte(typ.Elem()):
return writeByteArray, nil
case kind == reflect.Slice || kind == reflect.Array:
return makeSliceWriter(typ)
case kind == reflect.Struct:
return makeStructWriter(typ)
case kind == reflect.Ptr:
return makePtrWriter(typ)
default:
return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
}
}
示例3: convertVal
func convertVal(t reflect.Type, v reflect.Value) reflect.Value {
var ptr Pointer
if v.Type() == obj_t {
ptr = v.Interface().(*Object).GetPtr()
} else if v.Type() == ptr_t {
ptr = v.Interface().(Pointer)
}
if ptr != nil {
var ret reflect.Value
if t.Implements(ptr_setter_i) {
// Desired type implements PointerSetter so we are creating
// new value with desired type and set it from ptr
if t.Kind() == reflect.Ptr {
ret = reflect.New(t.Elem())
} else {
ret = reflect.Zero(t)
}
ret.Interface().(PointerSetter).SetPtr(ptr)
} else if t.Kind() == reflect.Ptr {
// t doesn't implements PointerSetter but it is pointer
// so we bypass type checking and setting it from ptr.
ret = valueFromPointer(ptr, t)
}
return ret
}
return v
}
示例4: getValueParser
func getValueParser(t reflect.Type) func(s string) (reflect.Value, error) {
if parser, ok := valueParsers[t]; ok {
return parser
}
if t.Implements(unmarshalerType) {
return func(s string) (reflect.Value, error) {
v := reflect.Zero(t)
buff, err := json.Marshal(s)
if err != nil {
return v, err
}
err = v.Interface().(json.Unmarshaler).UnmarshalJSON(buff)
return v, err
}
}
tt := reflect.PtrTo(t)
if tt.Implements(unmarshalerType) {
return func(s string) (reflect.Value, error) {
v := reflect.New(t)
buff, err := json.Marshal(s)
if err != nil {
return v, err
}
err = v.Interface().(json.Unmarshaler).UnmarshalJSON(buff)
return reflect.Indirect(v), err
}
}
return nil
}
示例5: getDecoder
func (m *structCache) getDecoder(typ reflect.Type) decoderFunc {
if decoder, ok := typDecMap[typ]; ok {
return decoder
}
if typ.Implements(unmarshalerType) {
return unmarshalValue
}
kind := typ.Kind()
switch kind {
case reflect.Slice:
elemKind := typ.Elem().Kind()
if dec := sliceDecoders[elemKind]; dec != nil {
return dec
}
case reflect.Ptr:
fallthrough
case reflect.Struct:
if m.ext != nil {
if decoder, ok := m.ext.decTypeMap[typ]; ok {
return decoder
}
}
}
return valueDecoders[kind]
}
示例6: ShouldNotImplement
// ShouldNotImplement receives exactly two parameters and ensures
// that the first does NOT implement the interface type of the second.
func ShouldNotImplement(actual interface{}, expectedList ...interface{}) string {
if fail := need(1, expectedList); fail != success {
return fail
}
expected := expectedList[0]
if fail := ShouldBeNil(expected); fail != success {
return shouldCompareWithInterfacePointer
}
if fail := ShouldNotBeNil(actual); fail != success {
return shouldNotBeNilActual
}
var actualType reflect.Type
if reflect.TypeOf(actual).Kind() != reflect.Ptr {
actualType = reflect.PtrTo(reflect.TypeOf(actual))
} else {
actualType = reflect.TypeOf(actual)
}
expectedType := reflect.TypeOf(expected)
if fail := ShouldNotBeNil(expectedType); fail != success {
return shouldCompareWithInterfacePointer
}
expectedInterface := expectedType.Elem()
if actualType.Implements(expectedInterface) {
return fmt.Sprintf(shouldNotHaveImplemented, actualType, expectedInterface)
}
return success
}
示例7: Decoder
func Decoder(typ reflect.Type) valueDecoder {
if typ == timeType {
return decodeTimeValue
}
if reflect.PtrTo(typ).Implements(scannerType) {
return decodeScannerAddrValue
}
if typ.Implements(scannerType) {
return decodeScannerValue
}
kind := typ.Kind()
if kind == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
return decodeBytesValue
}
if dec := valueDecoders[kind]; dec != nil {
return dec
}
return nil
}
示例8: isScalar
// isScalar returns true if the type can be parsed from a single string
func isScalar(t reflect.Type) (scalar, boolean bool) {
// If it implements encoding.TextUnmarshaler then use that
if t.Implements(textUnmarshalerType) {
// scalar=YES, boolean=NO
return true, false
}
// If we have a pointer then dereference it
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
// Check for other special types
switch t {
case durationType, mailAddressType, ipType, macType:
// scalar=YES, boolean=NO
return true, false
}
// Fall back to checking the kind
switch t.Kind() {
case reflect.Bool:
// scalar=YES, boolean=YES
return true, true
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
// scalar=YES, boolean=NO
return true, false
}
// scalar=NO, boolean=NO
return false, false
}
示例9: getTypeEncoder
func getTypeEncoder(typ reflect.Type) encoderFunc {
kind := typ.Kind()
if typ.Implements(encoderType) {
return encodeCustomValue
}
// Addressable struct field value.
if reflect.PtrTo(typ).Implements(encoderType) {
return encodeCustomValuePtr
}
if typ.Implements(marshalerType) {
return marshalValue
}
if encoder, ok := typEncMap[typ]; ok {
return encoder
}
if kind == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
return encodeBytesValue
}
return valueEncoders[kind]
}
示例10: getDecoder
func getDecoder(typ reflect.Type) decoderFunc {
kind := typ.Kind()
// Addressable struct field value.
if kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderType) {
return decodeCustomValuePtr
}
if typ.Implements(decoderType) {
return decodeCustomValue
}
if typ.Implements(unmarshalerType) {
return unmarshalValue
}
if decoder, ok := typDecMap[typ]; ok {
return decoder
}
if kind == reflect.Slice && typ.Elem().Kind() == reflect.Uint8 {
return decodeBytesValue
}
return valueDecoders[kind]
}
示例11: makeDecoder
func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
kind := typ.Kind()
switch {
case typ == rawValueType:
return decodeRawValue, nil
case typ.Implements(decoderInterface):
return decodeDecoder, nil
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface):
return decodeDecoderNoPtr, nil
case typ.AssignableTo(reflect.PtrTo(bigInt)):
return decodeBigInt, nil
case typ.AssignableTo(bigInt):
return decodeBigIntNoPtr, nil
case isUint(kind):
return decodeUint, nil
case kind == reflect.Bool:
return decodeBool, nil
case kind == reflect.String:
return decodeString, nil
case kind == reflect.Slice || kind == reflect.Array:
return makeListDecoder(typ, tags)
case kind == reflect.Struct:
return makeStructDecoder(typ)
case kind == reflect.Ptr:
if tags.nilOK {
return makeOptionalPtrDecoder(typ)
}
return makePtrDecoder(typ)
case kind == reflect.Interface:
return decodeInterface, nil
default:
return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
}
}
示例12: getSetter
func getSetter(outt reflect.Type, out reflect.Value) Setter {
setterMutex.RLock()
style := setterStyle[outt]
setterMutex.RUnlock()
if style == setterNone {
return nil
}
if style == setterUnknown {
setterMutex.Lock()
defer setterMutex.Unlock()
if outt.Implements(setterIface) {
setterStyle[outt] = setterType
} else if reflect.PtrTo(outt).Implements(setterIface) {
setterStyle[outt] = setterAddr
} else {
setterStyle[outt] = setterNone
return nil
}
style = setterStyle[outt]
}
if style == setterAddr {
if !out.CanAddr() {
return nil
}
out = out.Addr()
} else if outt.Kind() == reflect.Ptr && out.IsNil() {
out.Set(reflect.New(outt.Elem()))
}
return out.Interface().(Setter)
}
示例13: IsSupportUnmarshal
func IsSupportUnmarshal(t reflect.Type) bool {
if t.Kind() != reflect.Ptr {
t = reflect.PtrTo(t)
}
return t.Implements(unmarshalerType)
}
示例14: implementsUnpacker
func implementsUnpacker(t reflect.Type) bool {
for _, tUnpack := range tUnpackers {
if t.Implements(tUnpack) {
return true
}
}
return false
}
示例15: isOkType
func isOkType(v reflect.Type) bool {
if v.Implements(typeOfPropertyLoadSaver) {
return true
}
if v.Kind() == reflect.Ptr && v.Elem().Kind() == reflect.Struct {
return true
}
return false
}