示例#1
0
文件: xml.go 项目: katydid/katydid
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{}
}
示例#2
0
文件: json.go 项目: katydid/katydid
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
	}
}
示例#3
0
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
}
示例#4
0
文件: xml.go 项目: katydid/katydid
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
}
示例#5
0
文件: walk.go 项目: katydid/katydid
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
}
示例#6
0
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()
	}
}
示例#7
0
文件: proto.go 项目: katydid/katydid
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
}
示例#8
0
文件: proto.go 项目: katydid/katydid
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
}
示例#9
0
文件: proto.go 项目: katydid/katydid
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()
	}
}