Skip to content

Commit

Permalink
fixes to make gofmt lint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Br3nda committed Jan 3, 2024
1 parent ef32348 commit 1ab8f23
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 31 deletions.
6 changes: 5 additions & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// GetJSONTag takes a field name and json struct tag value and returns the effective name, omitempty setting
// and an indicator if the field value should be ommitted (if the tag value is "-").
// and an indicator if the field value should be omitted (if the tag value is "-").
//
// name, omitEmpty, omit := GetJSONTag(fieldName, fieldTag)
//
Expand All @@ -27,6 +27,7 @@ func GetJSONTag(fieldName, tag string) (string, bool, bool) {
if len(f) < 1 || f[0] == "" {
return fieldName, omitEmpty, false
}

return f[0], omitEmpty, false
}

Expand All @@ -37,6 +38,7 @@ func getJSONTagFromField(f reflect.StructField) (string, bool, bool) {
if t == "" {
t = f.Tag.Get("json")
}

return GetJSONTag(f.Name, t)
}

Expand All @@ -59,11 +61,13 @@ func StringToBool(s string) bool {
return i > 0
}
s = strings.ToLower(s)

return len(s) > 0 && s[0] == 't' || s[0] == 'y'
}

// stringToStringSlice hopes the input is a comma separated string and returns it as a slice.
func stringToStringSlice(s string) []string {
s = strings.Replace(s, ", ", ",", -1)

return strings.Split(s, ",")
}
1 change: 0 additions & 1 deletion examples/example-redis/redis_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type example1 struct {
}

func main() {

s := example1{
Tuna: "this is odd",
Songs: []string{"one", "two", "three"},
Expand Down
1 change: 0 additions & 1 deletion examples/example1/example1.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type example1 struct {
}

func main() {

s := example1{"hello", []string{"hi", "nice"}, 2, 20.5, true}

m, err := structmapper.StructToStringMap(&s)
Expand Down
2 changes: 0 additions & 2 deletions examples/example2/example2.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type example1 struct {
}

func main() {

m := map[string]string{
"candy": "",
// "candy": "99",
Expand All @@ -46,5 +45,4 @@ func main() {
}
fmt.Printf("from map: %#v\n", m)
fmt.Printf(" to struct: %#v\n", s)

}
2 changes: 0 additions & 2 deletions examples/example3/example3.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type example1 struct {
}

func main() {

s := example1{
Tuna: "hello",
Songs: []string{"hi", "nice"},
Expand All @@ -47,5 +46,4 @@ func main() {
}
fmt.Printf("from struct: %#v\n", s)
fmt.Printf(" to map: %#v\n", m)

}
24 changes: 7 additions & 17 deletions structmapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func Test_BadInputsToStructToStringMap(t *testing.T) {
}

func Test_BadInputsToStringMapToStruct(t *testing.T) {

m := map[string]string{}

a := "a"
Expand Down Expand Up @@ -77,7 +76,6 @@ type testStruct2 struct {
}

func Test_StructToStringMap(t *testing.T) {

type testdata struct {
expectedToError bool
input *testStruct
Expand Down Expand Up @@ -113,19 +111,17 @@ func Test_StructToStringMap(t *testing.T) {
if !v.expectedToError {
t.Errorf("test %d: got unexpected error - %v", i, err)
}

continue
}

if !reflect.DeepEqual(m, v.expectedMap) {
t.Errorf("test %d: unexpected result - %#v", i, m)
}

}

}

func Test_StringMapToStruct(t *testing.T) {

type testdata struct {
expectedToError bool
inputMap map[string]string
Expand Down Expand Up @@ -185,19 +181,17 @@ func Test_StringMapToStruct(t *testing.T) {
if !v.expectedToError {
t.Errorf("test %d: got unexpected error - %v", i, err)
}

continue
}

if !reflect.DeepEqual(s, v.expectedStruct) {
t.Errorf("test %d: unexpected result - %#v", i, s)
}

}

}

func Test_NonStrictStringMapToStruct(t *testing.T) {

type testdata struct {
expectedToError bool
inputMap map[string]string
Expand Down Expand Up @@ -250,19 +244,17 @@ func Test_NonStrictStringMapToStruct(t *testing.T) {
if !v.expectedToError {
t.Errorf("test %d: got unexpected error - %v", i, err)
}

continue
}

if !reflect.DeepEqual(s, v.expectedStruct) {
t.Errorf("test %d: got %#v, expected %#v", i, s, v.expectedStruct)
}

}

}

func Test_getJSONTag(t *testing.T) {

type testdata struct {
inName string
inTag string
Expand All @@ -288,13 +280,10 @@ func Test_getJSONTag(t *testing.T) {
if gotTag != v.expectedTag || gotOmitEmpty != v.expectedOmitEmpty {
t.Errorf("test %d: expected (%q,%t) got (%q,%t)", i, v.expectedTag, v.expectedOmitEmpty, gotTag, gotOmitEmpty)
}

}

}

func Test_stringToBool(t *testing.T) {

type testdata struct {
in string
expected bool
Expand All @@ -321,13 +310,10 @@ func Test_stringToBool(t *testing.T) {
if got != v.expected {
t.Errorf("test %d: expected %t got %t for %q", i, v.expected, got, v.in)
}

}

}

func Test_AnonymousFields(t *testing.T) {

type T2 struct {
F21 int `json:"f21"`
F22 []string `json:"f22"`
Expand Down Expand Up @@ -357,9 +343,11 @@ func Test_AnonymousFields(t *testing.T) {

if err != nil {
t.Errorf("StructToStringMap - unexpected error: %v", err)

return
} else if !reflect.DeepEqual(m, expected) {
t.Errorf("StructToStringMap - expected %#v got %#v", expected, m)

return
}

Expand All @@ -368,9 +356,11 @@ func Test_AnonymousFields(t *testing.T) {

if err != nil {
t.Errorf("StringMapToStruct - unexpected error: %v", err)

return
} else if !reflect.DeepEqual(t1, t2) {
t.Errorf("StringMapToStruct - expected %#v got %#v", t1, t2)

return
}
}
9 changes: 6 additions & 3 deletions to_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
// encoded.
// JSON struct tags are consulted for map key names and ommit behaviour.
func StructToStringMap(s interface{}) (map[string]string, error) {

if reflect.TypeOf(s).Kind() != reflect.Ptr || reflect.Indirect(reflect.ValueOf(s)).Kind() != reflect.Struct {
return nil, fmt.Errorf("s must be a pointer to a struct")
}
Expand All @@ -37,6 +36,7 @@ func StructToStringMap(s interface{}) (map[string]string, error) {
if err := walkValue(f); err != nil {
return err
}

continue
}

Expand All @@ -59,7 +59,7 @@ func StructToStringMap(s interface{}) (map[string]string, error) {
} else {
buf, err := json.Marshal(f.Interface())
if err != nil {
return err
return fmt.Errorf("json encoding: %w", err)
}
stringValue = string(buf)
}
Expand All @@ -68,8 +68,8 @@ func StructToStringMap(s interface{}) (map[string]string, error) {
continue
}
m[t] = stringValue

}

return nil
}

Expand All @@ -84,15 +84,18 @@ func isTime(f reflect.Value) (string, bool) {
if t.IsZero() {
return "", true
}

return t.Format(time.RFC3339), true
}
} else if f.Kind() == reflect.Ptr {
if t, ok := f.Interface().(*time.Time); ok {
if t == nil {
return "", true
}

return t.Format(time.RFC3339), true
}
}

return "", false
}
7 changes: 3 additions & 4 deletions to_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
//
// Note: bool conversion is non-strict in all cases.
func StringMapToStruct(m map[string]string, s interface{}, strict bool) error {

if reflect.TypeOf(s).Kind() != reflect.Ptr || reflect.Indirect(reflect.ValueOf(s)).Kind() != reflect.Struct {
return fmt.Errorf("s must be a pointer to a struct")
}
Expand All @@ -32,7 +31,6 @@ func StringMapToStruct(m map[string]string, s interface{}, strict bool) error {
// Iterate over the given struct and collect values into the map.
// Anonymous fields cause a recursive call to walkValue().
walkValue = func(sv reflect.Value) error {

st := sv.Type()

// Iterate over the struct looking for matches in the string map.
Expand All @@ -48,6 +46,7 @@ func StringMapToStruct(m map[string]string, s interface{}, strict bool) error {
if err := walkValue(sv.Field(i)); err != nil {
return err
}

continue
}

Expand Down Expand Up @@ -89,10 +88,10 @@ func StringMapToStruct(m map[string]string, s interface{}, strict bool) error {
if err != nil {
return err
}

}
}
}

return nil
}
if err := walkValue(reflect.ValueOf(s).Elem()); err != nil {
Expand All @@ -102,7 +101,7 @@ func StringMapToStruct(m map[string]string, s interface{}, strict bool) error {
// we now json encode m2 to make it a form which looks more like s
buf, err := json.Marshal(m2)
if err != nil {
return err
return fmt.Errorf("json encoding: %w", err)
}

// json decode fully into s
Expand Down

0 comments on commit 1ab8f23

Please sign in to comment.