mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 21:27:31 +00:00
util/mak: deprecate NonNil, add type-safe NonNilSliceForJSON, NonNilMapForJSON
And put the rationale in the name too to save the callers the need for a comment. Change-Id: I090f51b749a5a0641897ee89a8fb2e2080c8b782 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Maisem Ali

parent
e7376aca25
commit
f3ce1e2536
@@ -25,10 +25,8 @@ func Set[K comparable, V any, T ~map[K]V](m *T, k K, v V) {
|
||||
// (currently only a slice or a map) and makes sure it's non-nil for
|
||||
// JSON serialization. (In particular, JavaScript clients usually want
|
||||
// the field to be defined after they decode the JSON.)
|
||||
// MakeNonNil takes a pointer to a Go data structure
|
||||
// (currently only a slice or a map) and makes sure it's non-nil for
|
||||
// JSON serialization. (In particular, JavaScript clients usually want
|
||||
// the field to be defined after they decode the JSON.)
|
||||
//
|
||||
// Deprecated: use NonNilSliceForJSON or NonNilMapForJSON instead.
|
||||
func NonNil(ptr interface{}) {
|
||||
if ptr == nil {
|
||||
panic("nil interface")
|
||||
@@ -51,3 +49,23 @@ func NonNil(ptr interface{}) {
|
||||
rv.Set(reflect.MakeMap(rv.Type()))
|
||||
}
|
||||
}
|
||||
|
||||
// NonNilSliceForJSON makes sure that *slicePtr is non-nil so it will
|
||||
// won't be omitted from JSON serialization and possibly confuse JavaScript
|
||||
// clients expecting it to be preesnt.
|
||||
func NonNilSliceForJSON[T any, S ~[]T](slicePtr *S) {
|
||||
if *slicePtr != nil {
|
||||
return
|
||||
}
|
||||
*slicePtr = make([]T, 0)
|
||||
}
|
||||
|
||||
// NonNilMapForJSON makes sure that *slicePtr is non-nil so it will
|
||||
// won't be omitted from JSON serialization and possibly confuse JavaScript
|
||||
// clients expecting it to be preesnt.
|
||||
func NonNilMapForJSON[K comparable, V any, M ~map[K]V](mapPtr *M) {
|
||||
if *mapPtr != nil {
|
||||
return
|
||||
}
|
||||
*mapPtr = make(M)
|
||||
}
|
||||
|
@@ -69,3 +69,21 @@ func TestNonNil(t *testing.T) {
|
||||
t.Error("map still nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonNilMapForJSON(t *testing.T) {
|
||||
type M map[string]int
|
||||
var m M
|
||||
NonNilMapForJSON(&m)
|
||||
if m == nil {
|
||||
t.Fatal("still nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonNilSliceForJSON(t *testing.T) {
|
||||
type S []int
|
||||
var s S
|
||||
NonNilSliceForJSON(&s)
|
||||
if s == nil {
|
||||
t.Fatal("still nil")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user