From 70d8bb7e9f584af93455066760f5869448da3e50 Mon Sep 17 00:00:00 2001 From: "torben.diekmann" Date: Tue, 9 May 2023 11:05:57 +0200 Subject: [PATCH 1/5] marshals nil slices/arrays like expected and equivalent to json.marshal --- sheriff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sheriff.go b/sheriff.go index 4ac7fa0..5d37c51 100644 --- a/sheriff.go +++ b/sheriff.go @@ -233,7 +233,7 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) { k := v.Kind() switch k { - case reflect.Interface, reflect.Map, reflect.Ptr: + case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: if v.IsNil() { return val, nil } From 977351cf6bbac5ad67a1ee4414ac5e6af5b52314 Mon Sep 17 00:00:00 2001 From: "torben.diekmann" Date: Tue, 9 May 2023 11:05:57 +0200 Subject: [PATCH 2/5] marshals nil slices/arrays like expected and equivalent to json.marshal --- sheriff_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sheriff_test.go b/sheriff_test.go index 3dcd937..84e438f 100644 --- a/sheriff_test.go +++ b/sheriff_test.go @@ -743,6 +743,20 @@ func TestMarshal_NilSlice(t *testing.T) { jsonResult, err := json.Marshal(marshalSlice) assert.NoError(t, err) + expect := "null" + + assert.Equal(t, expect, string(jsonResult)) +} + +func TestMarshal_EmptySlice(t *testing.T) { + var stringSlice = []string{} // empty slice + + marshalSlice, err := Marshal(&Options{}, stringSlice) + assert.NoError(t, err) + + jsonResult, err := json.Marshal(marshalSlice) + assert.NoError(t, err) + expect := "[]" assert.Equal(t, expect, string(jsonResult)) From 459725b266ed6692fbe4de71395b41913e337985 Mon Sep 17 00:00:00 2001 From: Mandeep Rekhi Date: Tue, 9 May 2023 04:10:53 -0700 Subject: [PATCH 3/5] fix: empty map to empty json (#38) Co-authored-by: mrekhi Co-authored-by: Michael Weibel <307427+mweibel@users.noreply.github.com> --- sheriff.go | 2 +- sheriff_test.go | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/sheriff.go b/sheriff.go index 5d37c51..85ca75b 100644 --- a/sheriff.go +++ b/sheriff.go @@ -263,7 +263,7 @@ func marshalValue(options *Options, v reflect.Value) (interface{}, error) { if k == reflect.Map { mapKeys := v.MapKeys() if len(mapKeys) == 0 { - return nil, nil + return val, nil } if mapKeys[0].Kind() != reflect.String { return nil, MarshalInvalidTypeError{t: mapKeys[0].Kind(), data: val} diff --git a/sheriff_test.go b/sheriff_test.go index 84e438f..cd79294 100644 --- a/sheriff_test.go +++ b/sheriff_test.go @@ -505,13 +505,33 @@ func TestMarshal_EmptyMap(t *testing.T) { assert.NoError(t, err) expected, err := json.Marshal(map[string]interface{}{ - "a_map": nil, + "a_map": make(map[string]interface{}), }) assert.NoError(t, err) assert.Equal(t, string(expected), string(actual)) } +func TestMarshal_EmptyMapJson(t *testing.T) { + emp := EmptyMapTest{ + AMap: make(map[string]string), + } + o := &Options{ + Groups: []string{"test"}, + } + + actualMap, err := Marshal(o, emp) + assert.NoError(t, err) + + actual, err := json.Marshal(actualMap) + assert.NoError(t, err) + + expected, err := json.Marshal(emp) + assert.NoError(t, err) + + assert.Equal(t, string(expected), string(actual)) +} + type TestMarshal_Embedded struct { Foo string `json:"foo" groups:"test"` } From 1fdb13b673325f8417a3944fe881ed0f3550de71 Mon Sep 17 00:00:00 2001 From: Michael Weibel Date: Wed, 6 Mar 2024 08:27:08 +0100 Subject: [PATCH 4/5] re-add test which went missing due to rebase --- sheriff_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/sheriff_test.go b/sheriff_test.go index cd79294..fe23197 100644 --- a/sheriff_test.go +++ b/sheriff_test.go @@ -532,6 +532,48 @@ func TestMarshal_EmptyMapJson(t *testing.T) { assert.Equal(t, string(expected), string(actual)) } +type PointerTest struct { + BoolPointer *bool `json:"boolPointer" groups:"test"` + BoolPointerNil *bool `json:"boolPointerNil" groups:"test"` + BoolPointerNilOmit *bool `json:"boolPointerNilOmit,omitempty" groups:"test"` + FloatPointer *float64 `json:"floatPointer" groups:"test"` + FloatPointerNil *float64 `json:"floatPointerNil" groups:"test"` + FloatPointerNilOmit *float64 `json:"floatPointerNilOmit" groups:"test"` + IntPointer *int `json:"intPointer" groups:"test"` + IntPointerNil *int `json:"intPointerNil" groups:"test"` + IntPointerNilOmit *int `json:"intPointerNilOmit" groups:"test"` + StringPointer *string `json:"stringPointer" groups:"test"` + StringPointerNil *string `json:"stringPointerNil" groups:"test"` + StringPointerNilOmit *string `json:"stringPointerNilOmit" groups:"test"` +} + +func TestMarshal_Pointer(t *testing.T) { + boolValue := true + intValue := -20000 + stringValue := "12%&/()ยง?-loaMEN" + floatValue := 0.0 + emp := PointerTest{ + BoolPointer: &boolValue, + IntPointer: &intValue, + StringPointer: &stringValue, + FloatPointer: &floatValue, + } + o := &Options{ + Groups: []string{"test"}, + } + + actualMap, err := Marshal(o, emp) + assert.NoError(t, err) + + actual, err := json.Marshal(actualMap) + assert.NoError(t, err) + + expected, err := json.Marshal(emp) + assert.NoError(t, err) + + assert.Equal(t, string(expected), string(actual)) +} + type TestMarshal_Embedded struct { Foo string `json:"foo" groups:"test"` } From dca258c34a7b4c68c13204672d289c894043e14a Mon Sep 17 00:00:00 2001 From: Michael Weibel Date: Wed, 6 Mar 2024 08:33:22 +0100 Subject: [PATCH 5/5] move to v1 --- LICENSE | 4 ++-- README.md | 4 ++-- example_test.go | 2 +- go.mod | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/LICENSE b/LICENSE index 73d7252..0e02fae 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2017 Michael Weibel, All rights reserved. +Copyright (c) 2017-2024 Michael Weibel, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are @@ -24,4 +24,4 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index 2f1feb8..c9393c9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![GoDoc](https://godoc.org/github.com/liip/sheriff?status.svg)](https://godoc.org/github.com/liip/sheriff) [![Build Status](https://travis-ci.org/liip/sheriff.svg?branch=master)](https://travis-ci.org/liip/sheriff) [![Coverage Status](https://coveralls.io/repos/github/liip/sheriff/badge.svg?branch=master)](https://coveralls.io/github/liip/sheriff?branch=master) ``` -go get github.com/liip/sheriff +go get github.com/liip/sheriff/v1 ``` Package sheriff marshals structs conditionally based on tags on the fields. @@ -92,7 +92,7 @@ import ( "log" "github.com/hashicorp/go-version" - "github.com/liip/sheriff" + "github.com/liip/sheriff/v1" ) type User struct { diff --git a/example_test.go b/example_test.go index 8d8b584..9dff7e1 100644 --- a/example_test.go +++ b/example_test.go @@ -6,7 +6,7 @@ import ( "log" "github.com/hashicorp/go-version" - "github.com/liip/sheriff" + "github.com/liip/sheriff/v1" ) type User struct { diff --git a/go.mod b/go.mod index 216cc24..38fe2a3 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/liip/sheriff +module github.com/liip/sheriff/v1 go 1.20