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


Golang reflect.Type函数代码示例

本文整理汇总了Golang中reflect.Type函数的典型用法代码示例。如果您正苦于以下问题:Golang Type函数的具体用法?Golang Type怎么用?Golang Type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Type函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: NewOutChan

func NewOutChan(dec *Decoder, typ interface{}, buffer int) (ch <-chan interface{}) {
	t_ch := &reflect.MakeChan(reflect.Type(typ), buffer-1)
	p := t_ch.(unsafe.Pointer)
	ch = *p.(*chan interface{})
	go func() {
		var err error
		for err != nil {
			i := &reflect.New(reflect.Type(typ))
			err = dec.Decode(i)
			ch <- i.(unsafe.Pointer)
		}
	}()
}
开发者ID:MetaParticle,项目名称:server,代码行数:13,代码来源:iochan_v2.go

示例2: Load

func (p *structPLS) Load(propMap PropertyMap) error {
	if err := p.Problem(); err != nil {
		return err
	}

	convFailures := errors.MultiError(nil)

	t := reflect.Type(nil)
	for name, props := range propMap {
		multiple := len(props) > 1
		for i, prop := range props {
			if reason := loadInner(p.c, p.o, i, name, prop, multiple); reason != "" {
				if t == nil {
					t = p.o.Type()
				}
				convFailures = append(convFailures, &ErrFieldMismatch{
					StructType: t,
					FieldName:  name,
					Reason:     reason,
				})
			}
		}
	}

	if len(convFailures) > 0 {
		return convFailures
	}

	return nil
}
开发者ID:martiniss,项目名称:gae,代码行数:30,代码来源:pls_impl.go

示例3: NewClient

func NewClient(connStr string) (Client, error) {
	methods := map[string]reflect.Type{
		"insert": reflect.Type(reflect.ValueOf(insertMethod).Type()),
		"search": reflect.Type(reflect.ValueOf(searchMethod).Type()),
	}
	serviceGlobalConfig := new(client.ServiceConfig)
	serviceGlobalConfig.ConnectionTimeout = 10
	serviceGlobalConfig.ServerAddr = connStr
	clientApi, err := client.NewServiceClient(make(map[string]string), methods, serviceGlobalConfig)
	if err != nil {
		log.Fatalln(err)
		return nil, err
	}
	return &clientImpl{
		Client: clientApi,
	}, nil
}
开发者ID:moetang,项目名称:searchservice,代码行数:17,代码来源:client.go

示例4: NewInChan

func NewInChan(enc *Encoder, typ interface{}, buffer int) (ch chan<- interface{}) {
	//ch = make(chan interface{}) //
	t_ch := &reflect.MakeChan(reflect.Type(typ), buffer-1)
	p := t_ch.(unsafe.Pointer)
	ch = *p.(*chan interface{})
	go func() {
		var err error
		for err != nil {
			i := <-ch
			err = enc.Encode(i)
		}
	}()
	return
}
开发者ID:MetaParticle,项目名称:server,代码行数:14,代码来源:iochan_v2.go

示例5: Load

func (p *structPLS) Load(propMap PropertyMap) error {
	convFailures := errors.MultiError(nil)

	useExtra := false
	extra := (*PropertyMap)(nil)
	if i, ok := p.c.bySpecial["extra"]; ok {
		useExtra = true
		f := p.c.byIndex[i]
		if f.canSet {
			extra = p.o.Field(i).Addr().Interface().(*PropertyMap)
		}
	}
	t := reflect.Type(nil)
	for name, props := range propMap {
		multiple := len(props) > 1
		for i, prop := range props {
			if reason := loadInner(p.c, p.o, i, name, prop, multiple); reason != "" {
				if useExtra {
					if extra != nil {
						if *extra == nil {
							*extra = make(PropertyMap, 1)
						}
						(*extra)[name] = props
					}
					break // go to the next property in propMap
				} else {
					if t == nil {
						t = p.o.Type()
					}
					convFailures = append(convFailures, &ErrFieldMismatch{
						StructType: t,
						FieldName:  name,
						Reason:     reason,
					})
				}
			}
		}
	}

	if len(convFailures) > 0 {
		return convFailures
	}

	return nil
}
开发者ID:tetrafolium,项目名称:gae,代码行数:45,代码来源:pls_impl.go

示例6: getStructCodecLocked

// getStructCodecLocked implements getStructCodec. The structCodecsMutex must
// be held when calling this function.
func getStructCodecLocked(t reflect.Type) (ret *structCodec, retErr error) {
	c, ok := structCodecs[t]
	if ok {
		return c, nil
	}
	c = &structCodec{
		byIndex: make([]structTag, t.NumField()),
		byName:  make(map[string]fieldCodec),
	}

	// Add c to the structCodecs map before we are sure it is good. If t is
	// a recursive type, it needs to find the incomplete entry for itself in
	// the map.
	structCodecs[t] = c
	defer func() {
		if retErr != nil {
			delete(structCodecs, t)
		}
	}()

	for i := range c.byIndex {
		f := t.Field(i)
		name, opts := f.Tag.Get("datastore"), ""
		if i := strings.Index(name, ","); i != -1 {
			name, opts = name[:i], name[i+1:]
		}
		if name == "" {
			if !f.Anonymous {
				name = f.Name
			}
		} else if name == "-" {
			c.byIndex[i] = structTag{name: name}
			continue
		} else if !validPropertyName(name) {
			return nil, fmt.Errorf("datastore: struct tag has invalid property name: %q", name)
		}

		substructType, fIsSlice := reflect.Type(nil), false
		switch f.Type.Kind() {
		case reflect.Struct:
			substructType = f.Type
		case reflect.Slice:
			if f.Type.Elem().Kind() == reflect.Struct {
				substructType = f.Type.Elem()
			}
			fIsSlice = f.Type != typeOfByteSlice
			c.hasSlice = c.hasSlice || fIsSlice
		}

		if substructType != nil && substructType != typeOfTime {
			if name != "" {
				name = name + "."
			}
			sub, err := getStructCodecLocked(substructType)
			if err != nil {
				return nil, err
			}
			if !sub.complete {
				return nil, fmt.Errorf("datastore: recursive struct: field %q", f.Name)
			}
			if fIsSlice && sub.hasSlice {
				return nil, fmt.Errorf(
					"datastore: flattening nested structs leads to a slice of slices: field %q", f.Name)
			}
			c.hasSlice = c.hasSlice || sub.hasSlice
			for relName := range sub.byName {
				absName := name + relName
				if _, ok := c.byName[absName]; ok {
					return nil, fmt.Errorf("datastore: struct tag has repeated property name: %q", absName)
				}
				c.byName[absName] = fieldCodec{index: i, substructCodec: sub}
			}
		} else {
			if _, ok := c.byName[name]; ok {
				return nil, fmt.Errorf("datastore: struct tag has repeated property name: %q", name)
			}
			c.byName[name] = fieldCodec{index: i}
		}

		c.byIndex[i] = structTag{
			name:    name,
			noIndex: opts == "noindex",
		}
	}
	c.complete = true
	return c, nil
}
开发者ID:cjmagee,项目名称:howsmyssl,代码行数:89,代码来源:prop.go

示例7: getStructCodecLocked

func getStructCodecLocked(t reflect.Type) (c *structCodec) {
	if c, ok := structCodecs[t]; ok {
		return c
	}

	me := func(fmtStr string, args ...interface{}) error {
		return fmt.Errorf(fmtStr, args...)
	}

	c = &structCodec{
		byIndex:   make([]structTag, t.NumField()),
		byName:    make(map[string]int, t.NumField()),
		byMeta:    make(map[string]int, t.NumField()),
		bySpecial: make(map[string]int, 1),

		problem: errRecursiveStruct, // we'll clear this later if it's not recursive
	}
	defer func() {
		// If the codec has a problem, free up the indexes
		if c.problem != nil {
			c.byIndex = nil
			c.byName = nil
			c.byMeta = nil
		}
	}()
	structCodecs[t] = c

	for i := range c.byIndex {
		st := &c.byIndex[i]
		f := t.Field(i)
		ft := f.Type

		name := f.Tag.Get("gae")
		opts := ""
		if i := strings.Index(name, ","); i != -1 {
			name, opts = name[:i], name[i+1:]
		}
		st.canSet = f.PkgPath == "" // blank == exported
		if opts == "extra" {
			if _, ok := c.bySpecial["extra"]; ok {
				c.problem = me("struct has multiple fields tagged as 'extra'")
				return
			}
			if name != "" && name != "-" {
				c.problem = me("struct 'extra' field has invalid name %s, expecing `` or `-`", name)
				return
			}
			if ft != typeOfPropertyMap {
				c.problem = me("struct 'extra' field has invalid type %s, expecing PropertyMap", ft)
				return
			}
			st.isExtra = true
			st.name = name
			c.bySpecial["extra"] = i
			continue
		}
		st.convert = reflect.PtrTo(ft).Implements(typeOfPropertyConverter)
		switch {
		case name == "":
			if !f.Anonymous {
				name = f.Name
			}
		case name[0] == '$':
			name = name[1:]
			if _, ok := c.byMeta[name]; ok {
				c.problem = me("meta field %q set multiple times", "$"+name)
				return
			}
			c.byMeta[name] = i
			if !st.convert {
				mv, err := convertMeta(opts, ft)
				if err != nil {
					c.problem = me("meta field %q has bad type: %s", "$"+name, err)
					return
				}
				st.metaVal = mv
			}
			fallthrough
		case name == "-":
			st.name = "-"
			continue
		default:
			if !validPropertyName(name) {
				c.problem = me("struct tag has invalid property name: %q", name)
				return
			}
		}
		if !st.canSet {
			st.name = "-"
			continue
		}

		substructType := reflect.Type(nil)
		if !st.convert {
			switch ft.Kind() {
			case reflect.Struct:
				if ft != typeOfTime && ft != typeOfGeoPoint {
					substructType = ft
				}
			case reflect.Slice:
//.........这里部分代码省略.........
开发者ID:tetrafolium,项目名称:gae,代码行数:101,代码来源:pls_impl.go

示例8: getStructCodecLocked

// getStructCodecLocked implements getStructCodec. The structCodecsMutex must
// be held when calling this function.
func getStructCodecLocked(t reflect.Type) (ret *structCodec, retErr error) {
	c, ok := structCodecs[t]
	if ok {
		return c, nil
	}
	c = &structCodec{
		fields: make(map[string]fieldCodec),
		// We initialize keyField to -1 so that the zero-value is not
		// misinterpreted as index 0.
		keyField: -1,
	}

	// Add c to the structCodecs map before we are sure it is good. If t is
	// a recursive type, it needs to find the incomplete entry for itself in
	// the map.
	structCodecs[t] = c
	defer func() {
		if retErr != nil {
			delete(structCodecs, t)
		}
	}()

	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		// Skip unexported fields.
		// Note that if f is an anonymous, unexported struct field,
		// we will not promote its fields. We will skip f entirely.
		if f.PkgPath != "" {
			continue
		}

		tags := strings.Split(f.Tag.Get("datastore"), ",")
		name := tags[0]
		opts := make(map[string]bool)
		for _, t := range tags[1:] {
			opts[t] = true
		}
		switch {
		case name == "":
			if !f.Anonymous {
				name = f.Name
			}
		case name == "-":
			continue
		case name == "__key__":
			if f.Type != typeOfKeyPtr {
				return nil, fmt.Errorf("datastore: __key__ field on struct %v is not a *datastore.Key", t)
			}
			c.keyField = i
		case !validPropertyName(name):
			return nil, fmt.Errorf("datastore: struct tag has invalid property name: %q", name)
		}

		substructType, fIsSlice := reflect.Type(nil), false
		switch f.Type.Kind() {
		case reflect.Struct:
			substructType = f.Type
		case reflect.Slice:
			if f.Type.Elem().Kind() == reflect.Struct {
				substructType = f.Type.Elem()
			}
			fIsSlice = f.Type != typeOfByteSlice
			c.hasSlice = c.hasSlice || fIsSlice
		}

		var sub *structCodec
		if substructType != nil && substructType != typeOfTime && substructType != typeOfGeoPoint {
			var err error
			sub, err = getStructCodecLocked(substructType)
			if err != nil {
				return nil, err
			}
			if !sub.complete {
				return nil, fmt.Errorf("datastore: recursive struct: field %q", f.Name)
			}
			if fIsSlice && sub.hasSlice {
				return nil, fmt.Errorf(
					"datastore: flattening nested structs leads to a slice of slices: field %q", f.Name)
			}
			c.hasSlice = c.hasSlice || sub.hasSlice
			// If name is empty at this point, f is an anonymous struct field.
			// In this case, we promote the substruct's fields up to this level
			// in the linked list of struct codecs.
			if name == "" {
				for subname, subfield := range sub.fields {
					if _, ok := c.fields[subname]; ok {
						return nil, fmt.Errorf("datastore: struct tag has repeated property name: %q", subname)
					}
					c.fields[subname] = fieldCodec{
						path:        append([]int{i}, subfield.path...),
						noIndex:     subfield.noIndex || opts["noindex"],
						structCodec: subfield.structCodec,
					}
				}
				continue
			}
		}

//.........这里部分代码省略.........
开发者ID:golang,项目名称:gddo,代码行数:101,代码来源:prop.go

示例9: createSliceByType

func createSliceByType(t reflect.Type) interface{} {
	return reflect.New(reflect.Type(reflect.SliceOf(t))).Interface()
}
开发者ID:jianingy,项目名称:restle,代码行数:3,代码来源:utils.go


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