@@ -21,10 +21,8 @@ func MapValueToLocation(mapToSearch map[string]interface{}, location, value stri
2121 // Iterate over the map and search for the location
2222 for key , mapValue := range mapToSearch {
2323 if key != locationParts [0 ] {
24- log .Printf ("BAD LOCATIONPART: %#v -> %#v" , key , locationParts [0 ])
2524 continue
2625 }
27- log .Printf ("GOOD LOCATIONPART: %#v -> %#v" , key , locationParts [0 ])
2826
2927 if len (locationParts ) == 1 {
3028 // We've reached the end of the location, set the value
@@ -41,16 +39,118 @@ func MapValueToLocation(mapToSearch map[string]interface{}, location, value stri
4139 // So in this case, the 'content' itself is an array.
4240 // This means we need to check the NEXT variable, which should be
4341 // #, #0, #1, #0-2 etc.
44- log .Printf ("[ERROR] Schemaless handling []interface{} with arbitary values. This MAY not work. '%#v' -> %#v" , key , mapValue )
45- newMap [key ] = make ([]interface {}, 0 )
42+ // FIXME: check the NEXT key for LOOP variables to put it in the RIGHT index
43+ // if it's in #, put it in ALL
44+ correctIndexes := []int {}
45+ if len (locationParts ) > 1 && strings .HasPrefix (locationParts [1 ], "#" ) {
46+ if locationParts [1 ] == "#" {
47+ for i , _ := range val {
48+ correctIndexes = append (correctIndexes , i )
49+ }
50+ } else if strings .Contains (locationParts [1 ], "-" ) {
51+ // Split the string into parts
52+ parts := strings .Split (locationParts [1 ], "-" )
53+ if len (parts ) != 2 {
54+ log .Printf ("[ERROR] Schemaless: Bad loop mapping with key %#v -> %#v (1)" , locationParts [0 ], locationParts [1 ])
55+ continue
56+ }
57+
58+ // Get the start and end indexes
59+ startIndex := strings .TrimPrefix (parts [0 ], "#" )
60+ endIndex := strings .TrimPrefix (parts [1 ], "#" )
61+ startIndexInt := 0
62+ endIndexInt := 0
63+
64+ _ , err := fmt .Sscanf (startIndex , "%d" , & startIndexInt )
65+ if err != nil {
66+ if len (startIndex ) == 0 || startIndex == "min" {
67+ startIndexInt = 0
68+ } else {
69+ log .Printf ("[ERROR] Schemaless: Bad loop mapping with key %#v -> %#v (2)" , locationParts [0 ], locationParts [1 ])
70+ continue
71+ }
72+ }
73+
74+ _ , err = fmt .Sscanf (endIndex , "%d" , & endIndexInt )
75+ if err != nil {
76+ if len (endIndex ) == 0 || endIndex == "max" {
77+ endIndexInt = len (val ) - 1
78+ } else {
79+ log .Printf ("[ERROR] Schemaless: Bad loop mapping with key %#v -> %#v (3)" , locationParts [0 ], locationParts [1 ])
80+ continue
81+ }
82+ }
83+
84+ // Check if the start and end indexes are valid
85+ if startIndexInt < 0 {
86+ log .Printf ("[ERROR] Schemaless: Bad loop mapping with key %#v -> %#v (4)" , locationParts [0 ], locationParts [1 ])
87+ continue
88+ }
89+
90+ // Add the indexes to the array
91+ for i := startIndexInt ; i <= endIndexInt ; i ++ {
92+ correctIndexes = append (correctIndexes , i )
93+ }
94+ } else if strings .Contains (locationParts [0 ], "#" ) {
95+ // Find the number after # and use it statically
96+ index := strings .TrimPrefix (locationParts [1 ], "#" )
97+ indexInt := 0
98+ _ , err := fmt .Sscanf (index , "%d" , & indexInt )
99+ if err != nil {
100+ if index == "min" {
101+ indexInt = 0
102+ } else if index == "max" {
103+ indexInt = len (val ) - 1
104+ } else {
105+ log .Printf ("[ERROR] Schemaless: Bad loop mapping with key %#v -> %#v (5)" , locationParts [0 ], locationParts [1 ])
106+ continue
107+ }
108+ }
109+
110+ // Check if the index is valid
111+ if indexInt < 0 {
112+ log .Printf ("[ERROR] Schemaless: Bad loop mapping with key %#v -> %#v (6)" , locationParts [0 ], locationParts [1 ])
113+ continue
114+ }
115+
116+ // Add the index to the array
117+ correctIndexes = append (correctIndexes , indexInt )
118+ } else {
119+ for i , _ := range val {
120+ correctIndexes = append (correctIndexes , i )
121+ }
122+ }
123+
124+ } else {
125+ // Add all indexes to the array (lol)
126+ for i , _ := range val {
127+ correctIndexes = append (correctIndexes , i )
128+ }
129+ }
130+
131+ //newMap[key] = make([]interface{}, 0)
132+ loopMap := make ([]interface {}, 0 )
46133 for i , v := range val {
47- log .Printf ("%#v: %#v" , i , v )
134+ foundIndex := false
135+ for _ , index := range correctIndexes {
136+ if i == index {
137+ foundIndex = true
138+ break
139+ }
140+ }
141+
142+ if ! foundIndex {
143+ loopMap = append (loopMap , v )
144+ continue
145+ }
146+
147+
48148 if subValue , ok := v .(map [string ]interface {}); ok {
49149 //newMap[key] = append(subValue[key].([]interface{}), mapValue)
50150 //_ = subValue
51151 splitLocationParts := locationParts [1 :]
52152 if len (splitLocationParts ) < 2 {
53- log .Printf ("[ERROR] Schemaless: handling []interface{} with arbitary values. This MAY not work. '%#v' -> %#v" , key , mapValue )
153+ log .Printf ("[ERROR] Schemaless: (1) handling []interface{} with arbitary values. This MAY not work. '%#v' -> %#v" , key , mapValue )
54154 continue
55155 }
56156
@@ -59,18 +159,19 @@ func MapValueToLocation(mapToSearch map[string]interface{}, location, value stri
59159 splitLocationParts = splitLocationParts [1 :]
60160 }
61161
62- splitLocationString := strings .Join (splitLocationParts [1 :], "." )
63- log .Printf ("\n \n SPLIT LOCATION PARTs: %#v\n \n String: %s" , splitLocationParts , splitLocationString )
64- loopResp := MapValueToLocation (subValue , splitLocationString , value )
65- log .Printf ("Loop response: %#v" , loopResp )
66- newMap [key ] = loopResp
162+ // Recurse
163+ loopMap = append (loopMap , MapValueToLocation (subValue , strings .Join (splitLocationParts , "." ), value ))
67164
68165 } else {
69- log .Printf ("[ERROR] Schemaless: No LOOP sub-handler for type %#v. Value: %#v" , reflect .TypeOf (v ).String (), v )
166+ //if debug {
167+ // log.Printf("[DEBUG] Schemaless: No LOOP sub-handler for replacing values of type %#v. Value: %#v", reflect.TypeOf(v).String(), v)
168+ //}
169+
170+ loopMap = append (loopMap , v )
70171 }
71172 }
72173
73- mapToSearch [key ] = newMap
174+ mapToSearch [key ] = loopMap
74175 continue
75176 } else {
76177 log .Printf ("[ERROR] Schemaless handling unknown type %#v. Value: %#v" , reflect .TypeOf (mapValue ).String (), value )
0 commit comments