AlexStocks commented on code in PR #1080:
URL: https://github.com/apache/dubbo-go-pixiu/pull/1080#discussion_r4505143486


##########
pkg/common/util/response.go:
##########
@@ -88,14 +88,15 @@ func mapIItoMapSI(in any) any {
        outMap := make(map[string]any, len(inMap))
 
        for k, v := range inMap {
-               if v == nil {
-                       continue
-               }
                s := fmt.Sprint(k)
                if s == "class" {
                        // ignore the "class" field
                        continue
                }
+               if v == nil {
+                       outMap[s] = nil

Review Comment:
   🟡 **typed nil 仍然会被折叠成 `{}` / `[]`,而不是 `null`。**
   
   `v == nil` 只对「无类型 nil 接口」成立。对这些值它是 `false`:
   
   ```go
   (*string)(nil)        // Kind = Ptr   -> default 分支,输出 null(OK)
   (map[any]any)(nil)    // Kind = Map   -> mapIItoMapSI(nil map) 迭代 0 次 -> 输出 
{}
   ([]any)(nil)          // Kind = Slice -> 输出 []
   ```
   
   也就是说 Hessian 回来一个 `nil list` / `nil map` 时会变成空容器,而调用方无法区分「空集合」和「null」。测试矩阵里 
`typed nil value` 只覆盖了 `*string`(那条其实在改动前就已经输出 `null`,属于控制组),`IsNil()` 的 
map/slice 分支没有用例。
   
   如果本次就修,最小改法是在 switch 前加一道:
   
   ```go
   rv := reflect.ValueOf(v)
   switch rv.Kind() {
   case reflect.Map, reflect.Slice, reflect.Ptr, reflect.Interface:
       if rv.IsNil() {
           outMap[s] = nil
           continue
       }
   }
   ```
   
   如果按 PR 描述里的「Typed nil maps/slices retain existing 
behavior」有意留给后续,建议在代码里补一行注释写明这是已决策的遗留,而不是漏掉——否则下一个读这段的人会以为 `{}` 是 bug 
顺手改掉,反而破坏兼容性。



##########
pkg/common/util/response_test.go:
##########
@@ -93,3 +95,77 @@ func TestNewDubboSliceResponse(t *testing.T) {
        assert.Equal(t, "tc", r["name"])
        assert.Equal(t, "bob", r["father"].(map[string]any)["name"])
 }
+
+func TestNewDubboResponsePreservesNullMapValues(t *testing.T) {
+       var nullString *string
+       for _, tt := range []struct {
+               name     string
+               input    any
+               want     string
+               humpWant string
+       }{
+               {
+                       name:     "string map",
+                       input:    map[string]any{"nullValue": nil, "zero": 0, 
"false": false, "empty": ""},
+                       want:     
`{"nullValue":null,"zero":0,"false":false,"empty":""}`,
+                       humpWant: 
`{"null_value":null,"zero":0,"false":false,"empty":""}`,
+               },
+               {
+                       name:     "generic map",
+                       input:    map[any]any{"nullValue": nil, "zero": 0, 
"false": false, "empty": "", "class": "java.util.Map"},
+                       want:     
`{"nullValue":null,"zero":0,"false":false,"empty":""}`,
+                       humpWant: 
`{"null_value":null,"zero":0,"false":false,"empty":""}`,
+               },
+               {
+                       name:     "nested generic map",
+                       input:    map[any]any{"child": map[any]any{"nullValue": 
nil}},
+                       want:     `{"child":{"nullValue":null}}`,
+                       humpWant: `{"child":{"null_value":null}}`,
+               },
+               {
+                       name:     "generic map in slice",
+                       input:    []any{map[any]any{"nullValue": nil}},
+                       want:     `[{"nullValue":null}]`,
+                       humpWant: `[{"null_value":null}]`,
+               },
+               {
+                       name:     "nested string map",
+                       input:    map[any]any{"child": 
map[string]any{"nullValue": nil}},
+                       want:     `{"child":{"nullValue":null}}`,
+                       humpWant: `{"child":{"null_value":null}}`,
+               },
+               {
+                       name:     "typed nil value",
+                       input:    map[any]any{"nullValue": nullString},

Review Comment:
   💭 建议把 typed nil 的当前行为固化成用例(哪怕结论是「保持 `{}` / `[]`」):
   
   ```go
   {name: "typed nil map", input: map[any]any{"m": (map[any]any)(nil)}, want: 
`{"m":{}}`},
   {name: "typed nil slice", input: map[any]any{"s": ([]any)(nil)}, want: 
`{"s":[]}`},
   ```
   
   理由:这两条正是未来最容易被「统一成 null」误改的分支,有金标准测试锁住,改的人就必须显式改测试,评审时也就看得见契约变了。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to