Skip to content

Commit

Permalink
optimize tailing nil removal
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Nov 27, 2024
1 parent 2a23c39 commit 40cde39
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions table/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ func objAsSlice(in interface{}) []interface{} {
}

// remove trailing nil pointers
tailIdx := len(out)
for i := len(out) - 1; i >= 0; i-- {
val := reflect.ValueOf(out[i])
if val.Kind() == reflect.Ptr && val.IsNil() {
out = out[:i]
} else {
if val.Kind() != reflect.Ptr || !val.IsNil() {
break
}
tailIdx = i
}
return out
return out[:tailIdx]
}

func objIsSlice(in interface{}) bool {
Expand Down
2 changes: 2 additions & 0 deletions table/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func Test_objAsSlice(t *testing.T) {
assert.Equal(t, "[1 2 3]", fmt.Sprint(objAsSlice(&[]int{a, b, c})))
assert.Equal(t, "[1 2 3]", fmt.Sprint(objAsSlice(&[]*int{&a, &b, &c})))
assert.Equal(t, "[1 2]", fmt.Sprint(objAsSlice(&[]*int{&a, &b, nil})))
assert.Equal(t, "[1]", fmt.Sprint(objAsSlice(&[]*int{&a, nil, nil})))
assert.Equal(t, "[]", fmt.Sprint(objAsSlice(&[]*int{nil, nil, nil})))
assert.Equal(t, "[<nil> 2]", fmt.Sprint(objAsSlice(&[]*int{nil, &b, nil})))
}

Expand Down

0 comments on commit 40cde39

Please sign in to comment.