/
composer.go
53 lines (40 loc) · 1.05 KB
/
composer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"encoding/json"
"github.com/jinzhu/copier"
)
func optimizeComposerVersions(versions []map[string]interface{}) []map[string]interface{} {
newVersionList := make([]map[string]interface{}, 0)
var firstVersion map[string]interface{}
for _, version := range versions {
if len(firstVersion) == 0 {
copier.Copy(&firstVersion, &version)
newVersionList = append(newVersionList, version)
continue
}
newVersion := make(map[string]interface{})
for key, val := range version {
firstVersionVal, ok := firstVersion[key]
if !ok {
firstVersion[key] = val
newVersion[key] = val
} else {
firstVal, _ := json.Marshal(firstVersionVal)
secondVal, _ := json.Marshal(val)
if string(firstVal) != string(secondVal) {
firstVersion[key] = val
newVersion[key] = val
}
}
}
for key, _ := range firstVersion {
_, ok := version[key]
if !ok {
newVersion[key] = "__unset"
delete(firstVersion, key)
}
}
newVersionList = append(newVersionList, newVersion)
}
return newVersionList
}