lib/ur: Track new folder feature usage (#6803)

This commit is contained in:
Audrius Butkevicius
2020-06-28 20:35:22 +02:00
committed by GitHub
parent d7fc7008af
commit 9d4a700829
4 changed files with 180 additions and 6 deletions
+32
View File
@@ -137,6 +137,38 @@ func UniqueTrimmedStrings(ss []string) []string {
return us
}
func FillNil(data interface{}) {
s := reflect.ValueOf(data).Elem()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
newValue := reflect.New(f.Type().Elem())
f.Set(newValue)
f = f.Elem()
}
if f.CanSet() {
if f.IsZero() {
switch f.Kind() {
case reflect.Map:
f.Set(reflect.MakeMap(f.Type()))
case reflect.Slice:
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
case reflect.Chan:
f.Set(reflect.MakeChan(f.Type(), 0))
}
}
if f.Kind() == reflect.Struct && f.CanAddr() {
if addr := f.Addr(); addr.CanInterface() {
FillNil(addr.Interface())
}
}
}
}
}
// FillNilSlices sets default value on slices that are still nil.
func FillNilSlices(data interface{}) error {
s := reflect.ValueOf(data).Elem()
+125
View File
@@ -287,3 +287,128 @@ func TestUtilStopTwicePanic(t *testing.T) {
}()
s.Stop()
}
func TestFillNil(t *testing.T) {
type A struct {
Slice []int
Map map[string]string
Chan chan int
}
type B struct {
Slice *[]int
Map *map[string]string
Chan *chan int
}
type C struct {
A A
B *B
D *****[]int
}
c := C{}
FillNil(&c)
if c.A.Slice == nil {
t.Error("c.A.Slice")
}
if c.A.Map == nil {
t.Error("c.A.Slice")
}
if c.A.Chan == nil {
t.Error("c.A.Chan")
}
if c.B == nil {
t.Error("c.B")
}
if c.B.Slice == nil {
t.Error("c.B.Slice")
}
if c.B.Map == nil {
t.Error("c.B.Slice")
}
if c.B.Chan == nil {
t.Error("c.B.Chan")
}
if *c.B.Slice == nil {
t.Error("*c.B.Slice")
}
if *c.B.Map == nil {
t.Error("*c.B.Slice")
}
if *c.B.Chan == nil {
t.Error("*c.B.Chan")
}
if *****c.D == nil {
t.Error("c.D")
}
}
func TestFillNilDoesNotBulldozeSetFields(t *testing.T) {
type A struct {
Slice []int
Map map[string]string
Chan chan int
}
type B struct {
Slice *[]int
Map *map[string]string
Chan *chan int
}
type C struct {
A A
B *B
D **[]int
}
ch := make(chan int, 10)
d := make([]int, 10)
dd := &d
c := C{
A: A{
Slice: []int{1},
Map: map[string]string{
"k": "v",
},
Chan: make(chan int, 10),
},
B: &B{
Slice: &[]int{1},
Map: &map[string]string{
"k": "v",
},
Chan: &ch,
},
D: &dd,
}
FillNil(&c)
if len(c.A.Slice) != 1 {
t.Error("c.A.Slice")
}
if len(c.A.Map) != 1 {
t.Error("c.A.Slice")
}
if cap(c.A.Chan) != 10 {
t.Error("c.A.Chan")
}
if c.B == nil {
t.Error("c.B")
}
if len(*c.B.Slice) != 1 {
t.Error("c.B.Slice")
}
if len(*c.B.Map) != 1 {
t.Error("c.B.Slice")
}
if cap(*c.B.Chan) != 10 {
t.Error("c.B.Chan")
}
if cap(**c.D) != 10 {
t.Error("c.D")
}
}