本文整理匯總了Golang中reflect.Type.Kind方法的典型用法代碼示例。如果您正苦於以下問題:Golang Type.Kind方法的具體用法?Golang Type.Kind怎麽用?Golang Type.Kind使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類reflect.Type
的用法示例。
在下文中一共展示了Type.Kind方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: labelType
func labelType(t reflect.Type) bool {
switch t.Kind() {
case reflect.Interface, reflect.Struct:
return true
}
return false
}
示例2: lessFunc
// lessFunc returns a function that implements the "<" operator
// for the given type, or nil if the type doesn't support "<" .
func lessFunc(t reflect.Type) func(v1, v2 interface{}) bool {
switch t.Kind() {
case reflect.String:
return func(v1, v2 interface{}) bool { return v1.(string) < v2.(string) }
case reflect.Int:
return func(v1, v2 interface{}) bool { return v1.(int) < v2.(int) }
case reflect.Int8:
return func(v1, v2 interface{}) bool { return v1.(int8) < v2.(int8) }
case reflect.Int16:
return func(v1, v2 interface{}) bool { return v1.(int16) < v2.(int16) }
case reflect.Int32:
return func(v1, v2 interface{}) bool { return v1.(int32) < v2.(int32) }
case reflect.Int64:
return func(v1, v2 interface{}) bool { return v1.(int64) < v2.(int64) }
case reflect.Uint:
return func(v1, v2 interface{}) bool { return v1.(uint) < v2.(uint) }
case reflect.Uint8:
return func(v1, v2 interface{}) bool { return v1.(uint8) < v2.(uint8) }
case reflect.Uint16:
return func(v1, v2 interface{}) bool { return v1.(uint16) < v2.(uint16) }
case reflect.Uint32:
return func(v1, v2 interface{}) bool { return v1.(uint32) < v2.(uint32) }
case reflect.Uint64:
return func(v1, v2 interface{}) bool { return v1.(uint64) < v2.(uint64) }
case reflect.Float32:
return func(v1, v2 interface{}) bool { return v1.(float32) < v2.(float32) }
case reflect.Float64:
return func(v1, v2 interface{}) bool { return v1.(float64) < v2.(float64) }
default:
return nil
}
}
示例3: CopyTableToStruct
// Copy matching Lua table entries to a struct, given the struct type
// and the index on the Lua stack.
func CopyTableToStruct(L *lua.State, t reflect.Type, idx int) interface{} {
was_ptr := t.Kind() == reflect.Ptr
if was_ptr {
t = t.Elem()
}
s := reflect.New(t) // T -> *T
ref := s.Elem()
L.PushNil()
if idx < 0 {
idx--
}
for L.Next(idx) != 0 {
key := L.ToString(-2)
f := ref.FieldByName(strings.Title(key))
if f.IsValid() {
val := luaToGoValue(L, f.Type(), -1)
f.Set(val)
}
L.Pop(1)
}
if was_ptr {
return s.Interface()
}
return s.Elem().Interface()
}
示例4: getComparator
func getComparator(dt reflect.Type) (funcPointer interface{}) {
switch dt.Kind() {
case reflect.Int:
funcPointer = func(a, b int) int64 { return int64(a - b) }
case reflect.Int8:
funcPointer = func(a, b int8) int64 { return int64(a - b) }
case reflect.Int16:
funcPointer = func(a, b int16) int64 { return int64(a - b) }
case reflect.Int32:
funcPointer = func(a, b int32) int64 { return int64(a - b) }
case reflect.Uint:
funcPointer = func(a, b uint) int64 { return int64(a - b) }
case reflect.Uint8:
funcPointer = func(a, b uint8) int64 { return int64(a - b) }
case reflect.Uint16:
funcPointer = func(a, b uint16) int64 { return int64(a - b) }
case reflect.Uint32:
funcPointer = func(a, b uint32) int64 { return int64(a - b) }
case reflect.Uint64:
funcPointer = func(a, b uint64) int64 { return int64(a - b) }
case reflect.Int64:
funcPointer = func(a, b int64) int64 { return a - b }
case reflect.Float32:
funcPointer = DefaultFloat32Comparator
case reflect.Float64:
funcPointer = DefaultFloat64Comparator
case reflect.String:
funcPointer = DefaultStringComparator
default:
log.Panicf("No default comparator for %s:%s", dt.String(), dt.Kind().String())
}
return
}
示例5: canBeNil
// canBeNil reports whether an untyped nil can be assigned to the type. See reflect.Zero.
//
// Copied from Go stdlib src/text/template/exec.go.
func canBeNil(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return true
}
return false
}
示例6: 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)
}
示例7: sizeof
// sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable.
func sizeof(t reflect.Type) int {
switch t.Kind() {
case reflect.Array:
if s := sizeof(t.Elem()); s >= 0 {
return s * t.Len()
}
case reflect.Struct:
sum := 0
for i, n := 0, t.NumField(); i < n; i++ {
s := sizeof(t.Field(i).Type)
if s < 0 {
return -1
}
sum += s
}
return sum
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128, reflect.Ptr:
return int(t.Size())
}
return -1
}
示例8: defaultType
func defaultType(t reflect.Type) TypeDesc {
switch t.Kind() {
case reflect.Bool:
return BooleanType
case reflect.String:
return UTF8Type
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return LongType
case reflect.Float32:
return FloatType
case reflect.Float64:
return DoubleType
case reflect.Array:
if t.Name() == "UUID" && t.Size() == 16 {
return UUIDType
}
return UnknownType
case reflect.Struct:
if t.Name() == "Time" && t.PkgPath() == "time" {
return DateType
}
return UnknownType
case reflect.Slice:
if et := t.Elem(); et.Kind() == reflect.Uint8 {
return BytesType
}
return UnknownType
}
return UnknownType
}
示例9: checkCompositeLitR
// Recursively check composite literals, where a child composite lit's type depends the
// parent's type For example, the expression [][]int{{1,2},{3,4}} contains two
// slice lits, {1,2} and {3,4}, but their types are inferenced from the parent [][]int{}.
func checkCompositeLitR(lit *ast.CompositeLit, t reflect.Type, env Env) (*CompositeLit, []error) {
alit := &CompositeLit{CompositeLit: lit}
// We won't generate any errors here if the given type does not match lit.Type.
// The caller will need to detect the type incompatibility.
if lit.Type != nil {
var errs []error
lit.Type, t, _, errs = checkType(lit.Type, env)
if errs != nil {
return alit, errs
}
} else if t == nil {
return alit, []error{ErrMissingCompositeLitType{alit}}
}
alit.knownType = knownType{t}
if alit.CompositeLit.Elts != nil {
alit.Elts = make([]Expr, len(alit.CompositeLit.Elts))
}
switch t.Kind() {
case reflect.Map:
return checkCompositeLitMap(alit, t, env)
case reflect.Array, reflect.Slice:
return checkCompositeLitArrayOrSlice(alit, t, env)
case reflect.Struct:
return checkCompositeLitStruct(alit, t, env)
default:
panic("eval: unimplemented composite lit " + t.Kind().String())
}
}
示例10: convert
// Convert a string into the specified type. Return the type's zero value
// if we receive an empty string
func convert(t reflect.Type, value string) (reflect.Value, error) {
if value == "" {
return reflect.ValueOf(nil), nil
}
var d time.Duration
switch t {
case reflect.TypeOf(d):
result, err := time.ParseDuration(value)
return reflect.ValueOf(result), err
default:
}
switch t.Kind() {
case reflect.String:
return reflect.ValueOf(value), nil
case reflect.Int:
return parseInt(value)
case reflect.Bool:
return parseBool(value)
}
return reflect.ValueOf(nil), conversionError(value, `unsupported `+t.Kind().String())
}
示例11: unflattenValue
func unflattenValue(v reflect.Value, t reflect.Type) reflect.Value {
// When t is an Interface, we can't do much, since we don't know the
// original (unflattened) type of the value placed in v, so we just nop it.
if t.Kind() == reflect.Interface {
return v
}
// v can be invalid, if it holds the nil value for pointer type
if !v.IsValid() {
return v
}
// Make sure v is indeed flat
if v.Kind() == reflect.Ptr {
panic("unflattening non-flat value")
}
// Add a *, one at a time
for t.Kind() == reflect.Ptr {
if v.CanAddr() {
v = v.Addr()
} else {
pw := reflect.New(v.Type())
pw.Elem().Set(v)
v = pw
}
t = t.Elem()
}
return v
}
示例12: prepareMethod
// prepareMethod returns a methodType for the provided method or nil
// in case if the method was unsuitable.
func prepareMethod(method reflect.Method) *methodType {
mtype := method.Type
mname := method.Name
var replyType, argType, contextType reflect.Type
stream := false
// Method must be exported.
if method.PkgPath != "" {
return nil
}
switch mtype.NumIn() {
case 3:
// normal method
argType = mtype.In(1)
replyType = mtype.In(2)
contextType = nil
case 4:
// method that takes a context
argType = mtype.In(2)
replyType = mtype.In(3)
contextType = mtype.In(1)
default:
log.Println("method", mname, "of", mtype, "has wrong number of ins:", mtype.NumIn())
return nil
}
// First arg need not be a pointer.
if !isExportedOrBuiltinType(argType) {
log.Println(mname, "argument type not exported:", argType)
return nil
}
// the second argument will tell us if it's a streaming call
// or a regular call
if replyType == typeOfStream {
// this is a streaming call
stream = true
} else if replyType.Kind() != reflect.Ptr {
log.Println("method", mname, "reply type not a pointer:", replyType)
return nil
}
// Reply type must be exported.
if !isExportedOrBuiltinType(replyType) {
log.Println("method", mname, "reply type not exported:", replyType)
return nil
}
// Method needs one out.
if mtype.NumOut() != 1 {
log.Println("method", mname, "has wrong number of outs:", mtype.NumOut())
return nil
}
// The return type of the method must be error.
if returnType := mtype.Out(0); returnType != typeOfError {
log.Println("method", mname, "returns", returnType.String(), "not error")
return nil
}
return &methodType{method: method, ArgType: argType, ReplyType: replyType, ContextType: contextType, stream: stream}
}
示例13: AddExt
// AddExt registers an encode and decode function for a reflect.Type.
// Note that the type must be a named type, and specifically not
// a pointer or Interface. An error is returned if that is not honored.
//
// To Deregister an ext, call AddExt with 0 tag, nil encfn and nil decfn.
func (o *extHandle) AddExt(
rt reflect.Type,
tag byte,
encfn func(reflect.Value) ([]byte, error),
decfn func(reflect.Value, []byte) error,
) (err error) {
// o is a pointer, because we may need to initialize it
if rt.PkgPath() == "" || rt.Kind() == reflect.Interface {
err = fmt.Errorf("codec.Handle.AddExt: Takes named type, especially not a pointer or interface: %T",
reflect.Zero(rt).Interface())
return
}
// o cannot be nil, since it is always embedded in a Handle.
// if nil, let it panic.
// if o == nil {
// err = errors.New("codec.Handle.AddExt: extHandle cannot be a nil pointer.")
// return
// }
rtid := reflect.ValueOf(rt).Pointer()
for _, v := range *o {
if v.rtid == rtid {
v.tag, v.encFn, v.decFn = tag, encfn, decfn
return
}
}
*o = append(*o, &extTypeTagFn{rtid, rt, tag, encfn, decfn})
return
}
示例14: sizeof
func sizeof(t reflect.Type) (int, error) {
switch t.Kind() {
case reflect.Array:
n, err := sizeof(t.Elem())
if err != nil {
return 0, err
}
return t.Len() * n, nil
case reflect.Struct:
sum := 0
for i, n := 0, t.NumField(); i < n; i++ {
s, err := sizeof(t.Field(i).Type)
if err != nil {
return 0, err
}
sum += s
}
return sum, nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
return int(t.Size()), nil
}
return 0, errors.New("invalid type " + t.String())
}
示例15: alignment
// alignment returns the alignment of values of type t.
func alignment(t reflect.Type) int {
switch t {
case variantType:
return 1
case objectPathType:
return 4
case signatureType:
return 1
case interfacesType:
return 4
}
switch t.Kind() {
case reflect.Uint8:
return 1
case reflect.Uint16, reflect.Int16:
return 2
case reflect.Uint32, reflect.Int32, reflect.String, reflect.Array, reflect.Slice, reflect.Map:
return 4
case reflect.Uint64, reflect.Int64, reflect.Float64, reflect.Struct:
return 8
case reflect.Ptr:
return alignment(t.Elem())
}
return 1
}