当前位置: 首页>>代码示例>>Golang>>正文


Golang Value.SetMapIndex方法代码示例

本文整理汇总了Golang中reflect.Value.SetMapIndex方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.SetMapIndex方法的具体用法?Golang Value.SetMapIndex怎么用?Golang Value.SetMapIndex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.Value的用法示例。


在下文中一共展示了Value.SetMapIndex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: decode

func (mapDec *mapDecoder) decode(node RMNode, data interface{}, v reflect.Value) {
	values, err := redis.Values(node.Value(), nil)
	if err != nil {
		panic(err)
	}

	size := len(values) / 2
	vals, err := redis.Values(node.Value(), nil)
	if err != nil {
		panic(err)
	}
	v.Set(reflect.MakeMap(v.Type()))

	for i := 0; i < size; i++ {
		mKey, err := redis.String(values[i*2], nil)
		if err != nil {
			panic(err)
		}
		elemV := newValueForType(v.Type().Elem())
		if i < node.Size() {
			mapDec.elemFunc(node.Child(i), vals[i*2+1], elemV)
		} else {
			mapDec.elemFunc(node, vals[i*2+1], elemV)
		}
		v.SetMapIndex(reflect.ValueOf(mKey), elemV)
	}
}
开发者ID:LindenY,项目名称:gomdise,代码行数:27,代码来源:decode.go

示例2: mapCopy

func (d *decoder) mapCopy(src reflect.Value, dst reflect.Value) error {
	sv := src.MapKeys()
	dt := dst.Type().Elem()

	for i := range sv {
		ot := reflect.New(dt)

		if dt.Kind() == reflect.Ptr {
			ot = reflect.New(ot.Type().Elem())
		}

		d.pushPath(sv[i].String())
		err := d.decode(src.MapIndex(sv[i]), ot)
		if err != nil && err != ErrIncompatStruct {
			err = errors.New(d.pathString() + err.Error())
			d.path = nil
			return err
		}
		d.popPath()

		if err == nil {
			dst.SetMapIndex(sv[i], ot.Elem())
		}
	}
	return nil
}
开发者ID:j0ju,项目名称:go-interfacetools,代码行数:26,代码来源:copier.go

示例3: decodeMap

func decodeMap(d *decodeState, kind int, v reflect.Value) {
	t := v.Type()
	if t.Key().Kind() != reflect.String || kind != kindDocument {
		d.saveErrorAndSkip(kind, t)
		return
	}
	if v.IsNil() {
		v.Set(reflect.MakeMap(t))
	}
	subv := reflect.New(t.Elem()).Elem()
	offset := d.beginDoc()
	for {
		kind, name := d.scanKindName()
		if kind == 0 {
			break
		}
		if kind == kindNull {
			continue
		}
		subv.Set(reflect.Zero(t.Elem()))
		d.decodeValue(kind, subv)
		v.SetMapIndex(reflect.ValueOf(string(name)), subv)
	}
	d.endDoc(offset)
}
开发者ID:teopeurt,项目名称:go-mongo,代码行数:25,代码来源:bson_decode.go

示例4: decode

func (d *mapAsMapDecoder) decode(dv, sv reflect.Value) {
	dt := dv.Type()
	dv.Set(reflect.MakeMap(reflect.MapOf(dt.Key(), dt.Elem())))

	var mapKey reflect.Value
	var mapElem reflect.Value

	keyType := dv.Type().Key()
	elemType := dv.Type().Elem()

	for _, sElemKey := range sv.MapKeys() {
		var dElemKey reflect.Value
		var dElemVal reflect.Value

		if !mapKey.IsValid() {
			mapKey = reflect.New(keyType).Elem()
		} else {
			mapKey.Set(reflect.Zero(keyType))
		}
		dElemKey = mapKey

		if !mapElem.IsValid() {
			mapElem = reflect.New(elemType).Elem()
		} else {
			mapElem.Set(reflect.Zero(elemType))
		}
		dElemVal = mapElem

		d.keyDec(dElemKey, sElemKey)
		d.elemDec(dElemVal, sv.MapIndex(sElemKey))

		dv.SetMapIndex(dElemKey, dElemVal)
	}
}
开发者ID:kakamessi99,项目名称:shipyard,代码行数:34,代码来源:decoder_types.go

示例5: kMap

func (f *decFnInfo) kMap(rv reflect.Value) {
	containerLen := f.dd.readMapLen()

	if rv.IsNil() {
		rv.Set(reflect.MakeMap(f.rt))
	}

	if containerLen == 0 {
		return
	}

	ktype, vtype := f.rt.Key(), f.rt.Elem()
	for j := 0; j < containerLen; j++ {
		rvk := reflect.New(ktype).Elem()
		f.d.decodeValue(rvk)

		if ktype == intfTyp {
			rvk = rvk.Elem()
			if rvk.Type() == byteSliceTyp {
				rvk = reflect.ValueOf(string(rvk.Bytes()))
			}
		}
		rvv := rv.MapIndex(rvk)
		if !rvv.IsValid() {
			rvv = reflect.New(vtype).Elem()
		}

		f.d.decodeValue(rvv)
		rv.SetMapIndex(rvk, rvv)
	}
}
开发者ID:niltonkummer,项目名称:go,代码行数:31,代码来源:decode.go

示例6: deepCopyMap

func deepCopyMap(src, dst reflect.Value, t reflect.Type) {
	for _, key := range src.MapKeys() {
		var nkey, nval reflect.Value
		if key.IsValid() && key.CanSet() {
			if key.Kind() == reflect.Ptr {
				nkey = reflect.New(key.Elem().Type())
			} else {
				nkey = reflect.New(key.Type())
				nkey = reflect.Indirect(nkey)
			}
			s := reflect.Indirect(key)
			d := reflect.Indirect(nkey)
			tt := s.Type()
			deepCopy(s, d, tt)
		} else {
			nkey = key
		}
		if val := src.MapIndex(key); val.IsValid() && val.CanSet() {
			if val.Kind() == reflect.Ptr {
				nval = reflect.New(val.Elem().Type())
			} else {
				nval = reflect.New(val.Type())
				nval = reflect.Indirect(nval)
			}
			s := reflect.Indirect(val)
			d := reflect.Indirect(nval)
			tt := s.Type()
			deepCopy(s, d, tt)
		} else {
			nval = val
		}
		dst.SetMapIndex(nkey, nval)
	}
}
开发者ID:zwczou,项目名称:goserver,代码行数:34,代码来源:clone.go

示例7: Uniq

func Uniq(source, selector interface{}) interface{} {
	if selector == nil {
		selector = func(value, _ interface{}) Facade {
			return Facade{reflect.ValueOf(value)}
		}
	}

	var mapRV reflect.Value
	var arrRV reflect.Value
	each(source, selector, func(resRV, valueRv, _ reflect.Value) bool {
		if !mapRV.IsValid() {
			mapRT := reflect.MapOf(resRV.Type(), reflect.TypeOf(false))
			mapRV = reflect.MakeMap(mapRT)

			arrRT := reflect.SliceOf(valueRv.Type())
			arrRV = reflect.MakeSlice(arrRT, 0, 0)
		}

		mapValueRV := mapRV.MapIndex(resRV)
		if !mapValueRV.IsValid() {
			mapRV.SetMapIndex(resRV, reflect.ValueOf(true))
			arrRV = reflect.Append(arrRV, valueRv)
		}
		return false
	})
	if mapRV.IsValid() {
		return arrRV.Interface()
	}

	return nil
}
开发者ID:ahl5esoft,项目名称:golang-underscore,代码行数:31,代码来源:uniq.go

示例8: unifyMap

func (md *MetaData) unifyMap(mapping interface{}, rv reflect.Value) error {
	tmap, ok := mapping.(map[string]interface{})
	if !ok {
		if tmap == nil {
			return nil
		}
		return badtype("map", mapping)
	}
	if rv.IsNil() {
		rv.Set(reflect.MakeMap(rv.Type()))
	}
	for k, v := range tmap {
		md.decoded[md.context.add(k).String()] = true
		md.context = append(md.context, k)

		rvkey := indirect(reflect.New(rv.Type().Key()))
		rvval := reflect.Indirect(reflect.New(rv.Type().Elem()))
		if err := md.unify(v, rvval); err != nil {
			return err
		}
		md.context = md.context[0 : len(md.context)-1]

		rvkey.SetString(k)
		rv.SetMapIndex(rvkey, rvval)
	}
	return nil
}
开发者ID:Zenithar,项目名称:dgabloom,代码行数:27,代码来源:decode.go

示例9: MakeMap

// MakeMap examines the key type from a Hash and create a map with builtin type if possible.
// The idea is to avoid generating map[interface{}]interface{}, which cannot be handled by json.Marshal.
func (h *Hash) MakeMap(pair map[interface{}]interface{}) interface{} {
	if !h.KeyType.Type.IsPrimitive() {
		// well, a type can't be handled by json.Marshal... not much we can do
		return pair
	}
	if len(pair) == 0 {
		// figure out the map type manually
		switch h.KeyType.Type.Kind() {
		case BooleanKind:
			return map[bool]interface{}{}
		case IntegerKind:
			return map[int]interface{}{}
		case NumberKind:
			return map[float64]interface{}{}
		case StringKind:
			return map[string]interface{}{}
		case DateTimeKind:
			return map[time.Time]interface{}{}
		default:
			return pair
		}
	}
	var newMap reflect.Value
	for key, value := range pair {
		rkey, rvalue := reflect.ValueOf(key), reflect.ValueOf(value)
		if !newMap.IsValid() {
			newMap = reflect.MakeMap(reflect.MapOf(rkey.Type(), rvalue.Type()))
		}
		newMap.SetMapIndex(rkey, rvalue)
	}
	return newMap.Interface()
}
开发者ID:DavyC,项目名称:goa,代码行数:34,代码来源:types.go

示例10: DeleteByPath

func (t *MapType) DeleteByPath(v *reflect.Value, path Path) (err error) {
	err = t.Init()
	if err != nil {
		return
	}
	if len(path) > 1 {
		vk, err := t.KeyStringConverter.FromString(path[0])
		if err != nil {
			return err
		}
		ev := v.MapIndex(vk)
		et := t.ElemType
		oEv := ev
		err = et.DeleteByPath(&ev, path[1:])
		if err != nil {
			return err
		}
		if oEv == ev {
			return nil
		}
		v.SetMapIndex(vk, ev)
		return nil
	} else if len(path) == 0 {
		return fmt.Errorf("[MapType.DeleteByPath] delete map with no path.")
	}
	//Addressable?
	vk, err := t.KeyStringConverter.FromString(path[0])
	if err != nil {
		return err
	}
	v.SetMapIndex(vk, reflect.Value{})
	return nil
}
开发者ID:iizotop,项目名称:kmg,代码行数:33,代码来源:MapType.go

示例11: decodeMap

func (q *queryDecoder) decodeMap(output reflect.Value, prefix string, tag reflect.StructTag) error {
	// check for unflattened list member
	if !q.isEC2 && tag.Get("flattened") == "" {
		prefix += ".entry"
	}
	namer := newMapNamer(prefix, tag)

	mapType := output.Type()
	output.Set(reflect.MakeMap(mapType))

	keyType := mapType.Key()
	valueType := mapType.Elem()

	for i := 0; ; i++ {
		keyName := namer.KeyName(i)
		if !q.containsPrefix(keyName) {
			break
		}

		key, err := q.getValue(keyName, keyType)
		if err != nil {
			return err
		}

		val, err := q.getValue(namer.ValueName(i), valueType)
		if err != nil {
			return err
		}

		output.SetMapIndex(key, val)
	}

	return nil
}
开发者ID:rosenhouse,项目名称:awsfaker,代码行数:34,代码来源:decode.go

示例12: ensureMapKey

func ensureMapKey(head P, mid string, obj, key reflect.Value, ctx *Context) (value reflect.Value, err error) {
	value = obj.MapIndex(key)

	if value.Kind() != reflect.Invalid {
		if !ctx.CreateIfMissing {
			return
		}

		if !isNillable(value) || !value.IsNil() {
			return
		}
	}

	if !ctx.CreateIfMissing {
		err = ErrMissing
		return
	}

	if value, err = zero(head, mid, obj.Type().Elem()); err != nil {
		return
	}

	obj.SetMapIndex(key, value)
	return
}
开发者ID:nativetouch,项目名称:gopath,代码行数:25,代码来源:ensure.go

示例13: parseMap

func parseMap(key, keytail string, values []string, target reflect.Value) {
	t := target.Type()
	mapkey, maptail := keyed(t, key, keytail)

	// BUG(carl): We don't support any map keys except strings, although
	// there's no reason we shouldn't be able to throw the value through our
	// unparsing stack.
	var mk reflect.Value
	if t.Key().Kind() == reflect.String {
		mk = reflect.ValueOf(mapkey).Convert(t.Key())
	} else {
		pebkac("key for map %v isn't a string (it's a %v).", t, t.Key())
	}

	if target.IsNil() {
		target.Set(reflect.MakeMap(t))
	}

	val := target.MapIndex(mk)
	if !val.IsValid() || !val.CanSet() {
		// It's a teensy bit annoying that the value returned by
		// MapIndex isn't Set()table if the key exists.
		val = reflect.New(t.Elem()).Elem()
	}
	parse(key, maptail, values, val)
	target.SetMapIndex(mk, val)
}
开发者ID:FihlaTV,项目名称:bridge-server,代码行数:27,代码来源:parse.go

示例14: rcopy

// rcopy performs a recursive copy of values from the source to destination.
//
// root is used to skip certain aspects of the copy which are not valid
// for the root node of a object.
func rcopy(dst, src reflect.Value, root bool) {
	if !src.IsValid() {
		return
	}

	switch src.Kind() {
	case reflect.Ptr:
		if _, ok := src.Interface().(io.Reader); ok {
			if dst.Kind() == reflect.Ptr && dst.Elem().CanSet() {
				dst.Elem().Set(src)
			} else if dst.CanSet() {
				dst.Set(src)
			}
		} else {
			e := src.Type().Elem()
			if dst.CanSet() && !src.IsNil() {
				dst.Set(reflect.New(e))
			}
			if src.Elem().IsValid() {
				// Keep the current root state since the depth hasn't changed
				rcopy(dst.Elem(), src.Elem(), root)
			}
		}
	case reflect.Struct:
		if !root {
			dst.Set(reflect.New(src.Type()).Elem())
		}

		t := dst.Type()
		for i := 0; i < t.NumField(); i++ {
			name := t.Field(i).Name
			srcval := src.FieldByName(name)
			if srcval.IsValid() {
				rcopy(dst.FieldByName(name), srcval, false)
			}
		}
	case reflect.Slice:
		s := reflect.MakeSlice(src.Type(), src.Len(), src.Cap())
		dst.Set(s)
		for i := 0; i < src.Len(); i++ {
			rcopy(dst.Index(i), src.Index(i), false)
		}
	case reflect.Map:
		s := reflect.MakeMap(src.Type())
		dst.Set(s)
		for _, k := range src.MapKeys() {
			v := src.MapIndex(k)
			v2 := reflect.New(v.Type()).Elem()
			rcopy(v2, v, false)
			dst.SetMapIndex(k, v2)
		}
	default:
		// Assign the value if possible. If its not assignable, the value would
		// need to be converted and the impact of that may be unexpected, or is
		// not compatible with the dst type.
		if src.Type().AssignableTo(dst.Type()) {
			dst.Set(src)
		}
	}
}
开发者ID:EricMountain-1A,项目名称:openshift-origin,代码行数:64,代码来源:copy.go

示例15: unmarshalDictionary

func (p *Decoder) unmarshalDictionary(pval *plistValue, val reflect.Value) {
	typ := val.Type()
	switch val.Kind() {
	case reflect.Struct:
		tinfo, err := getTypeInfo(typ)
		if err != nil {
			panic(err)
		}

		subvalues := pval.value.(*dictionary).m
		for _, finfo := range tinfo.fields {
			p.unmarshal(subvalues[finfo.name], finfo.value(val))
		}
	case reflect.Map:
		if val.IsNil() {
			val.Set(reflect.MakeMap(typ))
		}

		subvalues := pval.value.(*dictionary).m
		for k, sval := range subvalues {
			keyv := reflect.ValueOf(k).Convert(typ.Key())
			mapElem := val.MapIndex(keyv)
			if !mapElem.IsValid() {
				mapElem = reflect.New(typ.Elem()).Elem()
			}

			p.unmarshal(sval, mapElem)
			val.SetMapIndex(keyv, mapElem)
		}
	default:
		panic(&incompatibleDecodeTypeError{typ, pval.kind})
	}
}
开发者ID:QuocHuy7a10,项目名称:Arianrhod,代码行数:33,代码来源:unmarshal.go


注:本文中的reflect.Value.SetMapIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。