-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ptr.go
139 lines (110 loc) · 2.24 KB
/
ptr.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
package logs
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
"github.com/open-policy-agent/opa/util"
)
type ptr []string
func parsePtr(str string) (ptr, error) {
if len(str) == 0 {
return nil, fmt.Errorf("mask must be non-empty")
} else if str[0] != '/' {
return nil, fmt.Errorf("mask must be slash-prefixed")
}
parts := strings.Split(str[1:], "/")
if parts[0] != "input" && parts[0] != "result" {
return nil, fmt.Errorf("mask prefix not allowed")
}
for i := range parts {
var err error
parts[i], err = url.QueryUnescape(parts[i])
if err != nil {
return nil, err
}
}
return ptr(parts), nil
}
func (p ptr) String() string {
escaped := make([]string, len(p))
for i := range escaped {
escaped[i] = url.QueryEscape(p[i])
}
return "/" + strings.Join(escaped, "/")
}
func (p ptr) Erase(event *EventV1) {
if len(p) == 1 {
switch p[0] {
case "input":
event.Input = nil
case "result":
event.Result = nil
default:
panic("illegal value")
}
} else {
var node interface{}
switch p[0] {
case "input":
if event.Input != nil {
node = *event.Input
}
case "result":
if event.Result != nil {
node = *event.Result
}
}
parent := p[1 : len(p)-1].lookup(node)
parentObj, ok := parent.(map[string]interface{})
if !ok {
return
}
fld := p[len(p)-1]
if _, ok := parentObj[fld]; !ok {
return
}
delete(parentObj, fld)
}
event.Erased = append(event.Erased, p.String())
}
func (p ptr) lookup(node interface{}) interface{} {
for i := 0; i < len(p); i++ {
switch v := node.(type) {
case map[string]interface{}:
var ok bool
if node, ok = v[p[i]]; !ok {
return nil
}
case []interface{}:
idx, err := strconv.Atoi(p[i])
if err != nil {
return nil
} else if idx < 0 || idx >= len(v) {
return nil
}
node = v[idx]
default:
return nil
}
}
return node
}
func resultValueToPtrs(rv interface{}) ([]ptr, error) {
bs, err := json.Marshal(rv)
if err != nil {
return nil, err
}
var value []string
if err := util.Unmarshal(bs, &value); err != nil {
return nil, err
}
result := make([]ptr, len(value))
for i := range result {
if result[i], err = parsePtr(value[i]); err != nil {
return nil, err
}
}
return result, nil
}