Skip to content

add SetKeepNilAsNull to set null values #6

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Sets `value` on `data` at that json path

Note: you'll want to pass in a pointer to `data` so that the side effect actually is usable

### jsonpath.SetKeepNilAsNull(data interface{}, path string, value interface{}) (error)

Sets `value` on `data` at that json path while translating `nil` values as `null` instead of removing the key.

Note: you'll want to pass in a pointer to `data` so that the side effect actually is usable

### jsonpath.DoesNotExist error

Returned by `jsonpath.Get` on a nonexistent path:
Expand Down
21 changes: 18 additions & 3 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,16 @@ func Get(data interface{}, path string) (interface{}, error) {
return getByTokens(data, tokens)
}

// Set value on data at that json path
func Set(data interface{}, path string, value interface{}) error {
return set(data, path, value, false)
}

func SetKeepNilAsNull(data interface{}, path string, value interface{}) error {
return set(data, path, value, true)
}

// Set value on data at that json path
func set(data interface{}, path string, value interface{}, keepNilAsNull bool) error {
tokens, err := tokenizePath(path)
if err != nil {
return nil
Expand All @@ -118,7 +126,9 @@ func Set(data interface{}, path string, value interface{}) error {
last := tokens[len(tokens)-1]

data = followPtr(data)
value = followPtr(value)
if value != nil {
value = followPtr(value)
}

child := data
parent := data
Expand Down Expand Up @@ -163,7 +173,12 @@ func Set(data interface{}, path string, value interface{}) error {

switch reflect.ValueOf(child).Kind() {
case reflect.Map:
reflect.ValueOf(child).SetMapIndex(reflect.ValueOf(last), reflect.ValueOf(value))
if value == nil && keepNilAsNull {
var nilvalue interface{}
reflect.ValueOf(child).SetMapIndex(reflect.ValueOf(last), reflect.ValueOf(&nilvalue).Elem())
} else {
reflect.ValueOf(child).SetMapIndex(reflect.ValueOf(last), reflect.ValueOf(value))
}
return nil
case reflect.Slice:
sliceValue := reflect.ValueOf(child)
Expand Down
42 changes: 38 additions & 4 deletions jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ func TestJSON(t *testing.T) {
"pet": {
"name": "baxter",
"owner": {
"name": "john doe",
"contact": {
"phone": "859-289-9290"
}
"name": "john doe",
"contact": {
"phone": "859-289-9290"
}
},
"type": "dog",
"age": "4"
Expand Down Expand Up @@ -192,3 +192,37 @@ func TestErrors(t *testing.T) {
t.Errorf("error retrieving value %v", err)
}
}

func TestNullRemovesKey(t *testing.T) {
var payload = map[string]interface{}{
"key1": "val1",
}
err := Set(&payload, "key1", nil)
if err != nil {
t.Error("error setting a nil value failed", err)
}

_, ok := payload["key1"]
if ok {
t.Error("nil values assigned with Set should remove the key", err)
}
}

func TestNullSetWithSetKeepNilAsNullKeepKey(t *testing.T) {
var payload = map[string]interface{}{
"key1": "val1",
}

err := SetKeepNilAsNull(&payload, "key1", nil)
if err != nil {
t.Error("error setting a nil value failed", err)
}

val, ok := payload["key1"]
if !ok {
t.Error("nil values assigned with SetKeepNilAsNull should keep the key", err)
}
if val != nil {
t.Error("nil values assigned with SetKeepNilAsNull should set the key to null", err)
}
}