Pārlūkot izejas kodu

map any support get all

Tao Wen 8 gadi atpakaļ
vecāks
revīzija
1e91dbbf58
3 mainītis faili ar 56 papildinājumiem un 13 dzēšanām
  1. 40 12
      feature_any_object.go
  2. 6 0
      jsoniter_map_test.go
  3. 10 1
      jsoniter_object_test.go

+ 40 - 12
feature_any_object.go

@@ -503,14 +503,28 @@ func (any *objectAny) Get(path ...interface{}) Any {
 		return any
 	}
 	var element Any
-	key, ok := path[0].(string)
-	if ok {
-		element = any.fillCacheUntil(key)
+	switch firstPath := path[0].(type) {
+	case string:
+		element = any.fillCacheUntil(firstPath)
 		if element == nil {
-			element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", key, any.cache)}
+			element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
 		}
-	} else {
-		element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", key, any.cache)}
+	case int32:
+		if '*' == firstPath {
+			any.fillCache()
+			mappedAll := map[string]Any{}
+			for key, value := range any.cache {
+				mapped := value.Get(path[1:]...)
+				if mapped.ValueType() != Invalid {
+					mappedAll[key] = mapped
+				}
+			}
+			return wrapMap(mappedAll)
+		} else {
+			element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
+		}
+	default:
+		element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
 	}
 	if len(path) == 1 {
 		return element
@@ -738,14 +752,28 @@ func (any *mapAny) Get(path ...interface{}) Any {
 		return any
 	}
 	var element Any
-	key, ok := path[0].(string)
-	if ok {
-		element = any.fillCacheUntil(key)
+	switch firstPath := path[0].(type) {
+	case string:
+		element = any.fillCacheUntil(firstPath)
 		if element == nil {
-			element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", key, any.cache)}
+			element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
 		}
-	} else {
-		element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", key, any.cache)}
+	case int32:
+		if '*' == firstPath {
+			any.fillCache()
+			mappedAll := map[string]Any{}
+			for key, value := range any.cache {
+				mapped := value.Get(path[1:]...)
+				if mapped.ValueType() != Invalid {
+					mappedAll[key] = mapped
+				}
+			}
+			return wrapMap(mappedAll)
+		} else {
+			element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
+		}
+	default:
+		element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
 	}
 	if len(path) == 1 {
 		return element

+ 6 - 0
jsoniter_map_test.go

@@ -43,6 +43,12 @@ func Test_wrap_map(t *testing.T) {
 	should.Equal(map[string]string{"Field1":"hello"}, vals)
 }
 
+func Test_map_wrapper_any_get_all(t *testing.T) {
+	should := require.New(t)
+	any := Wrap(map[string][]int{"Field1": []int{1, 2}})
+	should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
+}
+
 func Test_write_val_map(t *testing.T) {
 	should := require.New(t)
 	val := map[string]string{"1": "2"}

+ 10 - 1
jsoniter_object_test.go

@@ -108,7 +108,6 @@ func Test_object_any_lazy_iterator(t *testing.T) {
 	should.Equal(map[string]string{"a":"b", "c":"d"}, vals)
 }
 
-
 func Test_object_any_with_two_lazy_iterators(t *testing.T) {
 	should := require.New(t)
 	any, err := UnmarshalAnyFromString(`{"a":"b","c":"d","e":"f"}`)
@@ -195,6 +194,16 @@ func Test_wrap_object(t *testing.T) {
 	should.Equal(map[string]string{"Field1":"hello"}, vals)
 }
 
+func Test_object_wrapper_any_get_all(t *testing.T) {
+	should := require.New(t)
+	type TestObject struct {
+		Field1 []int
+		Field2 []int
+	}
+	any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
+	should.Equal(`{"Field2":3,"Field1":1}`, any.Get('*', 0).ToString())
+}
+
 func Test_write_object(t *testing.T) {
 	should := require.New(t)
 	buf := &bytes.Buffer{}