-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifier.go
117 lines (106 loc) · 3.37 KB
/
modifier.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
/*
* Copyright 2022 InfAI (CC SES)
*
* 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 modifier
import (
"encoding/json"
"github.com/SENERGY-Platform/permission-search/lib/configuration"
"github.com/SENERGY-Platform/permission-search/lib/model"
)
func New(config configuration.Config, query Query) *Modifier {
return &Modifier{
config: config,
query: query,
}
}
type Modifier struct {
config configuration.Config
query Query
}
type Query interface {
GetResourceInterface(kind string, resource string, result interface{}) (version model.ResourceVersion, err error)
}
func (this *Modifier) UsePreparedModify(preparedModify map[string][]PreparedModifyInfo, entry model.Entry, kind string, cache ModifyResourceReferenceCache) (result []model.Entry, err error) {
for _, modifier := range preparedModify[entry.Resource] {
if modifier.Unmodified {
result = append(result, entry)
} else {
temp, err := this.modifyResult(entry, kind, modifier, cache)
if err != nil {
return result, err
}
result = append(result, temp)
}
}
return result, nil
}
func (this *Modifier) PrepareListModify(ids []string) (pureIds []string, prepared map[string][]PreparedModifyInfo) {
prepared = map[string][]PreparedModifyInfo{}
pureIdSeen := map[string]bool{}
for _, id := range ids {
pureId, modifiers := SplitModifier(id)
if !pureIdSeen[pureId] {
pureIdSeen[pureId] = true
pureIds = append(pureIds, pureId)
}
prepared[pureId] = append(prepared[pureId], PreparedModifyInfo{
RawId: id,
PureId: pureId,
Unmodified: modifiers == nil || len(modifiers) == 0,
Modifier: modifiers,
})
}
return
}
type PreparedModifyInfo struct {
RawId string
PureId string
Unmodified bool
Modifier map[string][]string
}
func (this *Modifier) modifyResult(entry model.Entry, kind string, modifier PreparedModifyInfo, cache ModifyResourceReferenceCache) (result model.Entry, err error) {
var temp map[string]interface{}
buf, err := json.Marshal(entry)
if err != nil {
return result, err
}
err = json.Unmarshal(buf, &temp)
if err != nil {
return result, err
}
temp["resource"] = modifier.RawId
modifierConfigs := this.config.ResultModifiers[kind]
for _, modifierConfig := range modifierConfigs {
modifyParameter, modifierIsUsed := modifier.Modifier[modifierConfig.Modifier]
if modifierIsUsed {
if modifierConfig.Template != nil {
temp, err = this.applyResultModifyTemplate(temp, *modifierConfig.Template, kind+"."+modifierConfig.Modifier, modifyParameter, cache)
}
}
}
buf, err = json.Marshal(temp)
if err != nil {
return result, err
}
err = json.Unmarshal(buf, &result)
if err != nil {
return result, err
}
return result, nil
}
type ModifyResourceReferenceCache = *map[string]map[string]map[string]interface{}
func NewModifyResourceReferenceCache() ModifyResourceReferenceCache {
return &map[string]map[string]map[string]interface{}{}
}