Repository navigation
Expand file tree
/
Copy patharray.go
More file actions
152 lines (122 loc) · 3.44 KB
/
Copy patharray.go
File metadata and controls
152 lines (122 loc) · 3.44 KB
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
// Copyright 2018 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/topdown/builtins"
)
func builtinArrayConcat(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arrA, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
arrB, err := builtins.ArrayOperand(operands[1].Value, 2)
if err != nil {
return err
}
if arrA.Len() == 0 {
return iter(operands[1])
}
if arrB.Len() == 0 {
return iter(operands[0])
}
arrC := make([]*ast.Term, arrA.Len()+arrB.Len())
i := 0
arrA.Foreach(func(elemA *ast.Term) {
arrC[i] = elemA
i++
})
arrB.Foreach(func(elemB *ast.Term) {
arrC[i] = elemB
i++
})
return iter(ast.ArrayTerm(arrC...))
}
func builtinArrayFlatten(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arr, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
size := arr.Len()
preAlloc := size
containsArray := false
for i := range size {
if nested, ok := arr.Elem(i).Value.(*ast.Array); ok {
containsArray = true
preAlloc += nested.Len() - 1
}
}
if !containsArray && size == preAlloc {
return iter(operands[0]) // Empty array, or no nested arrays -> nothing to flatten.
}
flattened := make([]*ast.Term, 0, preAlloc)
for i := range size {
elem := arr.Elem(i)
if nested, ok := elem.Value.(*ast.Array); ok {
for j := range nested.Len() {
flattened = append(flattened, nested.Elem(j))
}
} else {
flattened = append(flattened, elem)
}
}
return iter(ast.ArrayTerm(flattened...))
}
func builtinArraySlice(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arr, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
startIndex, err := builtins.IntOperand(operands[1].Value, 2)
if err != nil {
return err
}
stopIndex, err := builtins.IntOperand(operands[2].Value, 3)
if err != nil {
return err
}
l := arr.Len()
// Clamp stopIndex to avoid out-of-range errors. If negative, clamp to zero.
// Otherwise, clamp to length of array.
if stopIndex < 0 {
stopIndex = 0
} else if stopIndex > l {
stopIndex = l
}
// Clamp startIndex to avoid out-of-range errors. If negative, clamp to zero.
// Otherwise, clamp to stopIndex to avoid to avoid cases like arr[1:0].
if startIndex < 0 {
startIndex = 0
} else if startIndex > stopIndex {
startIndex = stopIndex
}
if startIndex == 0 && stopIndex >= l {
return iter(operands[0])
}
return iter(ast.NewTerm(arr.Slice(startIndex, stopIndex)))
}
func builtinArrayReverse(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
arr, err := builtins.ArrayOperand(operands[0].Value, 1)
if err != nil {
return err
}
length := arr.Len()
if length == 0 {
return iter(ast.InternedEmptyArray)
}
if length == 1 {
return iter(operands[0])
}
reversedArr := make([]*ast.Term, length)
for index := range length {
reversedArr[index] = arr.Elem(length - index - 1)
}
return iter(ast.ArrayTerm(reversedArr...))
}
func init() {
RegisterBuiltinFunc(ast.ArrayConcat.Name, builtinArrayConcat)
RegisterBuiltinFunc(ast.ArrayFlatten.Name, builtinArrayFlatten)
RegisterBuiltinFunc(ast.ArraySlice.Name, builtinArraySlice)
RegisterBuiltinFunc(ast.ArrayReverse.Name, builtinArrayReverse)
}