Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: nil handling for maps/slices / module version v1 #43

Merged
merged 5 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
POSSIBILITY OF SUCH DAMAGE.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -92,7 +92,7 @@ import (
"log"

"github.com/hashicorp/go-version"
"github.com/liip/sheriff"
"github.com/liip/sheriff/v1"
)

type User struct {
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"

"github.com/hashicorp/go-version"
"github.com/liip/sheriff"
"github.com/liip/sheriff/v1"
)

type User struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/liip/sheriff
module github.com/liip/sheriff/v1

go 1.20

Expand Down
4 changes: 2 additions & 2 deletions sheriff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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}
Expand Down
78 changes: 77 additions & 1 deletion sheriff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,75 @@ 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 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"`
}
Expand Down Expand Up @@ -743,6 +805,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))
Expand Down
Loading