Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
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
54
55
56
57
58
// Copyright 2020 Jebbs. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
 
package merge
 
func getTag(v map[string]interface{}) string {
    if field, ok := v["tag"]; ok {
        if t, ok := field.(string); ok {
            return t
        }
    }
    if field, ok := v[tagKey]; ok {
        if t, ok := field.(string); ok {
            return t
        }
    }
    return ""
}
 
func mergeSameTag(s []interface{}) ([]interface{}, error) {
    // from: [a,"",b,"",a,"",b,""]
    // to: [a,"",b,"",merged,"",merged,""]
    merged := &struct{}{}
    for i, item1 := range s {
        map1, ok := item1.(map[string]interface{})
        if !ok {
            continue
        }
        tag1 := getTag(map1)
        if tag1 == "" {
            continue
        }
        for j := i + 1; j < len(s); j++ {
            map2, ok := s[j].(map[string]interface{})
            if !ok {
                continue
            }
            tag2 := getTag(map2)
            if tag1 == tag2 {
                s[j] = merged
                err := mergeMaps(map1, map2)
                if err != nil {
                    return nil, err
                }
            }
        }
    }
    // remove merged
    ns := make([]interface{}, 0)
    for _, item := range s {
        if item == merged {
            continue
        }
        ns = append(ns, item)
    }
    return ns, nil
}