@@ -17,6 +17,13 @@ Table of Contents
1717 - [ Without trailing array] ( #without-trailing-array )
1818 - [ With trailing array] ( #with-trailing-array )
1919 * [ Delete Key By Path] ( #delete-key-by-path )
20+ * [Convert Utils](#convert-utils)
21+ + [Get map of strings from interface](#get-map-of-strings-from-interface)
22+ - [Get map directly from a GetPath object](#get-map-directly-from-a-getpath-object)
23+ - [Get map manually](#get-map-manually)
24+ + [Get array of string from interface](#get-array-of-string-from-interface)
25+ - [Get array directly from a GetPath object](#get-array-directly-from-a-getpath-object)
26+ - [Get array manually](#get-array-manually)
2027
2128## Features
2229
@@ -194,3 +201,134 @@ if err != nil {
194201 logger.Fatalf(err.Error())
195202}
196203```
204+
205+ ### Convert Utils
206+
207+ Convert simply automate the need to
208+ explicitly do assertion each time we need to access
209+ an interface object.
210+
211+ Let us assume we have the following YAML structure
212+
213+ ``` yaml
214+
215+ to :
216+ array-1 :
217+ key-1 :
218+ - key-2 : 2
219+ - key-3 : 3
220+ - key-4 : 4
221+ array-2 :
222+ - 1
223+ - 2
224+ - 3
225+ - 4
226+ - 5
227+ array-3 :
228+ - key-1 : 1
229+ - key-2 : 2
230+
231+ ```
232+
233+ #### Get map of strings from interface
234+
235+ We can do this in two ways, get object by giving a path and assert the interface to ` map[string]string ` , or work manually our way to the object
236+
237+ ##### Get map directly from a GetPath object
238+
239+ To get map ** key-2: 2** , first get object via GetPath
240+
241+ ``` go
242+
243+ obj , err := state.GetPath (" to.array-1.key-1.[0]" )
244+ if err != nil {
245+ logger.Fatalf (err.Error ())
246+ }
247+ logger.Info (val)
248+
249+ ```
250+
251+ Next, assert ** obj** as ` map[string]string `
252+
253+ ``` go
254+
255+ assertData := db.NewConvertFactory ()
256+
257+ assertData.Input (val)
258+ if assertData.Error != nil {
259+ logger.Fatalf (assertData.Error .Error ())
260+ }
261+ vMap := assertData.GetMap ()
262+ logger.Info (vMap[" key-2" ])
263+
264+ ```
265+
266+ ##### Get map manually
267+
268+ We can get the map manually by using only ** Convert** operations
269+
270+ ``` go
271+
272+ assertData := db.NewConvertFactory ()
273+
274+ assertData.Input (state.Data ).
275+ Key (" to" ).
276+ Key (" array-1" ).
277+ Key (" key-1" ).Index (0 )
278+ if assertData.Error != nil {
279+ logger.Fatalf (assertData.Error .Error ())
280+ }
281+ vMap := assertData.GetMap ()
282+ logger.Info (vMap[" key-2" ])
283+
284+ ```
285+
286+ #### Get array of string from interface
287+
288+ Again here we can do it two ways as with the map example
289+
290+ ##### Get array directly from a GetPath object
291+
292+ To get ** array-2** as ** [ ] string** , first get object via GetPath
293+
294+ ``` go
295+
296+ obj, err = state.GetPath (" to.array-2" )
297+ if err != nil {
298+ logger.Fatalf (err.Error ())
299+ }
300+ logger.Info (obj)
301+
302+ ```
303+
304+ Next, assert ** obj** as ` []string `
305+
306+ ``` go
307+
308+ assertData := db.NewConvertFactory ()
309+
310+ assertData.Input (obj)
311+ if assertData.Error != nil {
312+ logger.Fatalf (assertData.Error .Error ())
313+ }
314+ vArray := assertData.GetArray ()
315+ logger.Info (vArray)
316+
317+ ```
318+
319+ ##### Get array manually
320+
321+ We can get the array manually by using only ** Convert** operations
322+
323+ ``` go
324+
325+ assertData.Input (state.Data ).
326+ Key (" to" ).
327+ Key (" array-2" )
328+ if assertData.Error != nil {
329+ logger.Fatalf (assertData.Error .Error ())
330+ }
331+ vArray := assertData.GetArray ()
332+ logger.Info (vArray)
333+
334+ ```
0 commit comments