structure, remove sorting
This commit is contained in:
parent
3a3a3259e4
commit
f13a957d40
68
structure.go
68
structure.go
@ -1,10 +1,7 @@
|
|||||||
// Package structure contains various utilities functions to work with structs.
|
// Package structure contains various utilities functions to work with structs.
|
||||||
package structure
|
package structure
|
||||||
|
|
||||||
import (
|
import "reflect"
|
||||||
"reflect"
|
|
||||||
"sort"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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
|
||||||
@ -33,10 +30,6 @@ 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("structure"); tag != "" {
|
||||||
if tag == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
name = tag
|
name = tag
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,9 +39,9 @@ func Map(s interface{}) map[string]interface{} {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values converts the given s struct's field values to a []interface{}.
|
// Values converts the given s struct's field values to a []interface{}. A
|
||||||
// Values are inserted and sorted according to the field names. A struct tag
|
// struct tag with the content of "-" ignores the that particular field.
|
||||||
// with the content of "-" ignores the that particular field. Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// // Field is ignored by this package.
|
// // Field is ignored by this package.
|
||||||
// Field int `structure:"-"`
|
// Field int `structure:"-"`
|
||||||
@ -56,21 +49,11 @@ func Map(s interface{}) map[string]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 Values(s interface{}) []interface{} {
|
func Values(s interface{}) []interface{} {
|
||||||
m := Map(s)
|
v, fields := strctInfo(s)
|
||||||
|
|
||||||
keys := make([]string, len(m))
|
t := make([]interface{}, len(fields))
|
||||||
count := 0
|
for i := range fields {
|
||||||
for k := range m {
|
t[i] = v.Field(i).Interface()
|
||||||
keys[count] = k
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Strings(keys)
|
|
||||||
|
|
||||||
t := make([]interface{}, len(m))
|
|
||||||
|
|
||||||
for i, key := range keys {
|
|
||||||
t[i] = m[key]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return t
|
return t
|
||||||
@ -89,12 +72,7 @@ func Values(s interface{}) []interface{} {
|
|||||||
func IsValid(s interface{}) bool {
|
func IsValid(s interface{}) bool {
|
||||||
v, fields := strctInfo(s)
|
v, fields := strctInfo(s)
|
||||||
|
|
||||||
for i, field := range fields {
|
for i := range fields {
|
||||||
// don't check if it's omitted
|
|
||||||
if tag := field.Tag.Get("structure"); tag == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// zero value of the given field, such as "" for string, 0 for int
|
// zero value of the given field, such as "" for string, 0 for int
|
||||||
zero := reflect.Zero(v.Field(i).Type()).Interface()
|
zero := reflect.Zero(v.Field(i).Type()).Interface()
|
||||||
|
|
||||||
@ -109,8 +87,8 @@ func IsValid(s interface{}) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fields returns a sorted slice of field names. A struct tag with the content
|
// Fields returns a slice of field names. A struct tag with the content of "-"
|
||||||
// of "-" ignores the checking of that particular field. Example:
|
// ignores the checking of that particular field. Example:
|
||||||
//
|
//
|
||||||
// // Field is ignored by this package.
|
// // Field is ignored by this package.
|
||||||
// Field bool `structure:"-"`
|
// Field bool `structure:"-"`
|
||||||
@ -118,17 +96,13 @@ func IsValid(s interface{}) bool {
|
|||||||
// 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.
|
// fields will be neglected.
|
||||||
func Fields(s interface{}) []string {
|
func Fields(s interface{}) []string {
|
||||||
m := Map(s)
|
_, fields := strctInfo(s)
|
||||||
|
|
||||||
keys := make([]string, len(m))
|
keys := make([]string, len(fields))
|
||||||
count := 0
|
for i, field := range fields {
|
||||||
for k := range m {
|
keys[i] = field.Name
|
||||||
keys[count] = k
|
|
||||||
count++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(keys)
|
|
||||||
|
|
||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,15 +134,21 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) {
|
|||||||
|
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
|
|
||||||
f := make([]reflect.StructField, t.NumField())
|
f := make([]reflect.StructField, 0)
|
||||||
|
|
||||||
for i := 0; i < t.NumField(); i++ {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
field := t.Field(i)
|
||||||
// we can't access the value of unexported fields
|
// we can't access the value of unexported fields
|
||||||
if t.Field(i).PkgPath != "" {
|
if field.PkgPath != "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
f[i] = t.Field(i)
|
// don't check if it's omitted
|
||||||
|
if tag := field.Tag.Get("structure"); tag == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
f = append(f, field)
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, f
|
return v, f
|
||||||
|
|||||||
@ -71,10 +71,9 @@ func ExampleValues() {
|
|||||||
|
|
||||||
m := Values(s)
|
m := Values(s)
|
||||||
|
|
||||||
// note that the output is sorted according to the field names
|
|
||||||
fmt.Printf("Values: %+v\n", m)
|
fmt.Printf("Values: %+v\n", m)
|
||||||
// Output:
|
// Output:
|
||||||
// Values: [false 135790 Fatih]
|
// Values: [Fatih 135790 false]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleFields() {
|
func ExampleFields() {
|
||||||
@ -95,7 +94,7 @@ func ExampleFields() {
|
|||||||
// note that the output is sorted according to the field names
|
// note that the output is sorted according to the field names
|
||||||
fmt.Printf("Fields: %+v\n", m)
|
fmt.Printf("Fields: %+v\n", m)
|
||||||
// Output:
|
// Output:
|
||||||
// Fields: [LastAccessed Name Number]
|
// Fields: [Name LastAccessed Number]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleIsValid() {
|
func ExampleIsValid() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user