Last active
October 5, 2017 09:47
-
-
Save arehmandev/87a63bf5964ce4a8833294fb831c6373 to your computer and use it in GitHub Desktop.
Best way of doing this?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "containers": [{ | |
| "path": "pathA", | |
| "components": [{ | |
| "image": "Image1", | |
| "name": "Name1" | |
| }] | |
| }, | |
| { | |
| "path": "pathB", | |
| "components": [{ | |
| "image": "Image2", | |
| "name": "Name2" | |
| }] | |
| }, | |
| { | |
| "path": "pathB", | |
| "components": [{ | |
| "image": "Image2", | |
| "name": "Name2" | |
| }] | |
| } | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "containers": [{ | |
| "path": "pathA", | |
| "components": [{ | |
| "image": "Image1", | |
| "name": "Name1" | |
| }] | |
| }, | |
| { | |
| "path": "pathB", | |
| "components": [{ | |
| "image": "Image2", | |
| "name": "Name2" | |
| }, | |
| { | |
| "image": "Image2", | |
| "name": "Name2" | |
| } | |
| ] | |
| } | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Credits to reddit.com/user/titpetric | |
| package main | |
| // If the order isn't important, you can create a map[string]Container where you would use container.Path as the key, | |
| // and append components to it when a key would exist. | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| ) | |
| // Deployment - | |
| type Deployment struct { | |
| Containers []Container `json:"containers"` | |
| } | |
| // Container - | |
| type Container struct { | |
| Path string `json:"path"` | |
| Components []Component `json:"components"` | |
| } | |
| // Component - | |
| type Component struct { | |
| Image string `json:"image"` | |
| Name string `json:"name"` | |
| } | |
| var testdata = ` | |
| { | |
| "containers": [{ | |
| "path": "pathA", | |
| "components": [{ | |
| "image": "Image1", | |
| "name": "Name1" | |
| }] | |
| }, | |
| { | |
| "path": "pathB", | |
| "components": [{ | |
| "image": "Image2", | |
| "name": "Name2" | |
| }] | |
| }, | |
| { | |
| "path": "pathB", | |
| "components": [{ | |
| "image": "Image3", | |
| "name": "Name3" | |
| }] | |
| } | |
| ] | |
| }` | |
| func main() { | |
| data := []byte(testdata) | |
| object := Deployment{} | |
| err := json.Unmarshal(data, &object) | |
| if err != nil { | |
| panic(err) | |
| } | |
| m := make(map[string]Container) | |
| // find all the paths and filter to a list | |
| for _, element := range object.Containers { | |
| path := element.Path | |
| container, ok := m[path] | |
| if ok { | |
| container.Components = append(m[path].Components, element.Components...) | |
| m[path] = container | |
| } else { | |
| m[path] = element | |
| } | |
| } | |
| // fmt.Printf(strings.Replace(fmt.Sprintf("%#v\n", m), "},", "},\n", -1)) | |
| s := make([]Container, len(m)) | |
| index := 0 | |
| for key := range m { | |
| s[index] = m[key] | |
| index++ | |
| } | |
| // fmt.Println(len(m)) | |
| Finalstruct := Deployment{Containers: s} | |
| K, err := json.MarshalIndent(Finalstruct, "", " ") // Pretty printing (using MarshalIndent with 2 spaces, rather than just Marshal) | |
| finaljson := string(K) | |
| fmt.Println(finaljson) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment