forked from google/cel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_list.go
179 lines (159 loc) · 4.5 KB
/
json_list.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
// Copyright 2018 Google LLC
//
// 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 types
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/struct"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/common/types/traits"
"reflect"
)
var (
jsonListValueType = reflect.TypeOf(&structpb.ListValue{})
)
type jsonListValue struct {
*structpb.ListValue
}
// NewJsonList creates a traits.Lister implementation backed by a JSON list
// that has been encoded in protocol buffer form.
func NewJsonList(l *structpb.ListValue) traits.Lister {
return &jsonListValue{l}
}
func (l *jsonListValue) Add(other ref.Value) ref.Value {
if other.Type() != ListType {
return NewErr("no such overload")
}
switch other.(type) {
case *jsonListValue:
otherList := other.(*jsonListValue)
concatElems := append(l.GetValues(), otherList.GetValues()...)
return NewJsonList(&structpb.ListValue{Values: concatElems})
}
return &concatList{
prevList: l,
nextList: other.(traits.Lister)}
}
func (l *jsonListValue) Contains(elem ref.Value) ref.Value {
for i := Int(0); i < l.Size().(Int); i++ {
if l.Get(i).Equal(elem) == True {
return True
}
}
return False
}
func (l *jsonListValue) ConvertToNative(typeDesc reflect.Type) (interface{}, error) {
switch typeDesc.Kind() {
case reflect.Array, reflect.Slice:
elemCount := int(l.Size().(Int))
nativeList := reflect.MakeSlice(typeDesc, elemCount, elemCount)
for i := 0; i < elemCount; i++ {
elem := l.Get(Int(i))
nativeElemVal, err := elem.ConvertToNative(typeDesc.Elem())
if err != nil {
return nil, err
}
nativeList.Index(i).Set(reflect.ValueOf(nativeElemVal))
}
return nativeList.Interface(), nil
case reflect.Ptr:
switch typeDesc {
case jsonValueType:
return &structpb.Value{
Kind: &structpb.Value_ListValue{
ListValue: l.ListValue}}, nil
case jsonListValueType:
return l.ListValue, nil
case anyValueType:
return ptypes.MarshalAny(l.Value().(proto.Message))
}
case reflect.Interface:
// If the list is already assignable to the desired type return it.
if reflect.TypeOf(l).Implements(typeDesc) {
return l, nil
}
}
return nil, fmt.Errorf("no conversion found from list type to native type."+
" list elem: google.protobuf.Value, native type: %v", typeDesc)
}
func (l *jsonListValue) ConvertToType(typeVal ref.Type) ref.Value {
switch typeVal {
case ListType:
return l
case TypeType:
return ListType
}
return NewErr("type conversion error from '%s' to '%s'", ListType, typeVal)
}
func (l *jsonListValue) Equal(other ref.Value) ref.Value {
if ListType != other.Type() {
return False
}
otherList := other.(traits.Lister)
if l.Size() != otherList.Size() {
return False
}
for i := IntZero; i < l.Size().(Int); i++ {
thisElem := l.Get(i)
otherElem := otherList.Get(i)
if thisElem.Equal(otherElem) != True {
return False
}
}
return True
}
func (l *jsonListValue) Get(index ref.Value) ref.Value {
if IntType != index.Type() {
return NewErr("unsupported index type: '%v", index.Type())
}
i := index.(Int)
if i < 0 || i >= l.Size().(Int) {
return NewErr("index '%d' out of range in list size '%d'", i, l.Size())
}
elem := l.GetValues()[i]
return NativeToValue(elem)
}
func (l *jsonListValue) Iterator() traits.Iterator {
return &jsonValueListIterator{
baseIterator: &baseIterator{},
elems: l.GetValues(),
len: len(l.GetValues())}
}
func (l *jsonListValue) Size() ref.Value {
return Int(len(l.GetValues()))
}
func (l *jsonListValue) Type() ref.Type {
return ListType
}
func (l *jsonListValue) Value() interface{} {
return l.ListValue
}
type jsonValueListIterator struct {
*baseIterator
cursor int
elems []*structpb.Value
len int
}
func (it *jsonValueListIterator) HasNext() ref.Value {
return Bool(it.cursor < it.len)
}
func (it *jsonValueListIterator) Next() ref.Value {
if it.HasNext() == True {
index := it.cursor
it.cursor += 1
return NativeToValue(it.elems[index])
}
return nil
}