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


Golang StructValue.Type方法代码示例

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


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

示例1: printStructValue

// Generic struct printer.
// Doesn't care about the string tag "domain-name",
// but does look for an "ipv4" tag on uint32 variables
// and the "ipv6" tag on array variables,
// printing them as IP addresses.
func printStructValue(val *reflect.StructValue) string {
	s := "{"
	for i := 0; i < val.NumField(); i++ {
		if i > 0 {
			s += ", "
		}
		f := val.Type().(*reflect.StructType).Field(i)
		if !f.Anonymous {
			s += f.Name + "="
		}
		fval := val.Field(i)
		if fv, ok := fval.(*reflect.StructValue); ok {
			s += printStructValue(fv)
		} else if fv, ok := fval.(*reflect.UintValue); ok && f.Tag == "ipv4" {
			i := fv.Get()
			s += IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i)).String()
		} else if fv, ok := fval.(*reflect.ArrayValue); ok && f.Tag == "ipv6" {
			i := fv.Interface().([]byte)
			s += IP(i).String()
		} else {
			s += fmt.Sprint(fval.Interface())
		}
	}
	s += "}"
	return s
}
开发者ID:richlowe,项目名称:gcc,代码行数:31,代码来源:dnsmsg.go

示例2: writeStruct

func writeStruct(w io.Writer, val *reflect.StructValue) (err os.Error) {
	if _, err = fmt.Fprint(w, "{"); err != nil {
		return
	}

	typ := val.Type().(*reflect.StructType)

	for i := 0; i < val.NumField(); i++ {
		fieldValue := val.Field(i)
		if _, err = fmt.Fprintf(w, "%s:", Quote(typ.Field(i).Name)); err != nil {
			return
		}
		if err = writeValue(w, fieldValue); err != nil {
			return
		}
		if i < val.NumField()-1 {
			if _, err = fmt.Fprint(w, ","); err != nil {
				return
			}
		}
	}

	_, err = fmt.Fprint(w, "}")
	return
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:25,代码来源:struct.go

示例3: writeStruct

func writeStruct(w io.Writer, val *reflect.StructValue) (err os.Error) {
	_, err = fmt.Fprint(w, "d")
	if err != nil {
		return
	}

	typ := val.Type().(*reflect.StructType)

	numFields := val.NumField()
	svList := make(StringValueArray, numFields)

	for i := 0; i < numFields; i++ {
		field := typ.Field(i)
		key := field.Name
		if len(field.Tag) > 0 {
			key = field.Tag
		}
		svList[i].key = key
		svList[i].value = val.Field(i)
	}

	err = writeSVList(w, svList)
	if err != nil {
		return
	}

	_, err = fmt.Fprint(w, "e")
	if err != nil {
		return
	}
	return
}
开发者ID:quantumelixir,项目名称:Taipei-Torrent,代码行数:32,代码来源:struct.go

示例4: writeStruct

func (e *encodeState) writeStruct(v *reflect.StructValue) {
	offset := e.beginDoc()
	for name, f := range compileStruct(v.Type().(*reflect.StructType)) {
		e.encodeValue(name, v.FieldByIndex(f.Index))
	}
	e.WriteByte(0)
	e.endDoc(offset)
}
开发者ID:hokapoka,项目名称:go-mongo,代码行数:8,代码来源:bson_encode.go

示例5: unpackStructValue

// TODO(rsc): Move into generic library?
// Unpack a reflect.StructValue from msg.
// Same restrictions as packStructValue.
func unpackStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, ok bool) {
	for i := 0; i < val.NumField(); i++ {
		f := val.Type().(*reflect.StructType).Field(i)
		switch fv := val.Field(i).(type) {
		default:
		BadType:
			fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", f.Type)
			return len(msg), false
		case *reflect.StructValue:
			off, ok = unpackStructValue(fv, msg, off)
		case *reflect.UintValue:
			switch fv.Type().Kind() {
			default:
				goto BadType
			case reflect.Uint16:
				if off+2 > len(msg) {
					return len(msg), false
				}
				i := uint16(msg[off])<<8 | uint16(msg[off+1])
				fv.Set(uint64(i))
				off += 2
			case reflect.Uint32:
				if off+4 > len(msg) {
					return len(msg), false
				}
				i := uint32(msg[off])<<24 | uint32(msg[off+1])<<16 | uint32(msg[off+2])<<8 | uint32(msg[off+3])
				fv.Set(uint64(i))
				off += 4
			}
		case *reflect.StringValue:
			var s string
			switch f.Tag {
			default:
				fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", f.Tag)
				return len(msg), false
			case "domain-name":
				s, off, ok = unpackDomainName(msg, off)
				if !ok {
					return len(msg), false
				}
			case "":
				if off >= len(msg) || off+1+int(msg[off]) > len(msg) {
					return len(msg), false
				}
				n := int(msg[off])
				off++
				b := make([]byte, n)
				for i := 0; i < n; i++ {
					b[i] = msg[off+i]
				}
				off += n
				s = string(b)
			}
			fv.Set(s)
		}
	}
	return off, true
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:61,代码来源:dnsmsg.go

示例6: packStructValue

// TODO(rsc): Move into generic library?
// Pack a reflect.StructValue into msg.  Struct members can only be uint16, uint32, string,
// and other (often anonymous) structs.
func packStructValue(val *reflect.StructValue, msg []byte, off int) (off1 int, ok bool) {
	for i := 0; i < val.NumField(); i++ {
		f := val.Type().(*reflect.StructType).Field(i)
		switch fv := val.Field(i).(type) {
		default:
		BadType:
			fmt.Fprintf(os.Stderr, "net: dns: unknown packing type %v", f.Type)
			return len(msg), false
		case *reflect.StructValue:
			off, ok = packStructValue(fv, msg, off)
		case *reflect.UintValue:
			i := fv.Get()
			switch fv.Type().Kind() {
			default:
				goto BadType
			case reflect.Uint16:
				if off+2 > len(msg) {
					return len(msg), false
				}
				msg[off] = byte(i >> 8)
				msg[off+1] = byte(i)
				off += 2
			case reflect.Uint32:
				if off+4 > len(msg) {
					return len(msg), false
				}
				msg[off] = byte(i >> 24)
				msg[off+1] = byte(i >> 16)
				msg[off+2] = byte(i >> 8)
				msg[off+3] = byte(i)
				off += 4
			}
		case *reflect.StringValue:
			// There are multiple string encodings.
			// The tag distinguishes ordinary strings from domain names.
			s := fv.Get()
			switch f.Tag {
			default:
				fmt.Fprintf(os.Stderr, "net: dns: unknown string tag %v", f.Tag)
				return len(msg), false
			case "domain-name":
				off, ok = packDomainName(s, msg, off)
				if !ok {
					return len(msg), false
				}
			case "":
				// Counted string: 1 byte length.
				if len(s) > 255 || off+1+len(s) > len(msg) {
					return len(msg), false
				}
				msg[off] = byte(len(s))
				off++
				off += copy(msg[off:], s)
			}
		}
	}
	return off, true
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:61,代码来源:dnsmsg.go

示例7: readStructField

// Reads a field that is not a builtin type, but a user created struct. These
// types have specific message types so the receiving end can identify them.
func readStructField(val *reflect.StructValue) *protocol.StateValue {
	t := val.Type()
	switch t.Name() {
	case "V3": // Vector type:
		return makeVector3(val)
	}
	panic("Struct value not supported: " + t.String())
	return nil // Will never get here
}
开发者ID:archwyrm,项目名称:ghack,代码行数:11,代码来源:statevalue.go

示例8: writeStruct

func writeStruct(w io.Writer, val *reflect.StructValue) os.Error {
	fmt.Fprint(w, "{")

	typ := val.Type().(*reflect.StructType)

	for i := 0; i < val.NumField(); i++ {
		fieldValue := val.Field(i)
		fmt.Fprintf(w, "%q:", typ.Field(i).Name)
		if err := writeValue(w, fieldValue); err != nil {
			return err
		}
		if i < val.NumField()-1 {
			fmt.Fprint(w, ",")
		}
	}

	fmt.Fprint(w, "}")
	return nil
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:19,代码来源:struct.go

示例9: unmarshal

// Unmarshal a single XML element into val.
func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
	// Find start element if we need it.
	if start == nil {
		for {
			tok, err := p.Token()
			if err != nil {
				return err
			}
			if t, ok := tok.(StartElement); ok {
				start = &t
				break
			}
		}
	}

	if pv, ok := val.(*reflect.PtrValue); ok {
		if pv.Get() == 0 {
			zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem())
			pv.PointTo(zv)
			val = zv
		} else {
			val = pv.Elem()
		}
	}

	var (
		data        []byte
		saveData    reflect.Value
		comment     []byte
		saveComment reflect.Value
		sv          *reflect.StructValue
		styp        *reflect.StructType
	)
	switch v := val.(type) {
	default:
		return os.ErrorString("unknown type " + v.Type().String())

	case *reflect.BoolValue:
		v.Set(true)

	case *reflect.SliceValue:
		typ := v.Type().(*reflect.SliceType)
		if _, ok := typ.Elem().(*reflect.Uint8Type); ok {
			// []byte
			saveData = v
			break
		}

		// Slice of element values.
		// Grow slice.
		n := v.Len()
		if n >= v.Cap() {
			ncap := 2 * n
			if ncap < 4 {
				ncap = 4
			}
			new := reflect.MakeSlice(typ, n, ncap)
			reflect.ArrayCopy(new, v)
			v.Set(new)
		}
		v.SetLen(n + 1)

		// Recur to read element into slice.
		if err := p.unmarshal(v.Elem(n), start); err != nil {
			v.SetLen(n)
			return err
		}
		return nil

	case *reflect.StringValue,
		*reflect.IntValue, *reflect.UintValue, *reflect.UintptrValue,
		*reflect.Int8Value, *reflect.Int16Value, *reflect.Int32Value, *reflect.Int64Value,
		*reflect.Uint8Value, *reflect.Uint16Value, *reflect.Uint32Value, *reflect.Uint64Value,
		*reflect.FloatValue, *reflect.Float32Value, *reflect.Float64Value:
		saveData = v

	case *reflect.StructValue:
		if _, ok := v.Interface().(Name); ok {
			v.Set(reflect.NewValue(start.Name).(*reflect.StructValue))
			break
		}

		sv = v
		typ := sv.Type().(*reflect.StructType)
		styp = typ
		// Assign name.
		if f, ok := typ.FieldByName("XMLName"); ok {
			// Validate element name.
			if f.Tag != "" {
				tag := f.Tag
				ns := ""
				i := strings.LastIndex(tag, " ")
				if i >= 0 {
					ns, tag = tag[0:i], tag[i+1:]
				}
				if tag != start.Name.Local {
					return UnmarshalError("expected element type <" + tag + "> but have <" + start.Name.Local + ">")
				}
				if ns != "" && ns != start.Name.Space {
//.........这里部分代码省略.........
开发者ID:lougxing,项目名称:golang-china,代码行数:101,代码来源:read.go

示例10: object

// object consumes an object from d.data[d.off-1:], decoding into the value v.
// the first byte of the object ('{') has been read already.
func (d *decodeState) object(v reflect.Value) {
	// Check for unmarshaler.
	unmarshaler, pv := d.indirect(v, false)
	if unmarshaler != nil {
		d.off--
		err := unmarshaler.UnmarshalJSON(d.next())
		if err != nil {
			d.error(err)
		}
		return
	}
	v = pv

	// Decoding into nil interface?  Switch to non-reflect code.
	iv, ok := v.(*reflect.InterfaceValue)
	if ok {
		iv.Set(reflect.NewValue(d.objectInterface()))
		return
	}

	// Check type of target: struct or map[string]T
	var (
		mv *reflect.MapValue
		sv *reflect.StructValue
	)
	switch v := v.(type) {
	case *reflect.MapValue:
		// map must have string type
		t := v.Type().(*reflect.MapType)
		if t.Key() != reflect.Typeof("") {
			d.saveError(&UnmarshalTypeError{"object", v.Type()})
			break
		}
		mv = v
		if mv.IsNil() {
			mv.SetValue(reflect.MakeMap(t))
		}
	case *reflect.StructValue:
		sv = v
	default:
		d.saveError(&UnmarshalTypeError{"object", v.Type()})
	}

	if mv == nil && sv == nil {
		d.off--
		d.next() // skip over { } in input
		return
	}

	for {
		// Read opening " of string key or closing }.
		op := d.scanWhile(scanSkipSpace)
		if op == scanEndObject {
			// closing } - can only happen on first iteration.
			break
		}
		if op != scanBeginLiteral {
			d.error(errPhase)
		}

		// Read string key.
		start := d.off - 1
		op = d.scanWhile(scanContinue)
		item := d.data[start : d.off-1]
		key, ok := unquote(item)
		if !ok {
			d.error(errPhase)
		}

		// Figure out field corresponding to key.
		var subv reflect.Value
		if mv != nil {
			subv = reflect.MakeZero(mv.Type().(*reflect.MapType).Elem())
		} else {
			// First try for field with that tag.
			for i := 0; i < sv.NumField(); i++ {
				f := sv.Type().(*reflect.StructType).Field(i)
				if f.Tag == key {
					subv = sv.Field(i)
					break
				}
			}
			if subv == nil {
				// Second, exact match.
				subv = sv.FieldByName(key)
				if subv == nil {
					// Third, case-insensitive match.
					subv = sv.FieldByNameFunc(func(s string) bool { return matchName(key, s) })
				}
			}
		}

		// Read : before value.
		if op == scanSkipSpace {
			op = d.scanWhile(scanSkipSpace)
		}
		if op != scanObjectKey {
			d.error(errPhase)
//.........这里部分代码省略.........
开发者ID:rapgamer,项目名称:golang-china,代码行数:101,代码来源:decode.go

示例11: tagError

func tagError(sv *reflect.StructValue, idx1 []int, idx2 []int) os.Error {
	t := sv.Type().(*reflect.StructType)
	f1 := t.FieldByIndex(idx1)
	f2 := t.FieldByIndex(idx2)
	return &TagPathError{t, f1.Name, f1.Tag, f2.Name, f2.Tag}
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:6,代码来源:read.go

示例12: unmarshal

// Unmarshal a single XML element into val.
func (p *Parser) unmarshal(val reflect.Value, start *StartElement) os.Error {
	// Find start element if we need it.
	if start == nil {
		for {
			tok, err := p.Token()
			if err != nil {
				return err
			}
			if t, ok := tok.(StartElement); ok {
				start = &t
				break
			}
		}
	}

	if pv, ok := val.(*reflect.PtrValue); ok {
		if pv.Get() == 0 {
			zv := reflect.MakeZero(pv.Type().(*reflect.PtrType).Elem())
			pv.PointTo(zv)
			val = zv
		} else {
			val = pv.Elem()
		}
	}

	var (
		data         []byte
		saveData     reflect.Value
		comment      []byte
		saveComment  reflect.Value
		saveXML      reflect.Value
		saveXMLIndex int
		saveXMLData  []byte
		sv           *reflect.StructValue
		styp         *reflect.StructType
		fieldPaths   map[string]pathInfo
	)

	switch v := val.(type) {
	default:
		return os.ErrorString("unknown type " + v.Type().String())

	case *reflect.SliceValue:
		typ := v.Type().(*reflect.SliceType)
		if typ.Elem().Kind() == reflect.Uint8 {
			// []byte
			saveData = v
			break
		}

		// Slice of element values.
		// Grow slice.
		n := v.Len()
		if n >= v.Cap() {
			ncap := 2 * n
			if ncap < 4 {
				ncap = 4
			}
			new := reflect.MakeSlice(typ, n, ncap)
			reflect.Copy(new, v)
			v.Set(new)
		}
		v.SetLen(n + 1)

		// Recur to read element into slice.
		if err := p.unmarshal(v.Elem(n), start); err != nil {
			v.SetLen(n)
			return err
		}
		return nil

	case *reflect.BoolValue, *reflect.FloatValue, *reflect.IntValue, *reflect.UintValue, *reflect.StringValue:
		saveData = v

	case *reflect.StructValue:
		if _, ok := v.Interface().(Name); ok {
			v.Set(reflect.NewValue(start.Name).(*reflect.StructValue))
			break
		}

		sv = v
		typ := sv.Type().(*reflect.StructType)
		styp = typ
		// Assign name.
		if f, ok := typ.FieldByName("XMLName"); ok {
			// Validate element name.
			if f.Tag != "" {
				tag := f.Tag
				ns := ""
				i := strings.LastIndex(tag, " ")
				if i >= 0 {
					ns, tag = tag[0:i], tag[i+1:]
				}
				if tag != start.Name.Local {
					return UnmarshalError("expected element type <" + tag + "> but have <" + start.Name.Local + ">")
				}
				if ns != "" && ns != start.Name.Space {
					e := "expected element <" + tag + "> in name space " + ns + " but have "
					if start.Name.Space == "" {
//.........这里部分代码省略.........
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:101,代码来源:read.go


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