本文整理汇总了Golang中reflect.Type.ConvertibleTo方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.ConvertibleTo方法的具体用法?Golang Type.ConvertibleTo怎么用?Golang Type.ConvertibleTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.ConvertibleTo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: IsEqual
func (p *MethodMetadata) IsEqual(t reflect.Type) bool {
if t.ConvertibleTo(p.Method.Type) {
return false
}
baseIndex := 0
if p.Method.Index >= 0 {
baseIndex = 1
}
if t.NumIn()+baseIndex != p.Method.Type.NumIn() {
return false
}
for i := 0; i < p.Method.Type.NumIn()-baseIndex; i++ {
if p.Method.Type.In(baseIndex+i) != t.In(i) {
return false
}
}
for i := 0; i < p.Method.Type.NumOut(); i++ {
if p.Method.Type.Out(baseIndex+i) != t.Out(i) {
return false
}
}
return true
}
示例2: HandleEvent
func (h funcHandler) HandleEvent(typ reflect.Type, val reflect.Value) {
if typ == nil {
h.fn.Call([]reflect.Value{})
} else if typ.ConvertibleTo(h.typ) {
h.fn.Call([]reflect.Value{val})
}
}
示例3: Run
// Run recevies the argument and
func (m *mqueSub) Run(d interface{}, ctype reflect.Type) {
if !m.has {
for _, tm := range m.tms {
tm.Call([]reflect.Value{})
}
return
}
var configVal reflect.Value
if !ctype.AssignableTo(m.am) {
if !ctype.ConvertibleTo(m.am) {
return
}
vum := reflect.ValueOf(d)
configVal = vum.Convert(m.am)
} else {
configVal = reflect.ValueOf(d)
}
for _, tm := range m.tms {
tm.Call([]reflect.Value{configVal})
}
}
示例4: Short
// Short returns a unique (in the current scope) name for the argument of type t.
func (opts *GenOpts) Short(t reflect.Type, cur map[string]struct{}) string {
tt := t
for tt.Kind() == reflect.Ptr || tt.Kind() == reflect.Slice {
tt = tt.Elem()
}
pkg, name := packageAndName(tt)
f := opts.First(name) // First letter.
// Handle common types.
switch {
case t.ConvertibleTo(errorType):
f = "err"
case t.ConvertibleTo(ctxType):
f = "ctx"
default:
n, clean := opts.lowerName(name)
// Very short names.
if len(n) <= 3 && string(n) != clean && string(n) != pkg {
f = string(n)
}
}
// Make sure the name is unique.
name = f
for c := 1; ; c++ {
if _, ok := cur[name]; !ok {
// Update the set of currently used names.
cur[name] = struct{}{}
return name
}
name = fmt.Sprintf("%s%d", f, c)
}
}
示例5: mustBeCompatible
func mustBeCompatible(a, b reflect.Type) {
if !a.ConvertibleTo(b) || !a.AssignableTo(b) {
panic(errors.New(fmt.Sprintf(
"Types '%v' and '%v' is not compatile to each other! "+
"It is no possible to make a swap function that "+
"return or receive different kinds of objects!", a.Name(), b.Name())))
}
}
示例6: isNumberType
func isNumberType(t reflect.Type) bool {
for _, nt := range numberTypes {
if t.ConvertibleTo(nt) {
return true
}
}
return false
}
示例7: CanSetForType
// CanSetForType checks if a val reflect.Type can be used for the target type.
// It returns true bool, where the first returns if the value can be used and if
// it must be converted into the type first.
func CanSetForType(target, val reflect.Type) (canSet bool, mustConvert bool) {
if val.AssignableTo(target) {
canSet = true
return
}
if val.ConvertibleTo(target) {
canSet = true
mustConvert = true
return
}
return
}
示例8: unmarshalToType
func unmarshalToType(typ reflect.Type, value string) (val interface{}, err error) {
// If we get a pointer in, we'll return a pointer out
if typ.Kind() == reflect.Ptr {
val = reflect.New(typ.Elem()).Interface()
} else {
val = reflect.New(typ).Interface()
}
defer func() {
if err == nil && typ.Kind() != reflect.Ptr {
val = reflect.Indirect(reflect.ValueOf(val)).Interface()
}
}()
// If we can just assign the value, return the value
if typ.AssignableTo(reflect.TypeOf(value)) {
return value, nil
}
// Try Unmarshalers
if um, ok := val.(encoding.TextUnmarshaler); ok {
if err = um.UnmarshalText([]byte(value)); err == nil {
return val, nil
}
}
if um, ok := val.(json.Unmarshaler); ok {
if err = um.UnmarshalJSON([]byte(value)); err == nil {
return val, nil
}
}
// Try conversion
if typ.ConvertibleTo(reflect.TypeOf(value)) {
return reflect.ValueOf(value).Convert(typ).Interface(), nil
}
// Try JSON
if err = json.Unmarshal([]byte(value), val); err == nil {
return val, nil
}
// Return error if we have one
if err != nil {
return nil, err
}
return val, fmt.Errorf("No way to unmarshal \"%s\" to %s", value, typ.Name())
}
示例9: check
func (s *schemaField) check(ft reflect.Type, v interface{}) error {
t := reflect.TypeOf(v)
if !ft.AssignableTo(t) {
if !ft.ConvertibleTo(t) {
return fmt.Errorf("type %s (%v) cannot be converted to %T", ft.Name(), ft.Kind(), t.Name())
}
s.marshalType = t
}
if !t.AssignableTo(ft) {
if !t.ConvertibleTo(ft) {
return fmt.Errorf("type %s (%v) cannot be converted to %T", t.Name(), t.Kind(), ft.Name())
}
s.unmarshalType = ft
}
return nil
}
示例10: Type2SQLType
func Type2SQLType(t reflect.Type) (st SQLType) {
switch k := t.Kind(); k {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
st = SQLType{Int, 0, 0}
case reflect.Int64, reflect.Uint64:
st = SQLType{BigInt, 0, 0}
case reflect.Float32:
st = SQLType{Float, 0, 0}
case reflect.Float64:
st = SQLType{Double, 0, 0}
case reflect.Complex64, reflect.Complex128:
st = SQLType{Varchar, 64, 0}
case reflect.Array, reflect.Slice, reflect.Map:
if t.Elem() == reflect.TypeOf(c_BYTE_DEFAULT) {
st = SQLType{Blob, 0, 0}
} else {
st = SQLType{Text, 0, 0}
}
case reflect.Bool:
st = SQLType{Bool, 0, 0}
case reflect.String:
st = SQLType{Varchar, 255, 0}
case reflect.Struct:
if t.ConvertibleTo(reflect.TypeOf(c_TIME_DEFAULT)) {
st = SQLType{DateTime, 0, 0}
} else {
// TODO need to handle association struct
st = SQLType{Text, 0, 0}
}
case reflect.Ptr:
st, _ = ptrType2SQLType(t)
default:
st = SQLType{Text, 0, 0}
}
return
}
示例11: implements
func implements(wanted, source reflect.Type) bool {
return source.ConvertibleTo(wanted)
}
示例12: visible
func visible(wanted reflect.Type, mask reflect.Type) bool {
return mask.ConvertibleTo(wanted)
}
示例13: skipTextMarshalling
func skipTextMarshalling(t reflect.Type) bool {
/*// Skip time.Time because its text unmarshaling is overly rigid:
return t == timeType || t == timePtrType*/
// Skip time.Time & convertibles because its text unmarshaling is overly rigid:
return t.ConvertibleTo(timeType) || t.ConvertibleTo(timePtrType)
}
示例14: HandleEvent
func (h chanHandler) HandleEvent(typ reflect.Type, val reflect.Value) {
if typ.ConvertibleTo(typ) {
h.ch.TrySend(val)
}
}
示例15: tyvarName
func tyvarName(t reflect.Type) string {
if !t.ConvertibleTo(tyvarUnderlyingType) {
return ""
}
return t.Name()
}