From 50c639fd56ef5ccb1a2c5692bf5d08046619f711 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Fri, 8 Aug 2014 15:38:40 +0300 Subject: [PATCH] structure: golint and govet improvements --- structure.go | 14 +++++++++----- structure_test.go | 8 ++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/structure.go b/structure.go index e905dcf..c4b32ae 100644 --- a/structure.go +++ b/structure.go @@ -4,6 +4,9 @@ package structure import "reflect" 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 ) @@ -80,7 +83,7 @@ func Map(s interface{}) map[string]interface{} { func Values(s interface{}) []interface{} { v, fields := strctInfo(s) - t := make([]interface{}, 0) + var t []interface{} for _, field := range fields { val := v.FieldByName(field.Name) @@ -120,7 +123,8 @@ func Values(s interface{}) []interface{} { func Fields(s interface{}) []string { v, fields := strctInfo(s) - keys := make([]string, 0) + var keys []string + for _, field := range fields { val := v.FieldByName(field.Name) @@ -245,8 +249,8 @@ func IsStruct(s interface{}) bool { return t.Kind() == reflect.Struct } -// 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. +// 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. func Name(s interface{}) string { t := reflect.TypeOf(s) @@ -290,7 +294,7 @@ func strctInfo(s interface{}) (reflect.Value, []reflect.StructField) { v := strctVal(s) t := v.Type() - f := make([]reflect.StructField, 0) + var f []reflect.StructField for i := 0; i < t.NumField(); i++ { field := t.Field(i) diff --git a/structure_test.go b/structure_test.go index 3c822fe..4684f49 100644 --- a/structure_test.go +++ b/structure_test.go @@ -200,7 +200,7 @@ func TestMap_Nested(t *testing.T) { } 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" { - 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) 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"} m := Name(unnamed) 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() {