当前位置: 首页>>代码示例>>Golang>>正文


Golang Type.ConvertibleTo方法代码示例

本文整理汇总了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
}
开发者ID:gogap,项目名称:aop,代码行数:28,代码来源:method_metadata.go

示例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})
	}
}
开发者ID:noxer,项目名称:events,代码行数:7,代码来源:func.go

示例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})
	}
}
开发者ID:influx6,项目名称:gu,代码行数:27,代码来源:mque.go

示例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)
	}
}
开发者ID:zhaojkun,项目名称:goimpl,代码行数:32,代码来源:goimpl.go

示例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())))
	}
}
开发者ID:darciopacifico,项目名称:enablecache,代码行数:8,代码来源:cache_aop_utils.go

示例6: isNumberType

func isNumberType(t reflect.Type) bool {
	for _, nt := range numberTypes {
		if t.ConvertibleTo(nt) {
			return true
		}
	}
	return false
}
开发者ID:hajimehoshi,项目名称:kakeibo,代码行数:8,代码来源:view.go

示例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
}
开发者ID:influx6,项目名称:gu,代码行数:17,代码来源:reflection.go

示例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())
}
开发者ID:TheThingsNetwork,项目名称:ttn,代码行数:47,代码来源:decoder.go

示例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
}
开发者ID:pkf,项目名称:ql,代码行数:19,代码来源:introspection.go

示例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
}
开发者ID:mbrukman,项目名称:grafana,代码行数:37,代码来源:type.go

示例11: implements

func implements(wanted, source reflect.Type) bool {
	return source.ConvertibleTo(wanted)
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:3,代码来源:inherit.go

示例12: visible

func visible(wanted reflect.Type, mask reflect.Type) bool {
	return mask.ConvertibleTo(wanted)
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:3,代码来源:inherit.go

示例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)
}
开发者ID:srikalyan,项目名称:terraform,代码行数:6,代码来源:encode.go

示例14: HandleEvent

func (h chanHandler) HandleEvent(typ reflect.Type, val reflect.Value) {
	if typ.ConvertibleTo(typ) {
		h.ch.TrySend(val)
	}
}
开发者ID:noxer,项目名称:events,代码行数:5,代码来源:chan.go

示例15: tyvarName

func tyvarName(t reflect.Type) string {
	if !t.ConvertibleTo(tyvarUnderlyingType) {
		return ""
	}
	return t.Name()
}
开发者ID:BurntSushi,项目名称:ty,代码行数:6,代码来源:type-check.go


注:本文中的reflect.Type.ConvertibleTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。