func encode(p parser.Interface) (interface{}, error) { var fields = make(map[string]interface{}) var list []interface{} for { if err := p.Next(); err != nil { if err == io.EOF { if len(fields) > 0 { return fields, nil } if list != nil { return list, nil } return nil, nil } return nil, err } if p.IsLeaf() { if v, err := p.Int(); err == nil { return v, nil } if v, err := p.Uint(); err == nil { return v, nil } if v, err := p.Double(); err == nil { return v, nil } if v, err := p.Bool(); err == nil { return v, nil } if v, err := p.String(); err == nil { return v, nil } if v, err := p.Bytes(); err == nil { return base64.StdEncoding.EncodeToString(v), nil } return nil, nil } if _, err := p.Int(); err == nil { p.Down() item, err := encode(p) if err != nil { return nil, err } p.Up() list = append(list, item) continue } name, err := p.String() if err != nil { return nil, err } p.Down() value, err := encode(p) if err != nil { return nil, err } p.Up() fields[name] = value } }
func getCharData(p parser.Interface) xml.CharData { i, err := p.Int() if err == nil { return xml.CharData([]byte(strconv.FormatInt(i, 10))) } u, err := p.Uint() if err == nil { return xml.CharData([]byte(strconv.FormatUint(u, 10))) } d, err := p.Double() if err == nil { return xml.CharData([]byte(strconv.FormatFloat(d, 'e', -1, 64))) } b, err := p.Bool() if err == nil { return xml.CharData([]byte(strconv.FormatBool(b))) } s, err := p.String() if err == nil { return xml.CharData([]byte(s)) } v, err := p.Bytes() if err == nil { return xml.CharData([]byte(base64.StdEncoding.EncodeToString(v))) } return nil }
func getName(p parser.Interface) xml.Name { i, err := p.Int() if err == nil { return xml.Name{Local: strconv.FormatInt(i, 10)} } u, err := p.Uint() if err == nil { return xml.Name{Local: strconv.FormatUint(u, 10)} } d, err := p.Double() if err == nil { return xml.Name{Local: strconv.FormatFloat(d, 'e', -1, 64)} } b, err := p.Bool() if err == nil { return xml.Name{Local: strconv.FormatBool(b)} } s, err := p.String() if err == nil { return xml.Name{Local: s} } v, err := p.Bytes() if err == nil { return xml.Name{Local: base64.StdEncoding.EncodeToString(v)} } return xml.Name{} }
func newValue(p parser.Interface, typ reflect.Type) (reflect.Value, error) { if err := p.Next(); err != nil { if err == io.EOF { return reflect.ValueOf(nil), nil } else { panic(err) } } if !p.IsLeaf() { return reflect.ValueOf(nil), fmt.Errorf("expected leaf") } if value, err := p.Int(); err == nil { return reflect.ValueOf(value), nil } if value, err := p.Uint(); err == nil { return reflect.ValueOf(value), nil } if value, err := p.Double(); err == nil { return reflect.ValueOf(value), nil } if value, err := p.Bool(); err == nil { return reflect.ValueOf(value), nil } if value, err := p.String(); err == nil { return reflect.ValueOf(value), nil } if value, err := p.Bytes(); err == nil { return reflect.ValueOf(value), nil } return reflect.ValueOf(nil), nil }
func getValue(p parser.Interface) interface{} { var v interface{} var err error v, err = p.Int() if err == nil { return v } v, err = p.Uint() if err == nil { return v } v, err = p.Double() if err == nil { return v } v, err = p.Bool() if err == nil { return v } v, err = p.String() if err == nil { return v } v, err = p.Bytes() if err == nil { return v } return nil }
func (this *msg) encode(buf []byte, offset int, p parser.Interface) ([]byte, int, error) { for { if err := p.Next(); err != nil { if err == io.EOF { return buf, offset, nil } else { return nil, 0, err } } name, err := p.String() if err != nil { return nil, 0, err } fieldEnc, ok := this.fieldEncoders[name] if !ok { continue //skip field } p.Down() buf, offset, err = fieldEnc.encode(buf, offset, p) if err != nil { return nil, 0, err } p.Up() } }
func (this *stringField) encode(buf []byte, offset int, p parser.Interface) ([]byte, int, error) { if err := p.Next(); err != nil { return buf, offset, nil } if !p.IsLeaf() { return buf, offset, nil } buf, offset = safeCopy(buf, offset, this.key) v, err := p.String() if err != nil { return nil, 0, err } vb := []byte(v) buf, offset = safeVarint(buf, offset, uint64(len(vb))) buf, offset = safeCopy(buf, offset, vb) return buf, offset, nil }
func encodeStruct(p parser.Interface, v reflect.Value) error { kind := v.Type().Kind() isPtr := false if kind == reflect.Ptr { kind = v.Type().Elem().Kind() isPtr = true } if kind != reflect.Struct { return fmt.Errorf("expected struct") } for { if err := p.Next(); err != nil { if err == io.EOF { return nil } else { panic(err) } } if p.IsLeaf() { return fmt.Errorf("struct: did not expect leaf") } name, err := p.String() if err != nil { return err } strct := v if isPtr { strct = v.Elem() } field := strct.FieldByName(name) if !field.IsValid() { continue //skip field } fieldKind := field.Kind() fieldIsPtr := false fieldType := field.Type() if fieldKind == reflect.Ptr { fieldIsPtr = true fieldKind = field.Type().Elem().Kind() fieldType = field.Type().Elem() } p.Down() switch fieldKind { case reflect.Struct: if !p.IsLeaf() { if fieldIsPtr { field.Set(reflect.New(field.Type().Elem())) } if err := encodeStruct(p, field); err != nil { return err } } case reflect.Slice: list, err := newList(p, fieldType) if err != nil { return err } field.Set(list) default: value, err := newValue(p, fieldType) if err != nil { return err } field.Set(value) } p.Up() } }