strucuture: rename IsValid to IsZero
This commit is contained in:
parent
0ec62843ec
commit
778d3311fd
@ -46,7 +46,7 @@ f := structure.Fields(s)
|
|||||||
n := structure.Name(s)
|
n := structure.Name(s)
|
||||||
|
|
||||||
// Check if the fields of a struct is initialized or not.
|
// Check if the fields of a struct is initialized or not.
|
||||||
if structure.IsValid(s) {
|
if structure.IsZero(s) {
|
||||||
fmt.Println("s is initialized")
|
fmt.Println("s is initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -79,7 +79,7 @@ func Values(s interface{}) []interface{} {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValid returns true if all fields in a struct are initialized (non zero
|
// IsZero returns true if all fields in a struct are initialized (non zero
|
||||||
// value). A struct tag with the content of "-" ignores the checking of that
|
// value). A struct tag with the content of "-" ignores the checking of that
|
||||||
// particular field. Example:
|
// particular field. Example:
|
||||||
//
|
//
|
||||||
@ -88,13 +88,13 @@ func Values(s interface{}) []interface{} {
|
|||||||
//
|
//
|
||||||
// Note that only exported fields of a struct can be accessed, non exported
|
// Note that only exported fields of a struct can be accessed, non exported
|
||||||
// fields will be neglected. It panics if s's kind is not struct.
|
// fields will be neglected. It panics if s's kind is not struct.
|
||||||
func IsValid(s interface{}) bool {
|
func IsZero(s interface{}) bool {
|
||||||
v, fields := strctInfo(s)
|
v, fields := strctInfo(s)
|
||||||
|
|
||||||
for i := range fields {
|
for i := range fields {
|
||||||
val := v.Field(i)
|
val := v.Field(i)
|
||||||
if val.Kind() == reflect.Struct {
|
if val.Kind() == reflect.Struct {
|
||||||
ok := IsValid(val.Interface())
|
ok := IsZero(val.Interface())
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@ -96,7 +96,7 @@ func ExampleFields() {
|
|||||||
// Fields: [Name LastAccessed Number]
|
// Fields: [Name LastAccessed Number]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleIsValid() {
|
func ExampleIsZero() {
|
||||||
// Let's define an Access struct. Note that the "Enabled" field is not
|
// Let's define an Access struct. Note that the "Enabled" field is not
|
||||||
// going to be checked because we added the "structure" tag to the field.
|
// going to be checked because we added the "structure" tag to the field.
|
||||||
type Access struct {
|
type Access struct {
|
||||||
@ -110,7 +110,7 @@ func ExampleIsValid() {
|
|||||||
a := &Access{
|
a := &Access{
|
||||||
LastAccessed: time.Now(),
|
LastAccessed: time.Now(),
|
||||||
}
|
}
|
||||||
validA := IsValid(a)
|
validA := IsZero(a)
|
||||||
|
|
||||||
// Name and Number is initialized.
|
// Name and Number is initialized.
|
||||||
b := &Access{
|
b := &Access{
|
||||||
@ -118,7 +118,7 @@ func ExampleIsValid() {
|
|||||||
LastAccessed: time.Now(),
|
LastAccessed: time.Now(),
|
||||||
Number: 12345,
|
Number: 12345,
|
||||||
}
|
}
|
||||||
validB := IsValid(b)
|
validB := IsZero(b)
|
||||||
|
|
||||||
fmt.Printf("%#v\n", validA)
|
fmt.Printf("%#v\n", validA)
|
||||||
fmt.Printf("%#v\n", validB)
|
fmt.Printf("%#v\n", validB)
|
||||||
|
|||||||
@ -345,7 +345,7 @@ func TestFields_Anonymous(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsValid(t *testing.T) {
|
func TestIsZero(t *testing.T) {
|
||||||
var T = struct {
|
var T = struct {
|
||||||
A string
|
A string
|
||||||
B int
|
B int
|
||||||
@ -356,9 +356,9 @@ func TestIsValid(t *testing.T) {
|
|||||||
B: 2,
|
B: 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
ok := IsValid(T)
|
ok := IsZero(T)
|
||||||
if ok {
|
if ok {
|
||||||
t.Error("IsValid should return false because D is not initialized")
|
t.Error("IsZero should return false because D is not initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
var X = struct {
|
var X = struct {
|
||||||
@ -368,13 +368,13 @@ func TestIsValid(t *testing.T) {
|
|||||||
A: "a-value",
|
A: "a-value",
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = IsValid(X)
|
ok = IsZero(X)
|
||||||
if ok {
|
if ok {
|
||||||
t.Error("IsValid should return false because F is not initialized")
|
t.Error("IsZero should return false because F is not initialized")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsValid_Nested(t *testing.T) {
|
func TestIsZero_Nested(t *testing.T) {
|
||||||
type A struct {
|
type A struct {
|
||||||
Name string
|
Name string
|
||||||
D string
|
D string
|
||||||
@ -387,13 +387,13 @@ func TestIsValid_Nested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
b := &B{A: a, C: 123}
|
b := &B{A: a, C: 123}
|
||||||
|
|
||||||
ok := IsValid(b)
|
ok := IsZero(b)
|
||||||
if ok {
|
if ok {
|
||||||
t.Error("IsValid should return false because D is not initialized")
|
t.Error("IsZero should return false because D is not initialized")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsValid_Anonymous(t *testing.T) {
|
func TestIsZero_Anonymous(t *testing.T) {
|
||||||
type A struct {
|
type A struct {
|
||||||
Name string
|
Name string
|
||||||
D string
|
D string
|
||||||
@ -407,9 +407,9 @@ func TestIsValid_Anonymous(t *testing.T) {
|
|||||||
b := &B{C: 123}
|
b := &B{C: 123}
|
||||||
b.A = a
|
b.A = a
|
||||||
|
|
||||||
ok := IsValid(b)
|
ok := IsZero(b)
|
||||||
if ok {
|
if ok {
|
||||||
t.Error("IsValid should return false because D is not initialized")
|
t.Error("IsZero should return false because D is not initialized")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user