@@ -90,27 +90,29 @@ func getMapOfAllKeyValues(s interface{}) *map[string]interface{} {
90
90
var finalMap = make (map [string ]interface {})
91
91
// iterate through the map
92
92
for k , v := range vars {
93
- switch reflect .TypeOf (v ).Kind () {
94
- // if any of them is a slice
95
- case reflect .Slice :
96
- if reflect .TypeOf (v ).Elem ().Kind () == reflect .Struct {
97
- var sliceOfMap []map [string ]interface {}
98
- s := reflect .ValueOf (v )
99
- // iterate through the slice
100
- for i := 0 ; i < s .Len (); i ++ {
101
- if s .Index (i ).CanInterface () {
102
- m := getMapOfAllKeyValues (s .Index (i ).Interface ()) // get the map value of the object, recursively
103
- if m != nil {
104
- sliceOfMap = append (sliceOfMap , * m ) // append to the slice
93
+ if reflect .TypeOf (v ) != nil {
94
+ switch reflect .TypeOf (v ).Kind () {
95
+ // if any of them is a slice
96
+ case reflect .Slice :
97
+ if reflect .TypeOf (v ).Elem ().Kind () == reflect .Struct {
98
+ var sliceOfMap []map [string ]interface {}
99
+ s := reflect .ValueOf (v )
100
+ // iterate through the slice
101
+ for i := 0 ; i < s .Len (); i ++ {
102
+ if s .Index (i ).CanInterface () {
103
+ m := getMapOfAllKeyValues (s .Index (i ).Interface ()) // get the map value of the object, recursively
104
+ if m != nil {
105
+ sliceOfMap = append (sliceOfMap , * m ) // append to the slice
106
+ }
105
107
}
106
108
}
109
+ finalMap [k ] = sliceOfMap
110
+ } else {
111
+ finalMap [k ] = v
107
112
}
108
- finalMap [k ] = sliceOfMap
109
- } else {
113
+ default :
110
114
finalMap [k ] = v
111
115
}
112
- default :
113
- finalMap [k ] = v
114
116
}
115
117
}
116
118
@@ -134,6 +136,10 @@ func buildMap(s []string, value interface{}, parent *map[string]interface{}) err
134
136
// these values can be written in dot notation to create complex nested maps
135
137
// for a more comprehensive example, please see the
136
138
func ToMap (obj interface {}, tag string ) (* map [string ]interface {}, error ) {
139
+ if obj == nil {
140
+ return nil , fmt .Errorf ("nil object passed" )
141
+ }
142
+
137
143
tagName = tag
138
144
s := getMapOfAllKeyValues (obj )
139
145
0 commit comments