本文整理汇总了Golang中reflect.MakeMap函数的典型用法代码示例。如果您正苦于以下问题:Golang MakeMap函数的具体用法?Golang MakeMap怎么用?Golang MakeMap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MakeMap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DecoderContainer
// DecoderContainer supports common cases for decoding into a nil
// interface{} depending on the context.
//
// When decoding into a nil interface{}, the following rules apply as we have
// to make assumptions about the specific types you want.
// - Maps are decoded as map[interface{}]interface{}
// unless you provide a default map type when creating your decoder.
// option: MapType
// - Lists are always decoded as []interface{}
// unless you provide a default slice type when creating your decoder.
// option: SliceType
// - raw bytes are decoded into []byte or string depending on setting of:
// option: BytesStringMapValue (if within a map value, use this setting)
// option: BytesStringSliceElement (else if within a slice, use this setting)
// option: BytesStringLiteral (else use this setting)
func (d SimpleDecoderContainerResolver) DecoderContainer(
parentcontainer reflect.Value, parentkey interface{},
length int, ct ContainerType) (rvn reflect.Value) {
switch ct {
case ContainerMap:
if d.MapType != nil {
rvn = reflect.MakeMap(d.MapType)
} else {
rvn = reflect.MakeMap(mapIntfIntfTyp)
}
case ContainerList:
if d.SliceType != nil {
rvn = reflect.MakeSlice(d.SliceType, length, length)
} else {
rvn = reflect.MakeSlice(intfSliceTyp, length, length)
}
case ContainerRawBytes:
rk := parentcontainer.Kind()
if (rk == reflect.Invalid && d.BytesStringLiteral) ||
(rk == reflect.Slice && d.BytesStringSliceElement) ||
(rk == reflect.Map && d.BytesStringMapValue) {
rvm := ""
rvn = reflect.ValueOf(&rvm)
} else {
rvn = reflect.MakeSlice(byteSliceTyp, length, length)
}
}
// fmt.Printf("DecoderContainer: %T, %v\n", rvn.Interface(), rvn.Interface())
return
}
示例2: makeMapFrom
func makeMapFrom(from interface{}) reflect.Value {
if isPointer(from) {
return reflect.MakeMap(reflect.TypeOf(from).Elem())
}
return reflect.MakeMap(reflect.TypeOf(from))
}
示例3: indirectAlloc
// indirectAlloc indirects all interfaces/pointers and allocates a value if
// needed. If value is nil then a Map is allocated.
func indirectAlloc(v reflect.Value) reflect.Value {
loop:
for {
switch v.Kind() {
case reflect.Interface:
if v.IsNil() {
// If nil interface default to Map.
v.Set(reflect.MakeMap(reflect.TypeOf(Map{})))
}
v = v.Elem()
case reflect.Ptr:
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
case reflect.Map:
if v.IsNil() {
v.Set(reflect.MakeMap(v.Type()))
}
break loop
case reflect.Slice:
if v.IsNil() {
v.Set(reflect.MakeSlice(v.Type(), v.Len(), 0))
}
break loop
default:
break loop
}
}
return v
}
示例4: Value
func (n dictNode) Value(sig Signature) (interface{}, error) {
set := n.Sigs()
if set.Empty() {
// no type information -> empty dict
return reflect.MakeMap(typeFor(sig.str)).Interface(), nil
}
if !set[sig] {
return nil, varTypeError{n.String(), sig}
}
m := reflect.MakeMap(typeFor(sig.str))
ksig := Signature{sig.str[2:3]}
vsig := Signature{sig.str[3 : len(sig.str)-1]}
for _, v := range n.children {
kv, err := v.key.Value(ksig)
if err != nil {
return nil, err
}
vv, err := v.val.Value(vsig)
if err != nil {
return nil, err
}
m.SetMapIndex(reflect.ValueOf(kv), reflect.ValueOf(vv))
}
return m.Interface(), nil
}
示例5: decodeMap
func decodeMap(v reflect.Value, x interface{}) {
t := v.Type()
if v.IsNil() {
v.Set(reflect.MakeMap(t))
}
for k, c := range getNode(x) {
i := reflect.New(t.Key()).Elem()
decodeValue(i, k)
w := v.MapIndex(i)
if w.IsValid() { // We have an actual element value to decode into.
if w.Kind() == reflect.Interface {
w = w.Elem()
}
w = reflect.New(w.Type()).Elem()
} else if t.Elem().Kind() != reflect.Interface { // The map's element type is concrete.
w = reflect.New(t.Elem()).Elem()
} else {
// The best we can do here is to decode as either a string (for scalars) or a map[string]interface {} (for the rest).
// We could try to guess the type based on the string (e.g. true/false => bool) but that'll get ugly fast,
// especially if we have to guess the kind (slice vs. array vs. map) and index type (e.g. string, int, etc.)
switch c.(type) {
case node:
w = reflect.MakeMap(stringMapType)
case string:
w = reflect.New(stringType).Elem()
default:
panic("value is neither node nor string")
}
}
decodeValue(w, c)
v.SetMapIndex(i, w)
}
}
示例6: initializeMap
func initializeMap(target *reflect.Value) {
// Initialize the map. Can't add keys without this.
if target.Kind() == reflect.Interface {
target.Set(reflect.MakeMap(reflect.TypeOf(map[string]interface{}{})))
*target = target.Elem()
} else {
target.Set(reflect.MakeMap(target.Type()))
}
}
示例7: ArrayColumnMap
func ArrayColumnMap(data interface{}, columnNames string) interface{} {
//提取信息
name := Explode(columnNames, ",")
nameInfo := []arrayColumnMapInfo{}
dataValue := reflect.ValueOf(data)
dataType := dataValue.Type().Elem()
for _, singleName := range name {
singleField, ok := getFieldByName(dataType, singleName)
if !ok {
panic(dataType.Name() + " struct has not field " + singleName)
}
nameInfo = append(nameInfo, arrayColumnMapInfo{
Index: singleField.Index,
Type: singleField.Type,
})
}
prevType := dataType
for i := len(nameInfo) - 1; i >= 0; i-- {
nameInfo[i].MapType = reflect.MapOf(
nameInfo[i].Type,
prevType,
)
prevType = nameInfo[i].MapType
}
//整合map
result := reflect.MakeMap(nameInfo[0].MapType)
dataLen := dataValue.Len()
for i := 0; i != dataLen; i++ {
singleValue := dataValue.Index(i)
prevValue := result
for singleNameIndex, singleNameInfo := range nameInfo {
var nextValue reflect.Value
singleField := singleValue.FieldByIndex(singleNameInfo.Index)
nextValue = prevValue.MapIndex(singleField)
if !nextValue.IsValid() {
if singleNameIndex+1 < len(nameInfo) {
nextValue = reflect.MakeMap(nameInfo[singleNameIndex+1].MapType)
} else {
nextValue = singleValue
}
prevValue.SetMapIndex(singleField, nextValue)
}
prevValue = nextValue
}
}
return result.Interface()
}
示例8: decodeMap
// decodeMap treats the next bytes as an XDR encoded variable array of 2-element
// structures whose fields are of the same type as the map keys and elements
// represented by the passed reflection value. Pointers are automatically
// indirected and allocated as necessary. It returns the the number of bytes
// actually read.
//
// An UnmarshalError is returned if any issues are encountered while decoding
// the elements.
func (d *Decoder) decodeMap(v reflect.Value) (int, error) {
dataLen, n, err := d.DecodeUint()
if err != nil {
return n, err
}
// Allocate storage for the underlying map if needed.
vt := v.Type()
if v.IsNil() {
v.Set(reflect.MakeMap(vt))
}
// Decode each key and value according to their type.
keyType := vt.Key()
elemType := vt.Elem()
for i := uint32(0); i < dataLen; i++ {
key := reflect.New(keyType).Elem()
n2, err := d.decode(key)
n += n2
if err != nil {
return n, err
}
val := reflect.New(elemType).Elem()
n2, err = d.decode(val)
n += n2
if err != nil {
return n, err
}
v.SetMapIndex(key, val)
}
return n, nil
}
示例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(m map[interface{}]interface{}) interface{} {
hash := reflect.MakeMap(toReflectType(h))
for key, value := range m {
hash.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
}
return hash.Interface()
}
示例10: Map
func (w *walker) Map(m reflect.Value) error {
t := m.Type()
newMap := reflect.MakeMap(reflect.MapOf(t.Key(), t.Elem()))
w.cs = append(w.cs, newMap)
w.valPush(newMap)
return nil
}
示例11: TestUnmarshal
func (s *S) TestUnmarshal(c *C) {
for _, item := range unmarshalTests {
t := reflect.ValueOf(item.value).Type()
var value interface{}
switch t.Kind() {
case reflect.Map:
value = reflect.MakeMap(t).Interface()
case reflect.String:
value = reflect.New(t).Interface()
case reflect.Ptr:
value = reflect.New(t.Elem()).Interface()
default:
c.Fatalf("missing case for %s", t)
}
err := yaml.Unmarshal([]byte(item.data), value)
if _, ok := err.(*yaml.TypeError); !ok {
c.Assert(err, IsNil)
}
if t.Kind() == reflect.String {
c.Assert(*value.(*string), Equals, item.value)
} else {
c.Assert(value, DeepEquals, item.value)
}
}
}
示例12: mapMap
func (this *SpecificDatumReader) mapMap(field Schema, reflectField reflect.Value, dec Decoder) (reflect.Value, error) {
if mapLength, err := dec.ReadMapStart(); err != nil {
return reflect.ValueOf(mapLength), err
} else {
resultMap := reflect.MakeMap(reflectField.Type())
for {
var i int64 = 0
for ; i < mapLength; i++ {
key, err := this.readValue(&StringSchema{}, reflectField, dec)
if err != nil {
return reflect.ValueOf(mapLength), err
}
val, err := this.readValue(field.(*MapSchema).Values, reflectField, dec)
if err != nil {
return reflect.ValueOf(mapLength), nil
}
if val.Kind() == reflect.Ptr {
resultMap.SetMapIndex(key, val.Elem())
} else {
resultMap.SetMapIndex(key, val)
}
}
mapLength, err = dec.MapNext()
if err != nil {
return reflect.ValueOf(mapLength), err
} else if mapLength == 0 {
break
}
}
return resultMap, nil
}
}
示例13: mapValue
func (d *Decoder) mapValue(v reflect.Value) error {
n, err := d.DecodeMapLen()
if err != nil {
return err
}
if n == -1 {
return nil
}
typ := v.Type()
if v.IsNil() {
v.Set(reflect.MakeMap(typ))
}
keyType := typ.Key()
valueType := typ.Elem()
for i := 0; i < n; i++ {
mk := reflect.New(keyType).Elem()
if err := d.DecodeValue(mk); err != nil {
return err
}
mv := reflect.New(valueType).Elem()
if err := d.DecodeValue(mv); err != nil {
return err
}
v.SetMapIndex(mk, mv)
}
return nil
}
示例14: decodeMapStringInterface
func decodeMapStringInterface(d *decodeState, kind int, v reflect.Value) {
if kind != kindDocument {
d.saveErrorAndSkip(kind, v.Type())
}
if v.IsNil() {
v.Set(reflect.MakeMap(v.Type()))
}
var m map[string]interface{}
switch mm := v.Interface().(type) {
case map[string]interface{}:
m = mm
case M:
m = (map[string]interface{})(mm)
}
offset := d.beginDoc()
for {
kind, name := d.scanKindName()
if kind == 0 {
break
}
if kind == kindNull {
continue
}
m[string(name)] = d.decodeValueInterface(kind)
}
d.endDoc(offset)
}
示例15: indirect
// rv must be a non-nil pointer or a settable value
func indirect(rv reflect.Value) (reflect.Value, error) {
for {
if rv.Kind() == reflect.Interface && !rv.IsNil() {
if e := rv.Elem(); e.Kind() == reflect.Ptr && !e.IsNil() {
rv = e.Elem()
}
}
if rv.Kind() == reflect.Map && rv.IsNil() {
rv.Set(reflect.MakeMap(rv.Type()))
}
if rv.Kind() != reflect.Ptr {
break
}
if rv.IsNil() {
rv.Set(reflect.New(rv.Type().Elem()))
}
rv = rv.Elem()
}
if k := rv.Kind(); k != reflect.Struct && k != reflect.Map {
return reflect.Value{}, &InvalidIndirectError{rv.Type()}
}
if rv.Kind() == reflect.Map {
if t := rv.Type(); t.Key().Kind() != reflect.String || t.Elem().Kind() != reflect.String {
return reflect.Value{}, &InvalidIndirectError{t}
}
}
return rv, nil
}