From 4d548a92e91b687724e7e4f6bee6cf03bb2d8fbd Mon Sep 17 00:00:00 2001 From: Can Stand Date: Mon, 23 Feb 2026 15:34:10 +0800 Subject: [PATCH] fix: keep missing pointer fields nil in remapMapToStruct --- helpers.go | 19 ++++++++++++------- helpers_test.go | 15 +++++++++++++++ tests/browser_context_test.go | 2 +- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/helpers.go b/helpers.go index 16b10c6..7c23937 100644 --- a/helpers.go +++ b/helpers.go @@ -168,17 +168,22 @@ func remapValue(inMapValue reflect.Value, outStructValue reflect.Value) { fi := structTyp.Field(i) key := strings.Split(fi.Tag.Get("json"), ",")[0] structField := outStructValue.Field(i) - structFieldDeref := outStructValue.Field(i) + value := inMapValue.MapIndex(reflect.ValueOf(key)) + if !value.IsValid() { + continue + } + if value.Kind() == reflect.Interface { + if value.IsNil() { + continue + } + value = value.Elem() + } + structFieldDeref := structField if structField.Type().Kind() == reflect.Pointer { structField.Set(reflect.New(structField.Type().Elem())) structFieldDeref = structField.Elem() } - for _, e := range inMapValue.MapKeys() { - if key == e.String() { - value := inMapValue.MapIndex(e) - remapValue(value.Elem(), structFieldDeref) - } - } + remapValue(value, structFieldDeref) } default: panic(inMapValue.Interface()) diff --git a/helpers_test.go b/helpers_test.go index b82b413..2ce9b76 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -113,6 +113,21 @@ func TestRemapMapToStruct(t *testing.T) { require.Equal(t, ourStruct.V1, "foobar") } +func TestRemapMapToStructShouldKeepNilPointerForMissingField(t *testing.T) { + ourStruct := struct { + V1 string `json:"v1"` + PartitionKey *string `json:"partitionKey"` + }{} + inMap := map[string]any{ + "v1": "foobar", + } + + remapMapToStruct(inMap, &ourStruct) + + require.Equal(t, "foobar", ourStruct.V1) + require.Nil(t, ourStruct.PartitionKey) +} + func TestConvertSelectOptionSet(t *testing.T) { testCases := []struct { name string diff --git a/tests/browser_context_test.go b/tests/browser_context_test.go index e564647..515cfe9 100644 --- a/tests/browser_context_test.go +++ b/tests/browser_context_test.go @@ -187,7 +187,7 @@ func TestBrowserContextAddCookies(t *testing.T) { HttpOnly: false, Secure: false, SameSite: sameSite, - PartitionKey: playwright.String(""), + PartitionKey: nil, }, }, cookies)