From ae5dadae7669ff2736f74c1344ed90ca3b9299e7 Mon Sep 17 00:00:00 2001 From: LYY Date: Fri, 16 Aug 2024 11:46:07 +0800 Subject: [PATCH 1/3] feat: add tag option string for struct type filed --- query/encode.go | 2 +- query/encode_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/query/encode.go b/query/encode.go index f877b1c..51046ab 100644 --- a/query/encode.go +++ b/query/encode.go @@ -256,7 +256,7 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error { continue } - if sv.Kind() == reflect.Struct { + if sv.Kind() == reflect.Struct && !opts.Contains("string") { if err := reflectValue(values, sv, name); err != nil { return err } diff --git a/query/encode_test.go b/query/encode_test.go index a94b44d..e03e68b 100644 --- a/query/encode_test.go +++ b/query/encode_test.go @@ -745,3 +745,35 @@ func TestParseTag(t *testing.T) { } } } + +type customStructString struct { + V string +} + +func (s customStructString) String() string { + return s.V +} + +func TestValues_CustomStructStringValue(t *testing.T) { + tests := []struct { + input interface{} + want url.Values + }{ + { + struct { + V customStructString `url:"v,string"` + }{customStructString{"a"}}, + url.Values{"v": {"a"}}, + }, + { + struct { + V customStructString `url:"v"` + }{}, + url.Values{"v[V]": {""}}, + }, + } + + for _, tt := range tests { + testValue(t, tt.input, tt.want) + } +} From a841d7753d1f17fb50bc3d13dfb9a9461baf285c Mon Sep 17 00:00:00 2001 From: LYY Date: Fri, 16 Aug 2024 12:47:17 +0800 Subject: [PATCH 2/3] update --- query/encode_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/query/encode_test.go b/query/encode_test.go index e03e68b..9be33c5 100644 --- a/query/encode_test.go +++ b/query/encode_test.go @@ -747,11 +747,11 @@ func TestParseTag(t *testing.T) { } type customStructString struct { - V string + inner string } func (s customStructString) String() string { - return s.V + return s.inner } func TestValues_CustomStructStringValue(t *testing.T) { @@ -769,7 +769,7 @@ func TestValues_CustomStructStringValue(t *testing.T) { struct { V customStructString `url:"v"` }{}, - url.Values{"v[V]": {""}}, + url.Values{}, }, } From 2dc5904136723c56c00dd0750a3292689f85f7e2 Mon Sep 17 00:00:00 2001 From: LYY Date: Fri, 16 Aug 2024 12:55:19 +0800 Subject: [PATCH 3/3] update --- query/encode_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/query/encode_test.go b/query/encode_test.go index 9be33c5..10af29d 100644 --- a/query/encode_test.go +++ b/query/encode_test.go @@ -747,11 +747,11 @@ func TestParseTag(t *testing.T) { } type customStructString struct { - inner string + v string } func (s customStructString) String() string { - return s.inner + return s.v } func TestValues_CustomStructStringValue(t *testing.T) {