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 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 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 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 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 newList(p parser.Interface, typ reflect.Type) (reflect.Value, error) { list := reflect.MakeSlice(typ, 0, 0) for { if err := p.Next(); err != nil { if err == io.EOF { return list, nil } else { panic(err) } } if p.IsLeaf() { return reflect.ValueOf(nil), fmt.Errorf("list: did not expect leaf") } _, err := p.Int() if err != nil { return reflect.ValueOf(nil), err } elemType := typ.Elem() elemKind := elemType.Kind() elemIsPtr := false if elemKind == reflect.Ptr { elemIsPtr = true elemType = typ.Elem().Elem() elemKind = elemType.Kind() } p.Down() switch elemKind { case reflect.Struct: if !p.IsLeaf() { elem := reflect.New(elemType).Elem() if err := encodeStruct(p, elem); err != nil { return reflect.ValueOf(nil), err } if elemIsPtr { elem = elem.Addr() } list = reflect.Append(list, elem) } else { list = reflect.Append(list, reflect.Zero(typ.Elem())) } case reflect.Slice: newList, err := newList(p, elemType) if err != nil { return reflect.ValueOf(nil), err } list = reflect.Append(list, newList) default: elem, err := newValue(p, typ.Elem()) if err != nil { return reflect.ValueOf(nil), err } list = reflect.Append(list, elem) } p.Up() } }
func (this *sint64Field) 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.Int() if err != nil { return nil, 0, err } buf, offset = safeVarint(buf, offset, uint64((uint64(v)<<1)^uint64((v>>63)))) return buf, offset, nil }
func (this *fixed32IntField) 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.Int() if err != nil { return nil, 0, err } buf, offset = safeFixed32(buf, offset, uint32(v)) return buf, offset, nil }
func (this *repeatedField) 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 } } _, err := p.Int() if err != nil { return nil, 0, err } p.Down() buf, offset, err = this.fieldEnc.encode(buf, offset, p) if err != nil { return nil, 0, err } p.Up() } }