本文整理汇总了Golang中reflect.Type.Elem方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Elem方法的具体用法?Golang Type.Elem怎么用?Golang Type.Elem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Type
的用法示例。
在下文中一共展示了Type.Elem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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()
}
示例2: sampleFormat
func sampleFormat(b reflect.Type) (f C.PaSampleFormat) {
if b.Kind() != reflect.Slice {
return 0
}
b = b.Elem()
if b.Kind() == reflect.Slice {
f = C.paNonInterleaved
b = b.Elem()
}
switch b.Kind() {
case reflect.Float32:
f |= C.paFloat32
case reflect.Int32:
f |= C.paInt32
default:
if b == reflect.TypeOf(Int24{}) {
f |= C.paInt24
} else {
return 0
}
case reflect.Int16:
f |= C.paInt16
case reflect.Int8:
f |= C.paInt8
case reflect.Uint8:
f |= C.paUInt8
}
return f
}
示例3: newMapEncoder
func newMapEncoder(t reflect.Type) encoderFunc {
if t.Key().Kind() != reflect.String {
return unsupportedTypeEncoder
}
me := &mapEncoder{typeEncoder(t.Elem())}
return me.encode
}
示例4: New
func New(t interface{}, tags []string, conf Configurator) (*Struct, error) {
var typ reflect.Type
if tt, ok := t.(reflect.Type); ok {
typ = tt
} else {
typ = reflect.TypeOf(t)
}
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
if typ.Kind() != reflect.Struct {
return nil, ErrNoStruct
}
if typ.NumField() == 0 {
return nil, ErrNoFields
}
s := &Struct{
Type: typ,
MNameMap: make(map[string]int),
QNameMap: make(map[string]int),
tags: tags,
conf: conf,
}
if err := s.initialize(typ); err != nil {
return nil, err
}
return s, nil
}
示例5: decodeArray
func decodeArray(r io.Reader, t reflect.Type) (reflect.Value, error) {
var sz uint32
if err := binary.Read(r, byteOrder, &sz); err != nil {
return nullValue, err
}
ksz := int(kindSize(t.Elem().Kind()))
data := make([]byte, int(sz)*ksz)
_, err := r.Read(data)
if err != nil {
return nullValue, err
}
slice := reflect.MakeSlice(t, int(sz), int(sz))
for i := 0; i < int(sz); i++ {
from := data[i*ksz:]
var val uint64
switch ksz {
case 1:
val = uint64(from[0])
case 2:
val = uint64(byteOrder.Uint16(from[0:]))
case 4:
val = uint64(byteOrder.Uint32(from[0:]))
default:
panic("unimp")
}
slice.Index(i).SetUint(val)
}
return slice, nil
}
示例6: verifyHandler
// verifyHandler ensures that the given t is a function with the following signature:
// func(json.Context, *ArgType)(*ResType, error)
func verifyHandler(t reflect.Type) error {
if t.NumIn() != 2 || t.NumOut() != 2 {
return fmt.Errorf("handler should be of format func(json.Context, *ArgType) (*ResType, error)")
}
isStructPtr := func(t reflect.Type) bool {
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
}
isMap := func(t reflect.Type) bool {
return t.Kind() == reflect.Map && t.Key().Kind() == reflect.String
}
validateArgRes := func(t reflect.Type, name string) error {
if !isStructPtr(t) && !isMap(t) {
return fmt.Errorf("%v should be a pointer to a struct, or a map[string]interface{}", name)
}
return nil
}
if t.In(0) != typeOfContext {
return fmt.Errorf("arg0 should be of type json.Context")
}
if err := validateArgRes(t.In(1), "second argument"); err != nil {
return err
}
if err := validateArgRes(t.Out(0), "first return value"); err != nil {
return err
}
if !t.Out(1).AssignableTo(typeOfError) {
return fmt.Errorf("second return value should be an error")
}
return nil
}
示例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: ToSqlType
func (d SqliteDialect) ToSqlType(val reflect.Type, maxsize int, isAutoIncr bool) string {
switch val.Kind() {
case reflect.Ptr:
return d.ToSqlType(val.Elem(), maxsize, isAutoIncr)
case reflect.Bool:
return "integer"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "integer"
case reflect.Float64, reflect.Float32:
return "real"
case reflect.Slice:
if val.Elem().Kind() == reflect.Uint8 {
return "blob"
}
}
switch val.Name() {
case "NullInt64":
return "integer"
case "NullFloat64":
return "real"
case "NullBool":
return "integer"
case "Time":
return "datetime"
}
if maxsize < 1 {
maxsize = 255
}
return fmt.Sprintf("varchar(%d)", maxsize)
}
示例9: fieldType
func fieldType(t reflect.Type) byte {
switch t.Kind() {
case reflect.Bool:
return TypeBool
case reflect.Int8, reflect.Uint8:
return TypeByte
case reflect.Int16:
return TypeI16
case reflect.Int32, reflect.Int:
return TypeI32
case reflect.Int64:
return TypeI64
case reflect.Float64:
return TypeDouble
case reflect.Map:
return TypeMap
case reflect.Slice:
elemType := t.Elem()
if elemType.Kind() == reflect.Uint8 {
return TypeString
} else {
return TypeList
}
case reflect.Struct:
return TypeStruct
case reflect.String:
return TypeString
case reflect.Interface, reflect.Ptr:
return fieldType(t.Elem())
}
panic(&UnsupportedTypeError{t})
}
示例10: 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)
}
示例11: defaultStart
// defaultStart returns the default start element to use,
// given the reflect type, field info, and start template.
func (p *printer) defaultStart(typ reflect.Type, finfo *fieldInfo, startTemplate *StartElement) StartElement {
var start StartElement
// Precedence for the XML element name is as above,
// except that we do not look inside structs for the first field.
if startTemplate != nil {
start.Name = startTemplate.Name
start.Attr = append(start.Attr, startTemplate.Attr...)
} else if finfo != nil && finfo.name != "" {
start.Name.Local = finfo.name
start.Name.Space = finfo.xmlns
} else if typ.Name() != "" {
start.Name.Local = typ.Name()
} else {
// Must be a pointer to a named type,
// since it has the Marshaler methods.
start.Name.Local = typ.Elem().Name()
}
// Historic behaviour: elements use the name space of
// the element they are contained in by default.
if start.Name.Space == "" {
start.Name.Space = p.defaultNS
}
start.setDefaultNamespace()
return start
}
示例12: makeOptionalPtrDecoder
// makeOptionalPtrDecoder creates a decoder that decodes empty values
// as nil. Non-empty values are decoded into a value of the element type,
// just like makePtrDecoder does.
//
// This decoder is used for pointer-typed struct fields with struct tag "nil".
func makeOptionalPtrDecoder(typ reflect.Type) (decoder, error) {
etype := typ.Elem()
etypeinfo, err := cachedTypeInfo1(etype, tags{})
if err != nil {
return nil, err
}
dec := func(s *Stream, val reflect.Value) (err error) {
kind, size, err := s.Kind()
if err != nil || size == 0 && kind != Byte {
// rearm s.Kind. This is important because the input
// position must advance to the next value even though
// we don't read anything.
s.kind = -1
// set the pointer to nil.
val.Set(reflect.Zero(typ))
return err
}
newval := val
if val.IsNil() {
newval = reflect.New(etype)
}
if err = etypeinfo.decoder(s, newval.Elem()); err == nil {
val.Set(newval)
}
return err
}
return dec, nil
}
示例13: formFromGoType
// formFromGoType returns a suitable FITS TFORM string from a reflect.Type
func formFromGoType(rt reflect.Type, htype HDUType) string {
hdr := ""
var t reflect.Type
switch rt.Kind() {
case reflect.Array:
hdr = fmt.Sprintf("%d", rt.Len())
t = rt.Elem()
case reflect.Slice:
hdr = "Q"
t = rt.Elem()
default:
t = rt
}
dict, ok := g_gotype2FITS[t.Kind()]
if !ok {
return ""
}
form, ok := dict[htype]
if !ok {
return ""
}
return hdr + form
}
示例14: makeListDecoder
func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) {
etype := typ.Elem()
if etype.Kind() == reflect.Uint8 && !reflect.PtrTo(etype).Implements(decoderInterface) {
if typ.Kind() == reflect.Array {
return decodeByteArray, nil
} else {
return decodeByteSlice, nil
}
}
etypeinfo, err := cachedTypeInfo1(etype, tags{})
if err != nil {
return nil, err
}
var dec decoder
switch {
case typ.Kind() == reflect.Array:
dec = func(s *Stream, val reflect.Value) error {
return decodeListArray(s, val, etypeinfo.decoder)
}
case tag.tail:
// A slice with "tail" tag can occur as the last field
// of a struct and is upposed to swallow all remaining
// list elements. The struct decoder already called s.List,
// proceed directly to decoding the elements.
dec = func(s *Stream, val reflect.Value) error {
return decodeSliceElems(s, val, etypeinfo.decoder)
}
default:
dec = func(s *Stream, val reflect.Value) error {
return decodeListSlice(s, val, etypeinfo.decoder)
}
}
return dec, nil
}
示例15: getFields
func getFields(t reflect.Type) []reflect.StructField {
fields := make([]reflect.StructField, 0)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
ft := f.Type
if ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
switch ft.Kind() {
case reflect.Struct:
if f.Anonymous {
fields = append(fields, getFields(ft)...)
} else if len(f.PkgPath) == 0 {
fields = append(fields, f)
}
default:
if len(f.PkgPath) == 0 {
fields = append(fields, f)
}
}
}
return fields
}