Skip to content

Commit f468919

Browse files
committed
util/mak: delete long-deprecated, unused, pre-generics NonNil func
Updates tailscale#5590 (which deprecated it, 2.5 years ago) Change-Id: I137e82855ee33d91e5639b909f7ca64e237ed6ba Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
1 parent 8b72dd7 commit f468919

File tree

2 files changed

+0
-63
lines changed

2 files changed

+0
-63
lines changed

util/mak/mak.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
// things, notably to maps, but also slices.
66
package mak
77

8-
import (
9-
"fmt"
10-
"reflect"
11-
)
12-
138
// Set populates an entry in a map, making the map if necessary.
149
//
1510
// That is, it assigns (*m)[k] = v, making *m if it was nil.
@@ -20,35 +15,6 @@ func Set[K comparable, V any, T ~map[K]V](m *T, k K, v V) {
2015
(*m)[k] = v
2116
}
2217

23-
// NonNil takes a pointer to a Go data structure
24-
// (currently only a slice or a map) and makes sure it's non-nil for
25-
// JSON serialization. (In particular, JavaScript clients usually want
26-
// the field to be defined after they decode the JSON.)
27-
//
28-
// Deprecated: use NonNilSliceForJSON or NonNilMapForJSON instead.
29-
func NonNil(ptr any) {
30-
if ptr == nil {
31-
panic("nil interface")
32-
}
33-
rv := reflect.ValueOf(ptr)
34-
if rv.Kind() != reflect.Ptr {
35-
panic(fmt.Sprintf("kind %v, not Ptr", rv.Kind()))
36-
}
37-
if rv.Pointer() == 0 {
38-
panic("nil pointer")
39-
}
40-
rv = rv.Elem()
41-
if rv.Pointer() != 0 {
42-
return
43-
}
44-
switch rv.Type().Kind() {
45-
case reflect.Slice:
46-
rv.Set(reflect.MakeSlice(rv.Type(), 0, 0))
47-
case reflect.Map:
48-
rv.Set(reflect.MakeMap(rv.Type()))
49-
}
50-
}
51-
5218
// NonNilSliceForJSON makes sure that *slicePtr is non-nil so it will
5319
// won't be omitted from JSON serialization and possibly confuse JavaScript
5420
// clients expecting it to be present.

util/mak/mak_test.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,6 @@ func TestSet(t *testing.T) {
4040
})
4141
}
4242

43-
func TestNonNil(t *testing.T) {
44-
var s []string
45-
NonNil(&s)
46-
if len(s) != 0 {
47-
t.Errorf("slice len = %d; want 0", len(s))
48-
}
49-
if s == nil {
50-
t.Error("slice still nil")
51-
}
52-
53-
s = append(s, "foo")
54-
NonNil(&s)
55-
if len(s) != 1 {
56-
t.Errorf("len = %d; want 1", len(s))
57-
}
58-
if s[0] != "foo" {
59-
t.Errorf("value = %q; want foo", s)
60-
}
61-
62-
var m map[string]string
63-
NonNil(&m)
64-
if len(m) != 0 {
65-
t.Errorf("map len = %d; want 0", len(s))
66-
}
67-
if m == nil {
68-
t.Error("map still nil")
69-
}
70-
}
71-
7243
func TestNonNilMapForJSON(t *testing.T) {
7344
type M map[string]int
7445
var m M

0 commit comments

Comments
 (0)