本文整理汇总了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
}
示例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")
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}