當前位置: 首頁>>代碼示例>>Golang>>正文


Golang reflect.StructValue類代碼示例

本文整理匯總了Golang中reflect.StructValue的典型用法代碼示例。如果您正苦於以下問題:Golang StructValue類的具體用法?Golang StructValue怎麽用?Golang StructValue使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了StructValue類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: enc_struct

func enc_struct(val *reflect.StructValue, typ *reflect.StructType, buffer *bytes.Buffer) os.Error {
	buffer.WriteString("{")
	first := true
	for i := 0; i < typ.NumField(); i++ {
		f := typ.Field(i)
		if f.Tag == "" {
			continue
		}
		tags := strings.Split(f.Tag[3:len(f.Tag)-1], ",", -1)
		l := buffer.Len()
		if first {
			buffer.WriteString(fmt.Sprintf("\"%v\":", tags[1]))
		} else {
			buffer.WriteString(fmt.Sprintf(",\"%v\":", tags[1]))
		}
		written, err := enc(val.Field(i), f.Type, buffer, tags[2] == "req")
		if err != nil {
			return err
		}
		if !written {
			buffer.Truncate(l)
		} else {
			first = false
		}
	}
	buffer.WriteString("}")
	return nil
}
開發者ID:AaronO,項目名稱:lightwave,代碼行數:28,代碼來源:jsonprotobuf.go

示例2: unmarshalPaths

// unmarshalPaths walks down an XML structure looking for
// wanted paths, and calls unmarshal on them.
func (p *Parser) unmarshalPaths(sv *reflect.StructValue, paths map[string]pathInfo, path string, start *StartElement) os.Error {
	if info, _ := paths[path]; info.complete {
		return p.unmarshal(sv.FieldByIndex(info.fieldIdx), start)
	}
	for {
		tok, err := p.Token()
		if err != nil {
			return err
		}
		switch t := tok.(type) {
		case StartElement:
			k := path + ">" + fieldName(t.Name.Local)
			if _, found := paths[k]; found {
				if err := p.unmarshalPaths(sv, paths, k, &t); err != nil {
					return err
				}
				continue
			}
			if err := p.Skip(); err != nil {
				return err
			}
		case EndElement:
			return nil
		}
	}
	panic("unreachable")
}
開發者ID:IntegerCompany,項目名稱:linaro-android-gcc,代碼行數:29,代碼來源:read.go

示例3: 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

示例4: 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

示例5: 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

示例6: getField

// Get the i'th arg of the struct value.
// If the arg itself is an interface, return a value for
// the thing inside the interface, not the interface itself.
func getField(v *reflect.StructValue, i int) reflect.Value {
	val := v.Field(i)
	if i, ok := val.(*reflect.InterfaceValue); ok {
		if inter := i.Interface(); inter != nil {
			return reflect.NewValue(inter)
		}
	}
	return val
}
開發者ID:lougxing,項目名稱:golang-china,代碼行數:12,代碼來源:print.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: encodeStruct

func encodeStruct(w io.Writer, v *reflect.StructValue) (err os.Error) {
	n := v.NumField()
	if err = printfLine(w, "*%d", n); err != nil {
		return
	}
	for i := 0; i < n; i++ {
		if err = encode(w, v.Field(i).Interface()); err != nil {
			return
		}
	}
	return nil
}
開發者ID:andradeandrey,項目名稱:doozer,代碼行數:12,代碼來源:proto.go

示例9: ToByteSliceArray

func ToByteSliceArray(v *reflect.StructValue) (bsa [][]byte, ok bool) {
	n := v.NumField()
	bsa = make([][]byte, n)
	for i := 0; i < n; i++ {
		bsa[i], ok = GetByteArrayAtIndex(v, i)
		if !ok {
			if debug() {
			}
			break
		}
	}
	return
}
開發者ID:nono,項目名稱:Go-Redis,代碼行數:13,代碼來源:reflect.go

示例10: makeVector3

// Makes a Vector3 StateValue. Panics if the StructValue fields do not match the vector.
func makeVector3(v *reflect.StructValue) *protocol.StateValue {
	// If we panic here, struct layout was not as expected
	x := v.FieldByName("X").(*reflect.FloatValue).Get()
	y := v.FieldByName("Y").(*reflect.FloatValue).Get()
	z := v.FieldByName("Z").(*reflect.FloatValue).Get()

	vector3 := &protocol.Vector3{&x, &y, &z, nil}
	sv := &protocol.StateValue{
		Type:       protocol.NewStateValue_Type(protocol.StateValue_VECTOR3),
		Vector3Val: vector3,
	}
	return sv
}
開發者ID:archwyrm,項目名稱:ghack,代碼行數:14,代碼來源:statevalue.go

示例11: translateMap

func (m *Model) translateMap(obj *reflect.StructValue) map[string]Value {
	ret := make(map[string]Value)
	for attr, typ := range m.attributes {
		switch typ.(type) {
		case *reflect.IntType:
			ret[attr] = SysInt(obj.FieldByName(attr).(*reflect.IntValue).Get()).Value()
		case *reflect.StringType:
			ret[attr] = SysString(obj.FieldByName(attr).(*reflect.StringValue).Get()).Value()
		case nil:
			ret[attr] = new(_Null)
		}
	}
	return ret
}
開發者ID:zetaben,項目名稱:gouda,代碼行數:14,代碼來源:Model.go

示例12: 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

示例13: buildUpdateMap

func (m *Model) buildUpdateMap(st *reflect.StructValue, old map[string]Value) map[string]Value {
	ret := make(map[string]Value)
	for attr, typ := range m.attributes {
		switch typ.(type) {
		case *reflect.IntType:
			if tmp := st.FieldByName(attr).(*reflect.IntValue).Get(); int(old[strings.ToLower(attr)].Int()) != tmp {
				ret[attr] = SysInt(tmp).Value()
			}
		case *reflect.StringType:
			if tmp := st.FieldByName(attr).(*reflect.StringValue).Get(); string(old[strings.ToLower(attr)].String()) != tmp {
				ret[attr] = SysString(tmp).Value()
			}
		}
	}
	return ret
}
開發者ID:zetaben,項目名稱:gouda,代碼行數:16,代碼來源:Model.go

示例14: fuzzyEqualStruct

func fuzzyEqualStruct(a *reflect.StructValue, b *reflect.StructValue) bool {
	numA, numB := a.NumField(), b.NumField()
	if numA != numB {
		return false
	}

	for i := 0; i < numA; i++ {
		if !fuzzyEqualValue(a.Field(i), b.Field(i)) {
			return false
		}
	}
	return true
}
開發者ID:royger,項目名稱:wgo,代碼行數:13,代碼來源:bencode_test.go

示例15: dec_struct

func dec_struct(val *reflect.StructValue, typ *reflect.StructType, json map[string]interface{}) os.Error {
	for i := 0; i < typ.NumField(); i++ {
		f := typ.Field(i)
		if f.Tag == "" {
			continue
		}
		tags := strings.Split(f.Tag[3:len(f.Tag)-1], ",", -1)
		j, ok := json[tags[1]]
		if !ok {
			if tags[2] == "req" {
				return os.NewError("Field " + f.Name + " is missing")
			}
			continue
		}
		err := dec(val.Field(i), f.Type, j)
		if err != nil {
			return err
		}
	}
	return nil
}
開發者ID:AaronO,項目名稱:lightwave,代碼行數:21,代碼來源:jsonprotobuf.go


注:本文中的reflect.StructValue類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。