/
menu.go
198 lines (178 loc) · 4.62 KB
/
menu.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package extension
import (
"encoding/json"
"fmt"
"strings"
"github.com/erda-project/erda-proto-go/core/dicehub/extension/pb"
"github.com/erda-project/erda/pkg/i18n"
)
const DicehubExtensionsMenu = "dicehub.extensions.menu"
var CategoryTypes = map[string][]string{
"action": {
"source_code_management",
"build_management",
"deploy_management",
"version_management",
"test_management",
"data_management",
"custom_task",
},
"addon": {
"database",
"distributed_cooperation",
"search",
"message",
"content_management",
"security",
"traffic_load",
"monitoring&logging",
"content",
"image_processing",
"document_processing",
"sound_processing",
"custom",
"general_ability",
"new_retail",
"srm",
"solution",
},
}
type MenuItem struct {
Name string `json:"name"`
}
func menuExtWithLocale(extensions []*pb.Extension, locale *i18n.LocaleResource) map[string][]pb.ExtensionMenu {
var result = map[string][]pb.ExtensionMenu{}
extMap := extMap(extensions)
//Traverse the big category
for categoryType, typeItems := range CategoryTypes {
//Each category belongs to a map
menuList := result[categoryType]
//Traverse the subcategories in the big category
for _, v := range typeItems {
//Obtain the object data of this subcategory Extension
extensionListWithKeyName, ok := extMap[v]
if !ok {
continue
}
//Whether this subcategory is internationalized, if not, it is the name of the word category
var displayName string
if locale != nil {
displayNameTemplate := locale.GetTemplate(DicehubExtensionsMenu + "." + categoryType + "." + v)
if displayNameTemplate != nil {
displayName = displayNameTemplate.Content()
}
}
if displayName == "" {
displayName = v
}
// Assign these word categories to the array
menuList = append(menuList, pb.ExtensionMenu{
Name: v,
DisplayName: displayName,
Items: extensionListWithKeyName,
})
}
// set array to map
result[categoryType] = menuList
}
return result
}
func menuExt(extensions []*pb.Extension, confMenu map[string][]string) interface{} {
extMap := extMap(extensions)
menuMap := &MenuMap{}
for subMenuName, subMenuValues := range confMenu {
subMenu := &MenuMap{}
for _, v := range subMenuValues {
params := strings.Split(v, ":")
keyName := params[0]
displayName := params[1]
subMenus := getMapValue(extMap, keyName)
if len(subMenus) > 0 {
subMenu.Put(displayName, subMenus)
}
}
menuMap.Put(subMenuName, subMenu)
}
return menuMap
}
func getMapValue(extMap map[string][]*pb.Extension, key string) []*pb.Extension {
extList, _ := extMap[key]
return extList
}
func extMap(extensions []*pb.Extension) map[string][]*pb.Extension {
extMap := map[string][]*pb.Extension{}
for _, v := range extensions {
extList, exist := extMap[v.Category]
if exist {
extList = append(extList, v)
} else {
extList = []*pb.Extension{v}
}
extMap[v.Category] = extList
}
return extMap
}
type MenuMap []*SortMapNode
type SortMapNode struct {
Key string
Val interface{}
}
func (m *MenuMap) Put(key string, val interface{}) {
index, _, ok := m.get(key)
if ok {
(*m)[index].Val = val
} else {
node := &SortMapNode{Key: key, Val: val}
*m = append(*m, node)
}
}
func (m *MenuMap) Get(key string) (interface{}, bool) {
_, val, ok := m.get(key)
return val, ok
}
func (m *MenuMap) get(key string) (int, interface{}, bool) {
for index, node := range *m {
if node.Key == key {
return index, node.Val, true
}
}
return -1, nil, false
}
func (m *MenuMap) MarshalJSON() ([]byte, error) {
mapJson := m.ToSortedMapJson(m)
return []byte(mapJson), nil
}
func (m *MenuMap) ToSortedMapJson(smap *MenuMap) string {
s := "{"
for _, node := range *smap {
v := node.Val
isSamp := false
str := ""
switch v.(type) {
case *MenuMap:
isSamp = true
str = smap.ToSortedMapJson(v.(*MenuMap))
}
if !isSamp {
b, _ := json.Marshal(node.Val)
str = string(b)
}
s = fmt.Sprintf("%s\"%s\":%s,", s, node.Key, str)
}
s = strings.TrimRight(s, ",")
s = fmt.Sprintf("%s}", s)
return s
}