diff --git a/README.md b/README.md index 0a7d2ce..2be6d3f 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,9 @@ n := structure.Name(s) // => true h := structure.Has(s, "Enabled") -// Check if the fields of a struct is initialized or not. -if !structure.IsZero(s) { - fmt.Println("s is initialized") +// Check if any field of a struct is initialized or not. +if structure.HasZero(s) { + fmt.Println("s has a zero value field") } // Check if it's a struct or a pointer to struct diff --git a/structure.go b/structure.go index 9364f20..9b9a16d 100644 --- a/structure.go +++ b/structure.go @@ -80,22 +80,22 @@ func Values(s interface{}) []interface{} { } -// IsZero returns true if any field in a struct is not initialized and has the -// zero default value. A struct tag with the content of "-" ignores the checking -// of that particular field. Example: +// HasZero returns true if a field in a struct is not initialized (zero value). +// A struct tag with the content of "-" ignores the checking of that particular +// field. Example: // // // Field is ignored by this package. // Field bool `structure:"-"` // // 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. -func IsZero(s interface{}) bool { +func HasZero(s interface{}) bool { v, fields := strctInfo(s) for i := range fields { val := v.Field(i) if IsStruct(val.Interface()) { - ok := IsZero(val.Interface()) + ok := HasZero(val.Interface()) if ok { return true } @@ -221,21 +221,6 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) { return v, f } -func strctType(s interface{}) reflect.Type { - t := reflect.TypeOf(s) - - // if pointer get the underlying element≤ - if t.Kind() == reflect.Ptr { - t = t.Elem() - } - - if t.Kind() != reflect.Struct { - panic("not struct") - } - - return t -} - func strctVal(s interface{}) reflect.Value { v := reflect.ValueOf(s) diff --git a/structure_example_test.go b/structure_example_test.go index dcb12ea..3babe71 100644 --- a/structure_example_test.go +++ b/structure_example_test.go @@ -96,7 +96,7 @@ func ExampleFields() { // Fields: [Name LastAccessed Number] } -func ExampleIsZero() { +func ExampleHasZero() { // 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. type Access struct { @@ -110,7 +110,7 @@ func ExampleIsZero() { a := &Access{ LastAccessed: time.Now(), } - isZeroA := IsZero(a) + hasZeroA := HasZero(a) // Name and Number is initialized. b := &Access{ @@ -118,10 +118,10 @@ func ExampleIsZero() { LastAccessed: time.Now(), Number: 12345, } - isZeroB := IsZero(b) + hasZeroB := HasZero(b) - fmt.Printf("%#v\n", isZeroA) - fmt.Printf("%#v\n", isZeroB) + fmt.Printf("%#v\n", hasZeroA) + fmt.Printf("%#v\n", hasZeroB) // Output: // true // false diff --git a/structure_test.go b/structure_test.go index 3980960..94f03dc 100644 --- a/structure_test.go +++ b/structure_test.go @@ -345,7 +345,7 @@ func TestFields_Anonymous(t *testing.T) { } } -func TestIsZero(t *testing.T) { +func TestHasZero(t *testing.T) { var T = struct { A string B int @@ -356,9 +356,9 @@ func TestIsZero(t *testing.T) { B: 2, } - ok := IsZero(T) + ok := HasZero(T) if !ok { - t.Error("IsZero should return true because A and B are initialized.") + t.Error("HasZero should return true because A and B are initialized.") } var X = struct { @@ -368,9 +368,9 @@ func TestIsZero(t *testing.T) { A: "a-value", } - ok = IsZero(X) + ok = HasZero(X) if !ok { - t.Error("IsZero should return true because A is initialized") + t.Error("HasZero should return true because A is initialized") } var Y = struct { @@ -381,13 +381,13 @@ func TestIsZero(t *testing.T) { B: 123, } - ok = IsZero(Y) + ok = HasZero(Y) if ok { - t.Error("IsZero should return false because A and B is initialized") + t.Error("HasZero should return false because A and B is initialized") } } -func TestIsZero_Nested(t *testing.T) { +func TestHasZero_Nested(t *testing.T) { type A struct { Name string D string @@ -400,13 +400,13 @@ func TestIsZero_Nested(t *testing.T) { } b := &B{A: a, C: 123} - ok := IsZero(b) + ok := HasZero(b) if !ok { - t.Error("IsZero should return true because D is not initialized") + t.Error("HasZero should return true because D is not initialized") } } -func TestIsZero_Anonymous(t *testing.T) { +func TestHasZero_Anonymous(t *testing.T) { type A struct { Name string D string @@ -420,9 +420,9 @@ func TestIsZero_Anonymous(t *testing.T) { b := &B{C: 123} b.A = a - ok := IsZero(b) + ok := HasZero(b) if !ok { - t.Error("IsZero should return false because D is not initialized") + t.Error("HasZero should return false because D is not initialized") } }