Last active
August 29, 2015 14:20
-
-
Save hagbarddenstore/6def70af090f42c1cf09 to your computer and use it in GitHub Desktop.
Question: How to determine if a value is a valid nil/empty string or is absent in the input JSON. If the value is absent, it shouldn't be changed, but if it's present and is nil/empty string it should be removed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
const ( | |
updateJson1 = ` | |
{ | |
"firstname": "Peter", | |
"lastname": "Griffin" | |
} | |
` | |
updateJson2 = ` | |
{ | |
"address": { | |
"street": "31 Spooner Street", | |
"city": "Quahog", | |
"country": "United States of America" | |
} | |
} | |
` | |
updateJson3 = ` | |
{ | |
"address": null | |
} | |
` | |
) | |
var ( | |
person = &Person{ | |
Firstname: "Lois", | |
Lastname: "Griffin", | |
Address: &Address{ | |
Street: "31 Spooner Street", | |
City: "Quahog", | |
Country: "United States of America", | |
}, | |
} | |
) | |
type Address struct { | |
Street string | |
City string | |
Country string | |
} | |
type Person struct { | |
Firstname string | |
Lastname string | |
Address *Address | |
} | |
// Update the firstname and lastname | |
func update1() { | |
var updatePerson *Person | |
json.Unmarshal(updateJson1, &updatePerson) | |
person.Firstname = updatePerson.Firstname | |
person.Lastname = updatePerson.Lastname | |
// The address is absent in this example, thus shouldn't be changed. | |
// I can't check for nil, since nil as a valid value. | |
if false { | |
person.Address = updatePerson.Address | |
} | |
} | |
// Update the address | |
func update2() { | |
var updatePerson *Person | |
json.Unmarshal(updateJson2, &updatePerson) | |
// The Firstname is absent in this example, thus shouldn't be changed. | |
// I can't check for empty string, since empty string is a valid value. | |
if false { | |
person.Firstname = updatePerson.Firstname | |
} | |
// The Lastname is absent in this example, thus shouldn't be changed. | |
// I can't check for empty string, since empty string is a valid value. | |
if false { | |
person.Lastname = updatePerson.Lastname | |
} | |
person.Address = updatePerson.Address | |
} | |
// Remove the address | |
func update3() { | |
var updatePerson *Person | |
json.Unmarshal(updateJson3, &updatePerson) | |
// The Firstname is absent in this example, thus shouldn't be changed. | |
// I can't check for empty string, since empty string is a valid value. | |
if false { | |
person.Firstname = updatePerson.Firstname | |
} | |
// The Lastname is absent in this example, thus shouldn't be changed. | |
// I can't check for empty string, since empty string is a valid value. | |
if false { | |
person.Lastname = updatePerson.Lastname | |
} | |
// The address is nil, so it should be removed. | |
person.Address = updatePerson.Address | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment