Kevin Wan 2 years ago
parent
commit
f76b976262
3 changed files with 577 additions and 525 deletions
  1. 527 523
      core/mapping/unmarshaler.go
  2. 48 0
      core/mapping/unmarshaler_test.go
  3. 2 2
      core/mapping/utils.go

File diff suppressed because it is too large
+ 527 - 523
core/mapping/unmarshaler.go


+ 48 - 0
core/mapping/unmarshaler_test.go

@@ -3628,6 +3628,54 @@ func TestUnmarshalJsonReaderWithMismatchTypeBoolMap(t *testing.T) {
 	}, &req))
 	}, &req))
 }
 }
 
 
+func TestUnmarshalJsonBytesSliceOfMaps(t *testing.T) {
+	input := []byte(`{
+  "order_id": "1234567",
+  "refund_reason": {
+    "reason_code": [
+      123,
+      234
+    ],
+    "desc": "not wanted",
+    "show_reason": [
+      {
+        "123": "not enough",
+        "234": "closed"
+      }
+    ]
+  },
+  "product_detail": {
+    "product_id": "123",
+    "sku_id": "123",
+    "name": "cake",
+    "actual_amount": 100
+  }
+}`)
+
+	type (
+		RefundReasonData struct {
+			ReasonCode []int               `json:"reason_code"`
+			Desc       string              `json:"desc"`
+			ShowReason []map[string]string `json:"show_reason"`
+		}
+
+		ProductDetailData struct {
+			ProductId    string `json:"product_id"`
+			SkuId        string `json:"sku_id"`
+			Name         string `json:"name"`
+			ActualAmount int    `json:"actual_amount"`
+		}
+		OrderApplyRefundReq struct {
+			OrderId       string            `json:"order_id"`
+			RefundReason  RefundReasonData  `json:"refund_reason,optional"`
+			ProductDetail ProductDetailData `json:"product_detail,optional"`
+		}
+	)
+
+	var req OrderApplyRefundReq
+	assert.NoError(t, UnmarshalJsonBytes(input, &req))
+}
+
 func BenchmarkDefaultValue(b *testing.B) {
 func BenchmarkDefaultValue(b *testing.B) {
 	for i := 0; i < b.N; i++ {
 	for i := 0; i < b.N; i++ {
 		var a struct {
 		var a struct {

+ 2 - 2
core/mapping/utils.go

@@ -210,8 +210,8 @@ func isRightInclude(b byte) (bool, error) {
 	}
 	}
 }
 }
 
 
-func maybeNewValue(field reflect.StructField, value reflect.Value) {
-	if field.Type.Kind() == reflect.Ptr && value.IsNil() {
+func maybeNewValue(fieldType reflect.Type, value reflect.Value) {
+	if fieldType.Kind() == reflect.Ptr && value.IsNil() {
 		value.Set(reflect.New(value.Type().Elem()))
 		value.Set(reflect.New(value.Type().Elem()))
 	}
 	}
 }
 }

Some files were not shown because too many files changed in this diff