Merge branch 'master' of https://github.com/fatih/structure
This commit is contained in:
commit
792cad134c
@ -51,12 +51,12 @@ n := structure.Name(s)
|
|||||||
// => true
|
// => true
|
||||||
h := structure.Has(s, "Enabled")
|
h := structure.Has(s, "Enabled")
|
||||||
|
|
||||||
// Check if a field of a struct is initialized or not.
|
// Check if any field of a struct is initialized or not.
|
||||||
if structure.HasZero(s) {
|
if structure.HasZero(s) {
|
||||||
fmt.Println("s has a zero value field")
|
fmt.Println("s has a zero value field")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if all field of a struct is initialized or not.
|
// Check if all fields of a struct is initialized or not.
|
||||||
if structure.IsZero(s) {
|
if structure.IsZero(s) {
|
||||||
fmt.Println("all fields of s is zero value")
|
fmt.Println("all fields of s is zero value")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,10 @@ package structure
|
|||||||
|
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultTagName = "structure" // struct's field default tag name
|
||||||
|
)
|
||||||
|
|
||||||
// Map converts the given s struct to a map[string]interface{}, where the keys
|
// Map converts the given s struct to a map[string]interface{}, where the keys
|
||||||
// of the map are the field names and the values of the map the associated
|
// of the map are the field names and the values of the map the associated
|
||||||
// values of the fields. The default key string is the struct field name but
|
// values of the fields. The default key string is the struct field name but
|
||||||
@ -40,7 +44,7 @@ func Map(s interface{}) map[string]interface{} {
|
|||||||
|
|
||||||
// override if the user passed a structure tag value
|
// override if the user passed a structure tag value
|
||||||
// ignore if the user passed the "-" value
|
// ignore if the user passed the "-" value
|
||||||
if tag := field.Tag.Get("structure"); tag != "" {
|
if tag := field.Tag.Get(DefaultTagName); tag != "" {
|
||||||
name = tag
|
name = tag
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,7 +253,7 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// don't check if it's omitted
|
// don't check if it's omitted
|
||||||
if tag := field.Tag.Get("structure"); tag == "-" {
|
if tag := field.Tag.Get(DefaultTagName); tag == "-" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -110,6 +110,41 @@ func TestMap_Tag(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMap_CustomTag(t *testing.T) {
|
||||||
|
var T = struct {
|
||||||
|
A string `dd:"x"`
|
||||||
|
B int `dd:"y"`
|
||||||
|
C bool `dd:"z"`
|
||||||
|
}{
|
||||||
|
A: "a-value",
|
||||||
|
B: 2,
|
||||||
|
C: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultName := DefaultTagName
|
||||||
|
DefaultTagName = "dd"
|
||||||
|
defer func() {
|
||||||
|
DefaultTagName = defaultName
|
||||||
|
}()
|
||||||
|
a := Map(T)
|
||||||
|
|
||||||
|
inMap := func(key interface{}) bool {
|
||||||
|
for k := range a {
|
||||||
|
if reflect.DeepEqual(k, key) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, key := range []string{"x", "y", "z"} {
|
||||||
|
if !inMap(key) {
|
||||||
|
t.Errorf("Map should have the key %v", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestMap_Nested(t *testing.T) {
|
func TestMap_Nested(t *testing.T) {
|
||||||
type A struct {
|
type A struct {
|
||||||
Name string
|
Name string
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user