本文整理汇总了Golang中reflect.Value.MapKeys方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.MapKeys方法的具体用法?Golang Value.MapKeys怎么用?Golang Value.MapKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.MapKeys方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: writeMap
func (e *encodeState) writeMap(v reflect.Value, topLevel bool) {
if v.IsNil() {
return
}
if v.Type().Key().Kind() != reflect.String {
abort(&EncodeTypeError{v.Type()})
}
offset := e.beginDoc()
skipId := false
if topLevel {
idValue := v.MapIndex(idKey)
if idValue.IsValid() {
skipId = true
e.encodeValue("_id", defaultFieldInfo, idValue)
}
}
for _, k := range v.MapKeys() {
sk := k.String()
if !skipId || sk != "_id" {
e.encodeValue(sk, defaultFieldInfo, v.MapIndex(k))
}
}
e.WriteByte(0)
e.endDoc(offset)
}
示例2: encodeMap
func (e *Encoder) encodeMap(av *dynamodb.AttributeValue, v reflect.Value, fieldTag tag) error {
av.M = map[string]*dynamodb.AttributeValue{}
for _, key := range v.MapKeys() {
keyName := fmt.Sprint(key.Interface())
if keyName == "" {
return &InvalidMarshalError{msg: "map key cannot be empty"}
}
elemVal := v.MapIndex(key)
elem := &dynamodb.AttributeValue{}
err := e.encode(elem, elemVal, tag{})
skip, err := keepOrOmitEmpty(fieldTag.OmitEmptyElem, elem, err)
if err != nil {
return err
} else if skip {
continue
}
av.M[keyName] = elem
}
if len(av.M) == 0 {
encodeNull(av)
}
return nil
}
示例3: convertMapToCFDictionaryHelper
func convertMapToCFDictionaryHelper(m reflect.Value, helper func(reflect.Value) (cfTypeRef, error)) (C.CFDictionaryRef, error) {
// assume m is a map, because our caller already checked
if m.Type().Key().Kind() != reflect.String {
// the map keys aren't strings
return nil, &UnsupportedTypeError{m.Type()}
}
mapKeys := m.MapKeys()
keys := make([]cfTypeRef, len(mapKeys))
values := make([]cfTypeRef, len(mapKeys))
// defer the release
defer func() {
for _, cfKey := range keys {
cfRelease(cfTypeRef(cfKey))
}
for _, cfVal := range values {
cfRelease(cfTypeRef(cfVal))
}
}()
// create the keys and values slices
for i, keyVal := range mapKeys {
// keyVal is a Value representing a string
cfStr := convertStringToCFString(keyVal.String())
if cfStr == nil {
return nil, errors.New("plist: could not convert string to CFStringRef")
}
keys[i] = cfTypeRef(cfStr)
cfObj, err := helper(m.MapIndex(keyVal))
if err != nil {
return nil, err
}
values[i] = cfObj
}
return createCFDictionary(keys, values), nil
}
示例4: encode
func (me *mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.WriteString("null")
return
}
e.WriteByte('{')
// Extract and sort the keys.
keys := v.MapKeys()
sv := make([]reflectWithString, len(keys))
for i, v := range keys {
sv[i].v = v
if err := sv[i].resolve(); err != nil {
e.error(&MarshalerError{v.Type(), err})
}
}
sort.Sort(byString(sv))
for i, kv := range sv {
if i > 0 {
e.WriteByte(',')
}
e.string(kv.s, opts.escapeHTML)
e.WriteByte(':')
me.elemEnc(e, v.MapIndex(kv.v), opts)
}
e.WriteByte('}')
}
示例5: decodeObjectInterface
// decodeObjectInterface decodes the source value into map[string]interface{}
func decodeObjectInterface(s *decodeState, sv reflect.Value) map[string]interface{} {
m := map[string]interface{}{}
for _, key := range sv.MapKeys() {
m[key.Interface().(string)] = decodeInterface(s, sv.MapIndex(key))
}
return m
}
示例6: writeMap
func writeMap(w io.Writer, val reflect.Value) (err error) {
key := val.Type().Key()
if key.Kind() != reflect.String {
return &MarshalError{val.Type()}
}
_, err = fmt.Fprint(w, "d")
if err != nil {
return
}
keys := val.MapKeys()
// Sort keys
svList := make(stringValueArray, len(keys))
for i, key := range keys {
svList[i].key = key.String()
svList[i].value = val.MapIndex(key)
}
err = writeSVList(w, svList)
if err != nil {
return
}
_, err = fmt.Fprint(w, "e")
if err != nil {
return
}
return
}
示例7: Validate
// Validate checks whether the given value is writeable to this schema.
func (*NullSchema) Validate(v reflect.Value) bool {
// Check if the value is something that can be null
switch v.Kind() {
case reflect.Interface:
return v.IsNil()
case reflect.Array:
return v.Cap() == 0
case reflect.Slice:
return v.IsNil() || v.Cap() == 0
case reflect.Map:
return len(v.MapKeys()) == 0
case reflect.String:
return len(v.String()) == 0
case reflect.Float32:
// Should NaN floats be treated as null?
return math.IsNaN(v.Float())
case reflect.Float64:
// Should NaN floats be treated as null?
return math.IsNaN(v.Float())
case reflect.Ptr:
return v.IsNil()
case reflect.Invalid:
return true
}
// Nothing else in particular, so this should not validate?
return false
}
示例8: printMap
func printMap(w io.Writer, tv reflect.Value, depth string) {
fmt.Fprintf(w, "{\n")
ss := make(sort.StringSlice, tv.Len())
i := 0
for _, kval := range tv.MapKeys() {
if kval.Kind() == reflect.Interface {
kval = kval.Elem()
}
if kval.Kind() != reflect.String {
continue
}
ss[i] = kval.String()
i++
}
sort.Sort(ss)
for _, k := range ss {
val := tv.MapIndex(reflect.ValueOf(k))
v := val.Interface()
nd := depth + " "
for i := 0; i < len(k)+2; i++ {
nd += " "
}
fmt.Fprintf(w, " %s%s: ", depth, k)
printValue(w, v, nd)
}
fmt.Fprintf(w, "%s}\n", depth)
}
示例9: reflectMap
func reflectMap(v reflect.Value) map[interface{}]interface{} {
switch v.Kind() {
case reflect.Struct:
m := map[interface{}]interface{}{}
for i := 0; i < v.NumField(); i++ {
sf := v.Type().Field(i)
if sf.PkgPath != "" && !sf.Anonymous {
continue // unexported
}
k := sf.Name
v := v.Field(i).Interface()
m[k] = v
}
return m
case reflect.Map:
m := map[interface{}]interface{}{}
for _, mk := range v.MapKeys() {
k := ""
if mk.Interface() != nil {
k = fmt.Sprintf("%v", mk.Interface())
}
v := v.MapIndex(mk).Interface()
m[k] = v
}
return m
default:
return nil
}
}
示例10: 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)
}
}
示例11: retrieveValues
func (m Master) retrieveValues(db *sql.DB, rv reflect.Value, cfgs ...interface{}) (idField reflect.Value, fields []string, holders []string, values []interface{}, table string, err error) {
table = strings.ToLower(rv.Type().Name()) + "s"
// var insertNull bool
for _, c := range cfgs {
switch c.(type) {
// case InsertNull:
// insertNull = true
case TableName:
table = string(c.(TableName))
}
}
// _ = insertNull
switch rv.Kind() {
case reflect.Struct:
idField, fields, holders, values, err = m.walkStruct(rv)
case reflect.Map:
if rv.Type().Key().Kind() != reflect.String {
err = fmt.Errorf("sqlkungfu: Insert(map key is %s, want string)", rv.Type().Key().Kind())
return
}
for _, k := range rv.MapKeys() {
fields = append(fields, m.quoteColumn(k.String()))
holders = append(holders, "?")
values = append(values, rv.MapIndex(k).Interface())
}
}
return
}
示例12: AnySettableValue
//AnySettableValue find if exist one value that you can set.
func AnySettableValue(val reflect.Value) bool {
switch val.Kind() {
case reflect.Array:
for i := 0; i < val.Type().Len(); i++ {
if AnySettableValue(val.Index(i)) {
return true
}
}
case reflect.Interface:
return AnySettableValue(val.Elem())
case reflect.Map:
for _, key := range val.MapKeys() {
if AnySettableValue(val.MapIndex(key)) {
return true
}
}
case reflect.Ptr:
return AnySettableValue(val.Elem())
case reflect.Slice:
for i := 0; i < val.Len(); i++ {
if AnySettableValue(val.Index(i)) {
return true
}
}
case reflect.Struct:
for i := 0; i < val.Type().NumField(); i++ {
if AnySettableValue(val.Field(i)) {
return true
}
}
default:
return val.CanSet()
}
return false
}
示例13: valEq
func valEq(t *testing.T, name string, actual, expected reflect.Value) {
switch expected.Kind() {
case reflect.Slice:
// Check the type/length/element type
if !eq(t, name+" (type)", actual.Kind(), expected.Kind()) ||
!eq(t, name+" (len)", actual.Len(), expected.Len()) ||
!eq(t, name+" (elem)", actual.Type().Elem(), expected.Type().Elem()) {
return
}
// Check value equality for each element.
for i := 0; i < actual.Len(); i++ {
valEq(t, fmt.Sprintf("%s[%d]", name, i), actual.Index(i), expected.Index(i))
}
case reflect.Ptr:
// Check equality on the element type.
valEq(t, name, actual.Elem(), expected.Elem())
case reflect.Map:
if !eq(t, name+" (len)", actual.Len(), expected.Len()) {
return
}
for _, key := range expected.MapKeys() {
expectedValue := expected.MapIndex(key)
actualValue := actual.MapIndex(key)
if actualValue.IsValid() {
valEq(t, fmt.Sprintf("%s[%s]", name, key), actualValue, expectedValue)
} else {
t.Errorf("Expected key %s not found", key)
}
}
default:
eq(t, name, actual.Interface(), expected.Interface())
}
}
示例14: encodeMap
func (e *Encoder) encodeMap(by []byte, m reflect.Value, strTable map[string]int, ptrTable map[uintptr]int) []byte {
keys := m.MapKeys()
l := len(keys)
by = append(by, typeHASH)
by = varint(by, uint(l))
if e.PerlCompat {
for _, k := range keys {
by = e.encodeString(by, k.String(), true, strTable)
v := m.MapIndex(k)
by = e.encodeScalar(by, v, false, strTable, ptrTable)
}
} else {
for _, k := range keys {
by, _ = e.encode(by, k, true, strTable, ptrTable)
v := m.MapIndex(k)
by, _ = e.encode(by, v, false, strTable, ptrTable)
}
}
return by
}
示例15: approxDataSize
func approxDataSize(rv reflect.Value) (sum int) {
switch rk := rv.Kind(); rk {
case reflect.Invalid:
case reflect.Ptr, reflect.Interface:
sum += int(rv.Type().Size())
sum += approxDataSize(rv.Elem())
case reflect.Slice:
sum += int(rv.Type().Size())
for j := 0; j < rv.Len(); j++ {
sum += approxDataSize(rv.Index(j))
}
case reflect.String:
sum += int(rv.Type().Size())
sum += rv.Len()
case reflect.Map:
sum += int(rv.Type().Size())
for _, mk := range rv.MapKeys() {
sum += approxDataSize(mk)
sum += approxDataSize(rv.MapIndex(mk))
}
case reflect.Struct:
//struct size already includes the full data size.
//sum += int(rv.Type().Size())
for j := 0; j < rv.NumField(); j++ {
sum += approxDataSize(rv.Field(j))
}
default:
//pure value types
sum += int(rv.Type().Size())
}
return
}