本文整理汇总了Golang中reflect.Value.SetLen方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.SetLen方法的具体用法?Golang Value.SetLen怎么用?Golang Value.SetLen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.SetLen方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: decodeSlice
func decodeSlice(d *decodeState, kind int, v reflect.Value) {
if kind != kindArray {
d.saveErrorAndSkip(kind, v.Type())
return
}
t := v.Type()
offset := d.beginDoc()
i := 0
for {
kind, _ := d.scanKindName()
if kind == 0 {
break
}
if i >= v.Cap() {
newcap := v.Cap() + v.Cap()/2
if newcap < 4 {
newcap = 4
}
newv := reflect.MakeSlice(t, v.Len(), newcap)
reflect.Copy(newv, v)
v.Set(newv)
}
if i >= v.Len() {
v.SetLen(i + 1)
}
d.decodeValue(kind, v.Index(i))
i += 1
}
d.endDoc(offset)
}
示例2: kSlice
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))
}
}
示例3: setRepeated
func setRepeated(field reflect.Value, vslice []Value, setElem setFunc) error {
vlen := len(vslice)
var flen int
switch field.Type().Kind() {
case reflect.Slice:
// Make a slice of the right size, avoiding allocation if possible.
switch {
case field.Len() < vlen:
field.Set(reflect.MakeSlice(field.Type(), vlen, vlen))
case field.Len() > vlen:
field.SetLen(vlen)
}
flen = vlen
case reflect.Array:
flen = field.Len()
if flen > vlen {
// Set extra elements to their zero value.
z := reflect.Zero(field.Type().Elem())
for i := vlen; i < flen; i++ {
field.Index(i).Set(z)
}
}
default:
return fmt.Errorf("bigquery: impossible field type %s", field.Type())
}
for i, val := range vslice {
if i < flen { // avoid writing past the end of a short array
if err := setElem(field.Index(i), val); err != nil {
return err
}
}
}
return nil
}
示例4: decodeList
func (d *Decoder) decodeList(avList []*dynamodb.AttributeValue, v reflect.Value) error {
switch v.Kind() {
case reflect.Slice:
// Make room for the slice elements if needed
if v.IsNil() || v.Cap() < len(avList) {
// What about if ignoring nil/empty values?
v.Set(reflect.MakeSlice(v.Type(), 0, len(avList)))
}
case reflect.Array:
// Limited to capacity of existing array.
case reflect.Interface:
s := make([]interface{}, len(avList))
for i, av := range avList {
if err := d.decode(av, reflect.ValueOf(&s[i]).Elem(), tag{}); err != nil {
return err
}
}
v.Set(reflect.ValueOf(s))
return nil
default:
return &UnmarshalTypeError{Value: "list", Type: v.Type()}
}
// If v is not a slice, array
for i := 0; i < v.Cap() && i < len(avList); i++ {
v.SetLen(i + 1)
if err := d.decode(avList[i], v.Index(i), tag{}); err != nil {
return err
}
}
return nil
}
示例5: decodeBinary
func (d *Decoder) decodeBinary(b []byte, v reflect.Value) error {
if v.Kind() == reflect.Interface {
buf := make([]byte, len(b))
copy(buf, b)
v.Set(reflect.ValueOf(buf))
return nil
}
switch v.Interface().(type) {
case []byte:
if v.IsNil() || v.Cap() < len(b) {
v.Set(reflect.MakeSlice(byteSliceType, len(b), len(b)))
} else if v.Len() != len(b) {
v.SetLen(len(b))
}
copy(v.Interface().([]byte), b)
default:
if v.Kind() == reflect.Array && v.Type().Elem().Kind() == reflect.Uint8 {
reflect.Copy(v, reflect.ValueOf(b))
break
}
return &UnmarshalTypeError{Value: "binary", Type: v.Type()}
}
return nil
}
示例6: decodeStringSet
func (d *Decoder) decodeStringSet(ss []*string, v reflect.Value) error {
switch v.Kind() {
case reflect.Slice:
// Make room for the slice elements if needed
if v.IsNil() || v.Cap() < len(ss) {
v.Set(reflect.MakeSlice(v.Type(), 0, len(ss)))
}
case reflect.Array:
// Limited to capacity of existing array.
case reflect.Interface:
set := make([]string, len(ss))
for i, s := range ss {
if err := d.decodeString(s, reflect.ValueOf(&set[i]).Elem(), tag{}); err != nil {
return err
}
}
v.Set(reflect.ValueOf(set))
return nil
default:
return &UnmarshalTypeError{Value: "string set", Type: v.Type()}
}
for i := 0; i < v.Cap() && i < len(ss); i++ {
v.SetLen(i + 1)
u, elem := indirect(v.Index(i), false)
if u != nil {
return u.UnmarshalDynamoDBAttributeValue(&dynamodb.AttributeValue{SS: ss})
}
if err := d.decodeString(ss[i], elem, tag{}); err != nil {
return err
}
}
return nil
}
示例7: setSlice
// parse a value as the appropriate type and store it in the struct
func setSlice(dest reflect.Value, values []string) error {
if !dest.CanSet() {
return fmt.Errorf("field is not writable")
}
var ptr bool
elem := dest.Type().Elem()
if elem.Kind() == reflect.Ptr {
ptr = true
elem = elem.Elem()
}
// Truncate the dest slice in case default values exist
if !dest.IsNil() {
dest.SetLen(0)
}
for _, s := range values {
v := reflect.New(elem)
if err := setScalar(v.Elem(), s); err != nil {
return err
}
if !ptr {
v = v.Elem()
}
dest.Set(reflect.Append(dest, v))
}
return nil
}
示例8: ensureLen
func ensureLen(d reflect.Value, n int) {
if n > d.Cap() {
d.Set(reflect.MakeSlice(d.Type(), n, n))
} else {
d.SetLen(n)
}
}
示例9: unmarshalArray
func (p *Decoder) unmarshalArray(pval *plistValue, val reflect.Value) {
subvalues := pval.value.([]*plistValue)
var n int
if val.Kind() == reflect.Slice {
// Slice of element values.
// Grow slice.
cnt := len(subvalues) + val.Len()
if cnt >= val.Cap() {
ncap := 2 * cnt
if ncap < 4 {
ncap = 4
}
new := reflect.MakeSlice(val.Type(), val.Len(), ncap)
reflect.Copy(new, val)
val.Set(new)
}
n = val.Len()
val.SetLen(cnt)
} else if val.Kind() == reflect.Array {
if len(subvalues) > val.Cap() {
panic(fmt.Errorf("plist: attempted to unmarshal %d values into an array of size %d", len(subvalues), val.Cap()))
}
} else {
panic(&incompatibleDecodeTypeError{val.Type(), pval.kind})
}
// Recur to read element into slice.
for _, sval := range subvalues {
p.unmarshal(sval, val.Index(n))
n++
}
return
}
示例10: decodeSliceElems
func decodeSliceElems(s *Stream, val reflect.Value, elemdec decoder) error {
i := 0
for ; ; i++ {
// grow slice if necessary
if i >= val.Cap() {
newcap := val.Cap() + val.Cap()/2
if newcap < 4 {
newcap = 4
}
newv := reflect.MakeSlice(val.Type(), val.Len(), newcap)
reflect.Copy(newv, val)
val.Set(newv)
}
if i >= val.Len() {
val.SetLen(i + 1)
}
// decode into element
if err := elemdec(s, val.Index(i)); err == EOL {
break
} else if err != nil {
return addErrorContext(err, fmt.Sprint("[", i, "]"))
}
}
if i < val.Len() {
val.SetLen(i)
}
return nil
}
示例11: decodeList
func (d *Decoder) decodeList(v reflect.Value) error {
//if we have an interface, just put a []interface{} in it!
if v.Kind() == reflect.Interface {
var x []interface{}
defer func(p reflect.Value) { p.Set(v) }(v)
v = reflect.ValueOf(&x).Elem()
}
if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
return fmt.Errorf("Cant store a []interface{} into %s", v.Type())
}
//read out the l that prefixes the list
ch, err := d.readByte()
if err != nil {
return err
}
if ch != 'l' {
panic("got something other than a list head after a peek")
}
for i := 0; ; i++ {
//peek for the end token and read it out
ch, err := d.peekByte()
if err != nil {
return err
}
switch ch {
case 'e':
_, err := d.readByte() //consume the end
return err
}
//grow it if required
if i >= v.Cap() && v.IsValid() {
newcap := v.Cap() + v.Cap()/2
if newcap < 4 {
newcap = 4
}
newv := reflect.MakeSlice(v.Type(), v.Len(), newcap)
reflect.Copy(newv, v)
v.Set(newv)
}
//reslice into cap (its a slice now since it had to have grown)
if i >= v.Len() && v.IsValid() {
v.SetLen(i + 1)
}
//decode a value into the index
if err := d.decodeInto(v.Index(i)); err != nil {
return err
}
}
panic("unreachable")
}
示例12: array
//FIXME: fix when v is invalid
// type(1) | name length(1) | item size(4) | raw name bytes | 0x00
// | element number(4) | element1 | ... | elementN
func (d *decodeState) array(v reflect.Value) {
d.off += 1 // type
klen := int(Int8(d.data[d.off:]))
d.off += 1 // name length
// vlen := int(Int32(d.data[d.off:]))
d.off += 4 // content length
//var key string
if klen > 0 {
key := d.data[d.off : d.off+klen-1]
d.off += klen
v = fieldByTag(v, key)
u, pv := d.indirect(v, false)
if u != nil {
d.off -= 1 + 1 + 4 + klen
if err := u.UnmarshalMCPACK(d.next()); err != nil {
d.error(err)
}
return
}
v = pv
}
n := int(Int32(d.data[d.off:]))
d.off += 4 // member number
if v.Kind() == reflect.Slice {
if n > v.Cap() {
newv := reflect.MakeSlice(v.Type(), n, n)
v.Set(newv)
}
v.SetLen(n)
}
for i := 0; i < n; i++ {
if i < v.Len() {
d.value(v.Index(i))
} else {
d.value(reflect.Value{})
}
}
if n < v.Len() {
if v.Kind() == reflect.Array {
z := reflect.Zero(v.Type().Elem())
for i := 0; i < v.Len(); i++ {
v.Index(i).Set(z)
}
}
}
if n == 0 && v.Kind() == reflect.Slice {
v.Set(reflect.MakeSlice(v.Type(), 0, 0))
}
}
示例13: fixSliceLen
func fixSliceLen(slice reflect.Value, lenslice int) reflect.Value {
if slice.Len() != lenslice {
if slice.Cap() >= lenslice {
slice.SetLen(lenslice)
} else {
slice.Set(reflect.MakeSlice(slice.Type(), lenslice, lenslice))
}
}
return slice
}
示例14: unmarshalAttr
// unmarshalAttr unmarshals a single XML attribute into val.
func (p *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
if val.Kind() == reflect.Ptr {
if val.IsNil() {
val.Set(reflect.New(val.Type().Elem()))
}
val = val.Elem()
}
if val.CanInterface() && val.Type().Implements(unmarshalerAttrType) {
// This is an unmarshaler with a non-pointer receiver,
// so it's likely to be incorrect, but we do what we're told.
return val.Interface().(UnmarshalerAttr).UnmarshalXMLAttr(attr)
}
if val.CanAddr() {
pv := val.Addr()
if pv.CanInterface() && pv.Type().Implements(unmarshalerAttrType) {
return pv.Interface().(UnmarshalerAttr).UnmarshalXMLAttr(attr)
}
}
// Not an UnmarshalerAttr; try encoding.TextUnmarshaler.
if val.CanInterface() && val.Type().Implements(textUnmarshalerType) {
// This is an unmarshaler with a non-pointer receiver,
// so it's likely to be incorrect, but we do what we're told.
return val.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(attr.Value))
}
if val.CanAddr() {
pv := val.Addr()
if pv.CanInterface() && pv.Type().Implements(textUnmarshalerType) {
return pv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(attr.Value))
}
}
if val.Type().Kind() == reflect.Slice && val.Type().Elem().Kind() != reflect.Uint8 {
// Slice of element values.
// Grow slice.
n := val.Len()
val.Set(reflect.Append(val, reflect.Zero(val.Type().Elem())))
// Recur to read element into slice.
if err := p.unmarshalAttr(val.Index(n), attr); err != nil {
val.SetLen(n)
return err
}
return nil
}
if val.Type() == attrType {
val.Set(reflect.ValueOf(attr))
return nil
}
return copyValue(val, []byte(attr.Value))
}
示例15: unifySlice
func (md *MetaData) unifySlice(data interface{}, rv reflect.Value) error {
datav := reflect.ValueOf(data)
if datav.Kind() != reflect.Slice {
return badtype("slice", data)
}
sliceLen := datav.Len()
if rv.IsNil() || rv.Len() < datav.Len() {
rv.Set(reflect.MakeSlice(rv.Type(), sliceLen, sliceLen))
}
rv.SetLen(datav.Len())
return md.unifySliceArray(datav, rv)
}