Remove testing imports from non-test code. (#9324)
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
9c76d9ba00
Коммит
e742ba7d51
@@ -15,11 +15,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
@@ -522,63 +520,6 @@ func IsValidId(value string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkNowhereNil checks that the given interface value is not nil, and if a struct, that all of
|
|
||||||
// its public fields are also nowhere nil
|
|
||||||
func checkNowhereNil(t *testing.T, name string, value interface{}) bool {
|
|
||||||
if value == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
v := reflect.ValueOf(value)
|
|
||||||
switch v.Type().Kind() {
|
|
||||||
case reflect.Ptr:
|
|
||||||
if v.IsNil() {
|
|
||||||
t.Logf("%s was nil", name)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return checkNowhereNil(t, fmt.Sprintf("(*%s)", name), v.Elem().Interface())
|
|
||||||
|
|
||||||
case reflect.Map:
|
|
||||||
if v.IsNil() {
|
|
||||||
t.Logf("%s was nil", name)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't check map values
|
|
||||||
return true
|
|
||||||
|
|
||||||
case reflect.Struct:
|
|
||||||
nowhereNil := true
|
|
||||||
for i := 0; i < v.NumField(); i++ {
|
|
||||||
f := v.Field(i)
|
|
||||||
// Ignore unexported fields
|
|
||||||
if v.Type().Field(i).PkgPath != "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
nowhereNil = nowhereNil && checkNowhereNil(t, fmt.Sprintf("%s.%s", name, v.Type().Field(i).Name), f.Interface())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nowhereNil
|
|
||||||
|
|
||||||
case reflect.Array:
|
|
||||||
fallthrough
|
|
||||||
case reflect.Chan:
|
|
||||||
fallthrough
|
|
||||||
case reflect.Func:
|
|
||||||
fallthrough
|
|
||||||
case reflect.Interface:
|
|
||||||
fallthrough
|
|
||||||
case reflect.UnsafePointer:
|
|
||||||
t.Logf("unhandled field %s, type: %s", name, v.Type().Kind())
|
|
||||||
return false
|
|
||||||
|
|
||||||
default:
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copied from https://golang.org/src/net/dnsclient.go#L119
|
// Copied from https://golang.org/src/net/dnsclient.go#L119
|
||||||
func IsDomainName(s string) bool {
|
func IsDomainName(s string) bool {
|
||||||
// See RFC 1035, RFC 3696.
|
// See RFC 1035, RFC 3696.
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package model
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -665,3 +666,60 @@ func TestNowhereNil(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkNowhereNil checks that the given interface value is not nil, and if a struct, that all of
|
||||||
|
// its public fields are also nowhere nil
|
||||||
|
func checkNowhereNil(t *testing.T, name string, value interface{}) bool {
|
||||||
|
if value == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
v := reflect.ValueOf(value)
|
||||||
|
switch v.Type().Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
if v.IsNil() {
|
||||||
|
t.Logf("%s was nil", name)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkNowhereNil(t, fmt.Sprintf("(*%s)", name), v.Elem().Interface())
|
||||||
|
|
||||||
|
case reflect.Map:
|
||||||
|
if v.IsNil() {
|
||||||
|
t.Logf("%s was nil", name)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Don't check map values
|
||||||
|
return true
|
||||||
|
|
||||||
|
case reflect.Struct:
|
||||||
|
nowhereNil := true
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
f := v.Field(i)
|
||||||
|
// Ignore unexported fields
|
||||||
|
if v.Type().Field(i).PkgPath != "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
nowhereNil = nowhereNil && checkNowhereNil(t, fmt.Sprintf("%s.%s", name, v.Type().Field(i).Name), f.Interface())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nowhereNil
|
||||||
|
|
||||||
|
case reflect.Array:
|
||||||
|
fallthrough
|
||||||
|
case reflect.Chan:
|
||||||
|
fallthrough
|
||||||
|
case reflect.Func:
|
||||||
|
fallthrough
|
||||||
|
case reflect.Interface:
|
||||||
|
fallthrough
|
||||||
|
case reflect.UnsafePointer:
|
||||||
|
t.Logf("unhandled field %s, type: %s", name, v.Type().Kind())
|
||||||
|
return false
|
||||||
|
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user