structure: rename IsZero to HasZero
This reflect the intention of this function much better. Suggestion is from golang-nuts.
This commit is contained in:
parent
69a6a30d8e
commit
301494b442
@ -51,9 +51,9 @@ n := structure.Name(s)
|
|||||||
// => true
|
// => true
|
||||||
h := structure.Has(s, "Enabled")
|
h := structure.Has(s, "Enabled")
|
||||||
|
|
||||||
// Check if the fields of a struct is initialized or not.
|
// Check if any field of a struct is initialized or not.
|
||||||
if !structure.IsZero(s) {
|
if structure.HasZero(s) {
|
||||||
fmt.Println("s is initialized")
|
fmt.Println("s has a zero value field")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's a struct or a pointer to struct
|
// Check if it's a struct or a pointer to struct
|
||||||
|
|||||||
25
structure.go
25
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
|
// HasZero returns true if a field in a struct is not initialized (zero value).
|
||||||
// zero default value. A struct tag with the content of "-" ignores the checking
|
// A struct tag with the content of "-" ignores the checking of that particular
|
||||||
// of that particular field. Example:
|
// field. Example:
|
||||||
//
|
//
|
||||||
// // Field is ignored by this package.
|
// // Field is ignored by this package.
|
||||||
// Field bool `structure:"-"`
|
// Field bool `structure:"-"`
|
||||||
//
|
//
|
||||||
// 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 IsZero(s interface{}) bool {
|
func HasZero(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 IsStruct(val.Interface()) {
|
if IsStruct(val.Interface()) {
|
||||||
ok := IsZero(val.Interface())
|
ok := HasZero(val.Interface())
|
||||||
if ok {
|
if ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -221,21 +221,6 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) {
|
|||||||
return v, f
|
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 {
|
func strctVal(s interface{}) reflect.Value {
|
||||||
v := reflect.ValueOf(s)
|
v := reflect.ValueOf(s)
|
||||||
|
|
||||||
|
|||||||
@ -96,7 +96,7 @@ func ExampleFields() {
|
|||||||
// Fields: [Name LastAccessed Number]
|
// Fields: [Name LastAccessed Number]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleIsZero() {
|
func ExampleHasZero() {
|
||||||
// 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 ExampleIsZero() {
|
|||||||
a := &Access{
|
a := &Access{
|
||||||
LastAccessed: time.Now(),
|
LastAccessed: time.Now(),
|
||||||
}
|
}
|
||||||
isZeroA := IsZero(a)
|
hasZeroA := HasZero(a)
|
||||||
|
|
||||||
// Name and Number is initialized.
|
// Name and Number is initialized.
|
||||||
b := &Access{
|
b := &Access{
|
||||||
@ -118,10 +118,10 @@ func ExampleIsZero() {
|
|||||||
LastAccessed: time.Now(),
|
LastAccessed: time.Now(),
|
||||||
Number: 12345,
|
Number: 12345,
|
||||||
}
|
}
|
||||||
isZeroB := IsZero(b)
|
hasZeroB := HasZero(b)
|
||||||
|
|
||||||
fmt.Printf("%#v\n", isZeroA)
|
fmt.Printf("%#v\n", hasZeroA)
|
||||||
fmt.Printf("%#v\n", isZeroB)
|
fmt.Printf("%#v\n", hasZeroB)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
|
|||||||
@ -345,7 +345,7 @@ func TestFields_Anonymous(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsZero(t *testing.T) {
|
func TestHasZero(t *testing.T) {
|
||||||
var T = struct {
|
var T = struct {
|
||||||
A string
|
A string
|
||||||
B int
|
B int
|
||||||
@ -356,9 +356,9 @@ func TestIsZero(t *testing.T) {
|
|||||||
B: 2,
|
B: 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
ok := IsZero(T)
|
ok := HasZero(T)
|
||||||
if !ok {
|
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 {
|
var X = struct {
|
||||||
@ -368,9 +368,9 @@ func TestIsZero(t *testing.T) {
|
|||||||
A: "a-value",
|
A: "a-value",
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = IsZero(X)
|
ok = HasZero(X)
|
||||||
if !ok {
|
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 {
|
var Y = struct {
|
||||||
@ -381,13 +381,13 @@ func TestIsZero(t *testing.T) {
|
|||||||
B: 123,
|
B: 123,
|
||||||
}
|
}
|
||||||
|
|
||||||
ok = IsZero(Y)
|
ok = HasZero(Y)
|
||||||
if ok {
|
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 {
|
type A struct {
|
||||||
Name string
|
Name string
|
||||||
D string
|
D string
|
||||||
@ -400,13 +400,13 @@ func TestIsZero_Nested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
b := &B{A: a, C: 123}
|
b := &B{A: a, C: 123}
|
||||||
|
|
||||||
ok := IsZero(b)
|
ok := HasZero(b)
|
||||||
if !ok {
|
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 {
|
type A struct {
|
||||||
Name string
|
Name string
|
||||||
D string
|
D string
|
||||||
@ -420,9 +420,9 @@ func TestIsZero_Anonymous(t *testing.T) {
|
|||||||
b := &B{C: 123}
|
b := &B{C: 123}
|
||||||
b.A = a
|
b.A = a
|
||||||
|
|
||||||
ok := IsZero(b)
|
ok := HasZero(b)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Error("IsZero should return false because D is not initialized")
|
t.Error("HasZero should return false because D is not initialized")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user