@@ -12,6 +12,7 @@ Main features:
1212* Modifying fields' types and tags
1313* Easy reading of dynamic structs
1414* Mapping dynamic struct with set values to existing struct
15+ * Make slices and maps of dynamic structs
1516
1617Works out-of-the-box with:
1718* https://github.com/go-playground/form
@@ -270,4 +271,146 @@ func main() {
270271 // <nil> {123 example 123.45}
271272 // <nil> {true [1 2 3] }
272273}
274+ ```
275+
276+ ## Make a slice of dynamic struct
277+
278+ ``` go
279+ package main
280+
281+ import (
282+ " encoding/json"
283+ " fmt"
284+ " log"
285+
286+ " github.com/ompluscator/dynamic-struct"
287+ )
288+
289+ type Data struct {
290+ Integer int ` json:"int"`
291+ Text string ` json:"someText"`
292+ Float float64 ` json:"double"`
293+ Boolean bool
294+ Slice []int
295+ Anonymous string ` json:"-"`
296+ }
297+
298+ func main () {
299+ definition := dynamicstruct.ExtendStruct (Data{}).Build ()
300+
301+ slice := definition.NewSliceOfStructs ()
302+
303+ data := []byte (`
304+ [
305+ {
306+ "int": 123,
307+ "someText": "example",
308+ "double": 123.45,
309+ "Boolean": true,
310+ "Slice": [1, 2, 3],
311+ "Anonymous": "avoid to read"
312+ }
313+ ]
314+ ` )
315+
316+ err := json.Unmarshal (data, &slice)
317+ if err != nil {
318+ log.Fatal (err)
319+ }
320+
321+ data, err = json.Marshal (slice)
322+ if err != nil {
323+ log.Fatal (err)
324+ }
325+
326+ fmt.Println (string (data))
327+ // Out:
328+ // [{"Boolean":true,"Slice":[1,2,3],"int":123,"someText":"example","double":123.45}]
329+
330+ reader := dynamicstruct.NewReader (slice)
331+ readersSlice := reader.ToSliceOfReaders ()
332+ for k , v := range readersSlice {
333+ var value Data
334+ err := v.ToStruct (&value)
335+ if err != nil {
336+ log.Fatal (err)
337+ }
338+
339+ fmt.Println (k, value)
340+ }
341+ // Out:
342+ // 0 {123 example 123.45 true [1 2 3] }
343+ }
344+
345+ ```
346+
347+ ## Make a map of dynamic struct
348+
349+ ``` go
350+ package main
351+
352+ import (
353+ " encoding/json"
354+ " fmt"
355+ " log"
356+
357+ " github.com/ompluscator/dynamic-struct"
358+ )
359+
360+ type Data struct {
361+ Integer int ` json:"int"`
362+ Text string ` json:"someText"`
363+ Float float64 ` json:"double"`
364+ Boolean bool
365+ Slice []int
366+ Anonymous string ` json:"-"`
367+ }
368+
369+ func main () {
370+ definition := dynamicstruct.ExtendStruct (Data{}).Build ()
371+
372+ mapWithStringKey := definition.NewMapOfStructs (" " )
373+
374+ data := []byte (`
375+ {
376+ "element": {
377+ "int": 123,
378+ "someText": "example",
379+ "double": 123.45,
380+ "Boolean": true,
381+ "Slice": [1, 2, 3],
382+ "Anonymous": "avoid to read"
383+ }
384+ }
385+ ` )
386+
387+ err := json.Unmarshal (data, &mapWithStringKey)
388+ if err != nil {
389+ log.Fatal (err)
390+ }
391+
392+ data, err = json.Marshal (mapWithStringKey)
393+ if err != nil {
394+ log.Fatal (err)
395+ }
396+
397+ fmt.Println (string (data))
398+ // Out:
399+ // {"element":{"int":123,"someText":"example","double":123.45,"Boolean":true,"Slice":[1,2,3]}}
400+
401+ reader := dynamicstruct.NewReader (mapWithStringKey)
402+ readersMap := reader.ToMapReaderOfReaders ()
403+ for k , v := range readersMap {
404+ var value Data
405+ err := v.ToStruct (&value)
406+ if err != nil {
407+ log.Fatal (err)
408+ }
409+
410+ fmt.Println (k, value)
411+ }
412+ // Out:
413+ // element {123 example 123.45 true [1 2 3] }
414+ }
415+
273416```
0 commit comments