func encodeArray(v reflect.Value) interface{} { n := node{} for i := 0; i < v.Len(); i++ { n[strconv.Itoa(i)] = encodeValue(v.Index(i)) } return n }
func (w *Writer) writeMap(v interface{}, rv reflect.Value) (err error) { w.setRef(v) s := w.Stream if err = s.WriteByte(TagMap); err == nil { if count := rv.Len(); count > 0 { if err = w.writeInt(count); err == nil { if err = s.WriteByte(TagOpenbrace); err == nil { keys := rv.MapKeys() for i := range keys { if err = w.WriteValue(keys[i]); err != nil { return err } if err = w.WriteValue(rv.MapIndex(keys[i])); err != nil { return err } } err = s.WriteByte(TagClosebrace) } } } else if err = s.WriteByte(TagOpenbrace); err == nil { err = s.WriteByte(TagClosebrace) } } return err }
func (f *decFnInfo) kSlice(rv reflect.Value) { // A slice can be set from a map or array in stream. currEncodedType := f.dd.currentEncodedType() switch currEncodedType { case valueTypeBytes, valueTypeString: if f.ti.rtid == uint8SliceTypId || f.ti.rt.Elem().Kind() == reflect.Uint8 { if bs2, changed2 := f.dd.decodeBytes(rv.Bytes()); changed2 { rv.SetBytes(bs2) } return } } if shortCircuitReflectToFastPath && rv.CanAddr() { switch f.ti.rtid { case intfSliceTypId: f.d.decSliceIntf(rv.Addr().Interface().(*[]interface{}), currEncodedType, f.array) return case uint64SliceTypId: f.d.decSliceUint64(rv.Addr().Interface().(*[]uint64), currEncodedType, f.array) return case int64SliceTypId: f.d.decSliceInt64(rv.Addr().Interface().(*[]int64), currEncodedType, f.array) return case strSliceTypId: f.d.decSliceStr(rv.Addr().Interface().(*[]string), currEncodedType, f.array) return } } containerLen, containerLenS := decContLens(f.dd, currEncodedType) // an array can never return a nil slice. so no need to check f.array here. if rv.IsNil() { rv.Set(reflect.MakeSlice(f.ti.rt, containerLenS, containerLenS)) } if containerLen == 0 { return } if rvcap, rvlen := rv.Len(), rv.Cap(); containerLenS > rvcap { if f.array { // !rv.CanSet() decErr(msgDecCannotExpandArr, rvcap, containerLenS) } rvn := reflect.MakeSlice(f.ti.rt, containerLenS, containerLenS) if rvlen > 0 { reflect.Copy(rvn, rv) } rv.Set(rvn) } else if containerLenS > rvlen { rv.SetLen(containerLenS) } for j := 0; j < containerLenS; j++ { f.d.decodeValue(rv.Index(j)) } }
func (e *encoder) addSlice(v reflect.Value) { vi := v.Interface() if d, ok := vi.(D); ok { for _, elem := range d { e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) } return } if d, ok := vi.(RawD); ok { for _, elem := range d { e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) } return } l := v.Len() et := v.Type().Elem() if et == typeDocElem { for i := 0; i < l; i++ { elem := v.Index(i).Interface().(DocElem) e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) } return } if et == typeRawDocElem { for i := 0; i < l; i++ { elem := v.Index(i).Interface().(RawDocElem) e.addElem(elem.Name, reflect.ValueOf(elem.Value), false) } return } for i := 0; i < l; i++ { e.addElem(itoa(i), v.Index(i), false) } }
func (d *decoder) readArrayDocTo(out reflect.Value) { end := int(d.readInt32()) end += d.i - 4 if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' { corrupted() } i := 0 l := out.Len() for d.in[d.i] != '\x00' { if i >= l { panic("Length mismatch on array field") } kind := d.readByte() for d.i < end && d.in[d.i] != '\x00' { d.i++ } if d.i >= end { corrupted() } d.i++ d.readElemTo(out.Index(i), kind) if d.i >= end { corrupted() } i++ } if i != l { panic("Length mismatch on array field") } d.i++ // '\x00' if d.i != end { corrupted() } }
// callSliceRequired returns true if CallSlice is required instead of Call. func callSliceRequired(param reflect.Type, val reflect.Value) bool { vt := val.Type() for param.Kind() == reflect.Slice { if val.Kind() == reflect.Interface { val = reflect.ValueOf(val.Interface()) vt = val.Type() } if vt.Kind() != reflect.Slice { return false } vt = vt.Elem() if val.Kind() != reflect.Invalid { if val.Len() > 0 { val = val.Index(0) } else { val = reflect.Value{} } } param = param.Elem() } return true }
func decodeSliceValue(d *Decoder, v reflect.Value) error { n, err := d.DecodeArrayLen() if err != nil { return err } if n == -1 { v.Set(reflect.Zero(v.Type())) return nil } if n == 0 && v.IsNil() { v.Set(reflect.MakeSlice(v.Type(), 0, 0)) return nil } if v.Cap() >= n { v.Set(v.Slice(0, n)) } else if v.Len() < v.Cap() { v.Set(v.Slice(0, v.Cap())) } for i := 0; i < n; i++ { if i >= v.Len() { v.Set(growSliceValue(v, n)) } sv := v.Index(i) if err := d.DecodeValue(sv); err != nil { return err } } return nil }
func encodeMap(val reflect.Value) ([]byte, error) { var t = val.Type() if t.Key().Kind() != reflect.String { return nil, fmt.Errorf("xmlrpc encode error: only maps with string keys are supported") } var b bytes.Buffer b.WriteString("<struct>") keys := val.MapKeys() for i := 0; i < val.Len(); i++ { key := keys[i] kval := val.MapIndex(key) b.WriteString("<member>") b.WriteString(fmt.Sprintf("<name>%s</name>", key.String())) p, err := encodeValue(kval) if err != nil { return nil, err } b.Write(p) b.WriteString("</member>") } b.WriteString("</struct>") return b.Bytes(), nil }
// decodeArrayInterface decodes the source value into []interface{} func decodeArrayInterface(s *decodeState, sv reflect.Value) []interface{} { arr := []interface{}{} for i := 0; i < sv.Len(); i++ { arr = append(arr, decodeInterface(s, sv.Index(i))) } return arr }
// isZero reports whether the value is the zero of its type. func isZero(val reflect.Value) bool { switch val.Kind() { case reflect.Array: for i := 0; i < val.Len(); i++ { if !isZero(val.Index(i)) { return false } } return true case reflect.Map, reflect.Slice, reflect.String: return val.Len() == 0 case reflect.Bool: return !val.Bool() case reflect.Complex64, reflect.Complex128: return val.Complex() == 0 case reflect.Chan, reflect.Func, reflect.Ptr: return val.IsNil() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return val.Int() == 0 case reflect.Float32, reflect.Float64: return val.Float() == 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return val.Uint() == 0 case reflect.Struct: for i := 0; i < val.NumField(); i++ { if !isZero(val.Field(i)) { return false } } return true } panic("unknown type in isZero " + val.Type().String()) }
// tomlArrayType returns the element type of a TOML array. The type returned // may be nil if it cannot be determined (e.g., a nil slice or a zero length // slize). This function may also panic if it finds a type that cannot be // expressed in TOML (such as nil elements, heterogeneous arrays or directly // nested arrays of tables). func tomlArrayType(rv reflect.Value) tomlType { if isNil(rv) || !rv.IsValid() || rv.Len() == 0 { return nil } firstType := tomlTypeOfGo(rv.Index(0)) if firstType == nil { encPanic(errArrayNilElement) } rvlen := rv.Len() for i := 1; i < rvlen; i++ { elem := rv.Index(i) switch elemType := tomlTypeOfGo(elem); { case elemType == nil: encPanic(errArrayNilElement) case !typeEqual(firstType, elemType): encPanic(errArrayMixedElementTypes) } } // If we have a nested array, then we must make sure that the nested // array contains ONLY primitives. // This checks arbitrarily nested arrays. if typeEqual(firstType, tomlArray) || typeEqual(firstType, tomlArrayHash) { nest := tomlArrayType(eindirect(rv.Index(0))) if typeEqual(nest, tomlHash) || typeEqual(nest, tomlArrayHash) { encPanic(errArrayNoTable) } } return firstType }
// checkWhereArray handles the where-matching logic when the seqv value is an // Array or Slice. func checkWhereArray( seqv, kv, mv reflect.Value, path []string, op string) (interface{}, error) { rv := reflect.MakeSlice(seqv.Type(), 0, 0) for i := 0; i < seqv.Len(); i++ { var vvv reflect.Value rvv := seqv.Index(i) if kv.Kind() == reflect.String { vvv = rvv for _, elemName := range path { var err error vvv, err = evaluateSubElem(vvv, elemName) if err != nil { return nil, err } } } else { vv, _ := indirect(rvv) if vv.Kind() == reflect.Map && kv.Type().AssignableTo(vv.Type().Key()) { vvv = vv.MapIndex(kv) } } if ok, err := checkCondition(vvv, mv, op); ok { rv = reflect.Append(rv, rvv) } else if err != nil { return nil, err } } return rv.Interface(), nil }
func (e *encoder) slicev(tag string, in reflect.Value) { var ctag *C.yaml_char_t var free func() var cimplicit C.int if tag != "" { ctag, free = ystr(tag) defer free() cimplicit = 0 } else { cimplicit = 1 } cstyle := C.yaml_sequence_style_t(C.YAML_BLOCK_SEQUENCE_STYLE) if e.flow { e.flow = false cstyle = C.YAML_FLOW_SEQUENCE_STYLE } C.yaml_sequence_start_event_initialize(&e.event, nil, ctag, cimplicit, cstyle) e.emit() n := in.Len() for i := 0; i < n; i++ { e.marshal("", in.Index(i)) } C.yaml_sequence_end_event_initialize(&e.event) e.emit() }
// visit calls the visitor function for each string it finds, and will descend // recursively into structures and slices. If any visitor returns an error then // the search will stop and that error will be returned. func visit(path string, v reflect.Value, t reflect.Type, fn visitor) error { switch v.Kind() { case reflect.String: return fn(path, v) case reflect.Struct: for i := 0; i < v.NumField(); i++ { vf := v.Field(i) tf := t.Field(i) newPath := fmt.Sprintf("%s.%s", path, tf.Name) if err := visit(newPath, vf, tf.Type, fn); err != nil { return err } } case reflect.Slice: for i := 0; i < v.Len(); i++ { vi := v.Index(i) ti := vi.Type() newPath := fmt.Sprintf("%s[%d]", path, i) if err := visit(newPath, vi, ti, fn); err != nil { return err } } } return nil }
func encodeSliceContent(buf *bytes2.ChunkedWriter, val reflect.Value) { lenWriter := NewLenWriter(buf) for i := 0; i < val.Len(); i++ { encodeField(buf, Itoa(i), val.Index(i)) } lenWriter.Close() }
func plain_extract_value(name string, field reflect.Value) url.Values { values := make(url.Values) switch field.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: values.Add(name, strconv.FormatInt(field.Int(), 10)) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: values.Add(name, strconv.FormatUint(field.Uint(), 10)) case reflect.Bool: values.Add(name, strconv.FormatBool(field.Bool())) case reflect.Struct: values = merge(values, plain_extract_struct(field)) case reflect.Slice: for i := 0; i < field.Len(); i++ { sv := field.Index(i) values = merge(values, plain_extract_value(name, sv)) } case reflect.String: values.Add(name, field.String()) case reflect.Float32, reflect.Float64: values.Add(name, strconv.FormatFloat(field.Float(), 'f', -1, 64)) case reflect.Ptr, reflect.Interface: values = merge(values, plain_extract_pointer(field)) } return values }
func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error) { switch val.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return strconv.FormatInt(val.Int(), 10), nil, nil case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return strconv.FormatUint(val.Uint(), 10), nil, nil case reflect.Float32, reflect.Float64: return strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits()), nil, nil case reflect.String: return val.String(), nil, nil case reflect.Bool: return strconv.FormatBool(val.Bool()), nil, nil case reflect.Array: if typ.Elem().Kind() != reflect.Uint8 { break } // [...]byte var bytes []byte if val.CanAddr() { bytes = val.Slice(0, val.Len()).Bytes() } else { bytes = make([]byte, val.Len()) reflect.Copy(reflect.ValueOf(bytes), val) } return "", bytes, nil case reflect.Slice: if typ.Elem().Kind() != reflect.Uint8 { break } // []byte return "", val.Bytes(), nil } return "", nil, &UnsupportedTypeError{typ} }
func display(name string, v reflect.Value) { switch v.Kind() { //uint case reflect.Invalid: fmt.Printf("%s = invalid\n", name) case reflect.Slice, reflect.Array: for i := 0; i < v.Len(); i++ { display(fmt.Sprintf("%s[%d]", name, i), v.Index(i)) } case reflect.Struct: for i := 0; i < v.NumField(); i++ { fieldPath := fmt.Sprintf("%s.%s", name, v.Type().Field(i).Name) display(fieldPath, v.Field(i)) } case reflect.Map: for _, key := range v.MapKeys() { display(fmt.Sprintf("%s[%s]", name, formatAtom(key)), v.MapIndex(key)) } case reflect.Ptr: if v.IsNil() { fmt.Printf("%s = nil\n", name) } else { display(fmt.Sprintf("(*%s)", name), v.Elem()) } case reflect.Interface: if v.IsNil() { fmt.Printf("%s = nil\n", name) } else { fmt.Printf("%s.type = %s\n", name, v.Elem().Type()) display(name+".value", v.Elem()) } default: // basic types, channels, funcs fmt.Printf("%s = %s\n", name, formatAtom(v)) } }
func encodeByteArray(b []byte, v reflect.Value) []byte { n := v.Len() if n < (0xec - 0xe0) { b = append(b, byte(0xe0+n)) } else { b = encodeK4(b, 0xec, uint64(n)) } // Fast path for when the array is addressable (which it almost // always will be). if v.CanAddr() { return append(b, v.Slice(0, n).Bytes()...) } i := len(b) j := i + n if j > cap(b) { t := make([]byte, i, j) copy(t, b) b = t } reflect.Copy(reflect.ValueOf(b[i:j]), v) return b[:j] }
func assertValid(vObj reflect.Value) error { if !vObj.IsValid() { return nil } if obj, ok := vObj.Interface().(interface { AssertValid() error }); ok && !(vObj.Kind() == reflect.Ptr && vObj.IsNil()) { if err := obj.AssertValid(); err != nil { return err } } switch vObj.Kind() { case reflect.Ptr: return assertValid(vObj.Elem()) case reflect.Struct: return assertStructValid(vObj) case reflect.Slice: for i := 0; i < vObj.Len(); i++ { if err := assertValid(vObj.Index(i)); err != nil { return err } } } return nil }
func setSliceField(value reflect.Value, str string, ctx *context) error { if ctx.defaultVal != "" { return ErrDefaultUnsupportedOnSlice } elType := value.Type().Elem() tnz := newSliceTokenizer(str) slice := reflect.MakeSlice(value.Type(), value.Len(), value.Cap()) for tnz.scan() { token := tnz.text() el := reflect.New(elType).Elem() if err := parseValue(el, token, ctx); err != nil { return err } slice = reflect.Append(slice, el) } value.Set(slice) return tnz.Err() }
// isTrue reports whether the value is 'true', in the sense of not the zero of its type, // and whether the value has a meaningful truth value. func isTrue(val reflect.Value) (truth, ok bool) { if !val.IsValid() { // Something like var x interface{}, never set. It's a form of nil. return false, true } switch val.Kind() { case reflect.Array, reflect.Map, reflect.Slice, reflect.String: truth = val.Len() > 0 case reflect.Bool: truth = val.Bool() case reflect.Complex64, reflect.Complex128: truth = val.Complex() != 0 case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Interface: truth = !val.IsNil() case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: truth = val.Int() != 0 case reflect.Float32, reflect.Float64: truth = val.Float() != 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: truth = val.Uint() != 0 case reflect.Struct: truth = true // Struct values are always true. default: return } return truth, true }
func (q *queryParser) parseList(v url.Values, value reflect.Value, prefix string, tag reflect.StructTag) error { // If it's empty, generate an empty value if !value.IsNil() && value.Len() == 0 { v.Set(prefix, "") return nil } // check for unflattened list member if !q.isEC2 && tag.Get("flattened") == "" { prefix += ".member" } for i := 0; i < value.Len(); i++ { slicePrefix := prefix if slicePrefix == "" { slicePrefix = strconv.Itoa(i + 1) } else { slicePrefix = slicePrefix + "." + strconv.Itoa(i+1) } if err := q.parseValue(v, value.Index(i), slicePrefix, ""); err != nil { return err } } return nil }
func normalizeArray( opts *options, tagOpts tagOptions, ctx context, v reflect.Value, ) (value, Error) { l := v.Len() out := make([]value, 0, l) cfg := New() cfg.metadata = opts.meta cfg.ctx = ctx val := cfgSub{cfg} for i := 0; i < l; i++ { idx := fmt.Sprintf("%v", i) ctx := context{ parent: val, field: idx, } tmp, err := normalizeValue(opts, tagOpts, ctx, v.Index(i)) if err != nil { return nil, err } out = append(out, tmp) } cfg.fields.a = out return val, nil }
func isZero(v reflect.Value) bool { switch v.Kind() { case reflect.Func, reflect.Map, reflect.Slice: return v.IsNil() case reflect.Array: z := true for i := 0; i < v.Len(); i++ { z = z && isZero(v.Index(i)) } return z case reflect.Struct: if v.Type() == reflect.TypeOf(t) { if v.Interface().(time.Time).IsZero() { return true } return false } z := true for i := 0; i < v.NumField(); i++ { z = z && isZero(v.Field(i)) } return z } // Compare other types directly: z := reflect.Zero(v.Type()) return v.Interface() == z.Interface() }
func decComplex128Array(state *decoderState, v reflect.Value, length int, ovfl error) bool { // Can only slice if it is addressable. if !v.CanAddr() { return false } return decComplex128Slice(state, v.Slice(0, v.Len()), length, ovfl) }
// Validates that if a value is provided for a field, that value must be at // least a minimum length. func validateFieldMin(f reflect.StructField, fvalue reflect.Value) error { minStr := f.Tag.Get("min") if minStr == "" { return nil } min, _ := strconv.ParseInt(minStr, 10, 64) kind := fvalue.Kind() if kind == reflect.Ptr { if fvalue.IsNil() { return nil } fvalue = fvalue.Elem() } switch fvalue.Kind() { case reflect.String: if int64(fvalue.Len()) < min { return fmt.Errorf("field too short, minimum length %d", min) } case reflect.Slice, reflect.Map: if fvalue.IsNil() { return nil } if int64(fvalue.Len()) < min { return fmt.Errorf("field too short, minimum length %d", min) } // TODO min can also apply to number minimum value. } return nil }
func decodeSlice(v reflect.Value, x interface{}) { t := v.Type() if t.Elem().Kind() == reflect.Uint8 { // Allow, but don't require, byte slices to be encoded as a single string. if s, ok := x.(string); ok { v.SetBytes([]byte(s)) return } } // NOTE: Implicit indexing is currently done at the parseValues level, // so if if an implicitKey reaches here it will always replace the last. implicit := 0 for k, c := range getNode(x) { var i int if k == implicitKey { i = implicit implicit++ } else { explicit, err := strconv.Atoi(k) if err != nil { panic(k + " is not a valid index for type " + t.String()) } i = explicit implicit = explicit + 1 } // "Extend" the slice if it's too short. if l := v.Len(); i >= l { delta := i - l + 1 v.Set(reflect.AppendSlice(v, reflect.MakeSlice(t, delta, delta))) } decodeValue(v.Index(i), c) } }
func isZero(v reflect.Value) bool { switch v.Kind() { case reflect.String: return len(v.String()) == 0 case reflect.Interface, reflect.Ptr: return v.IsNil() case reflect.Slice: return v.Len() == 0 case reflect.Map: return v.Len() == 0 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return v.Int() == 0 case reflect.Float32, reflect.Float64: return v.Float() == 0 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: return v.Uint() == 0 case reflect.Bool: return !v.Bool() case reflect.Struct: vt := v.Type() for i := v.NumField() - 1; i >= 0; i-- { if vt.Field(i).PkgPath != "" { continue // Private field } if !isZero(v.Field(i)) { return false } } return true } return false }
func (f *Field) Unpack(buf []byte, val reflect.Value, length int, options *Options) error { typ := f.Type.Resolve(options) if typ == Pad || f.kind == reflect.String { if typ == Pad { return nil } else { val.SetString(string(buf)) return nil } } else if f.Slice { if val.Cap() < length { val.Set(reflect.MakeSlice(val.Type(), length, length)) } else if val.Len() < length { val.Set(val.Slice(0, length)) } // special case byte slices for performance if !f.Array && typ == Uint8 && f.defType == Uint8 { copy(val.Bytes(), buf[:length]) return nil } pos := 0 size := typ.Size() for i := 0; i < length; i++ { if err := f.unpackVal(buf[pos:pos+size], val.Index(i), 1, options); err != nil { return err } pos += size } return nil } else { return f.unpackVal(buf, val, length, options) } }