structure: golint and govet improvements

This commit is contained in:
Fatih Arslan 2014-08-08 15:38:40 +03:00
parent 091a0feb9b
commit 50c639fd56
2 changed files with 13 additions and 9 deletions

View File

@ -4,6 +4,9 @@ package structure
import "reflect" import "reflect"
var ( var (
// DefaultTagName is the default tag name for struct fields which provides
// a more granular to tweak certain structs. Lookup the necessary functions
// for more info.
DefaultTagName = "structure" // struct's field default tag name DefaultTagName = "structure" // struct's field default tag name
) )
@ -80,7 +83,7 @@ func Map(s interface{}) map[string]interface{} {
func Values(s interface{}) []interface{} { func Values(s interface{}) []interface{} {
v, fields := strctInfo(s) v, fields := strctInfo(s)
t := make([]interface{}, 0) var t []interface{}
for _, field := range fields { for _, field := range fields {
val := v.FieldByName(field.Name) val := v.FieldByName(field.Name)
@ -120,7 +123,8 @@ func Values(s interface{}) []interface{} {
func Fields(s interface{}) []string { func Fields(s interface{}) []string {
v, fields := strctInfo(s) v, fields := strctInfo(s)
keys := make([]string, 0) var keys []string
for _, field := range fields { for _, field := range fields {
val := v.FieldByName(field.Name) val := v.FieldByName(field.Name)
@ -245,8 +249,8 @@ func IsStruct(s interface{}) bool {
return t.Kind() == reflect.Struct return t.Kind() == reflect.Struct
} }
// Name returns the structs's type name within its package. It returns an // Name returns the structs's type name within its package. It returns an
// empty string for unnamed types. It panics if s's kind is not struct. // empty string for unnamed types. It panics if s's kind is not struct.
func Name(s interface{}) string { func Name(s interface{}) string {
t := reflect.TypeOf(s) t := reflect.TypeOf(s)
@ -290,7 +294,7 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) {
v := strctVal(s) v := strctVal(s)
t := v.Type() t := v.Type()
f := make([]reflect.StructField, 0) var f []reflect.StructField
for i := 0; i < t.NumField(); i++ { for i := 0; i < t.NumField(); i++ {
field := t.Field(i) field := t.Field(i)

View File

@ -200,7 +200,7 @@ func TestMap_Nested(t *testing.T) {
} }
if name := in["Name"].(string); name != "example" { if name := in["Name"].(string); name != "example" {
t.Error("Map nested struct's name field should give example, got: %s", name) t.Errorf("Map nested struct's name field should give example, got: %s", name)
} }
} }
@ -228,7 +228,7 @@ func TestMap_Anonymous(t *testing.T) {
} }
if name := in["Name"].(string); name != "example" { if name := in["Name"].(string); name != "example" {
t.Error("Embedded A struct's Name field should give example, got: %s", name) t.Errorf("Embedded A struct's Name field should give example, got: %s", name)
} }
} }
@ -771,13 +771,13 @@ func TestName(t *testing.T) {
n := Name(f) n := Name(f)
if n != "Foo" { if n != "Foo" {
t.Error("Name should return Foo, got: %s", n) t.Errorf("Name should return Foo, got: %s", n)
} }
unnamed := struct{ Name string }{Name: "Cihangir"} unnamed := struct{ Name string }{Name: "Cihangir"}
m := Name(unnamed) m := Name(unnamed)
if m != "" { if m != "" {
t.Error("Name should return empty string for unnamed struct, got: %s", n) t.Errorf("Name should return empty string for unnamed struct, got: %s", n)
} }
defer func() { defer func() {