2
0

Simply typed nil and driver.Valuer handling

* Convert typed nils to untyped nils at beginning of encoding process.
* Restore v4 json/jsonb null behavior
* Add anynil internal package
This commit is contained in:
Jack Christensen
2022-03-05 19:45:44 -06:00
parent 39d2e3dc3f
commit 1cef9075d9
7 changed files with 66 additions and 94 deletions
+36
View File
@@ -0,0 +1,36 @@
package anynil
import "reflect"
// Is returns true if value is any type of nil. e.g. nil or []byte(nil).
func Is(value interface{}) bool {
if value == nil {
return true
}
refVal := reflect.ValueOf(value)
switch refVal.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return refVal.IsNil()
default:
return false
}
}
// Normalize converts typed nils (e.g. []byte(nil)) into untyped nil. Other values are returned unmodified.
func Normalize(v interface{}) interface{} {
if Is(v) {
return nil
}
return v
}
// NormalizeSlice converts all typed nils (e.g. []byte(nil)) in s into untyped nils. Other values are unmodified. s is
// mutated in place.
func NormalizeSlice(s []interface{}) {
for i := range s {
if Is(s[i]) {
s[i] = nil
}
}
}