forked from google/cel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
197 lines (176 loc) · 4.75 KB
/
map.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
// 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/ptypes/struct"
"github.com/google/cel-go/common/types/ref"
"github.com/google/cel-go/common/types/traits"
"reflect"
)
type baseMap struct {
value interface{}
refValue reflect.Value
}
// NewDynamicMap returns a traits.Mapper value with dynamic key, value pairs.
func NewDynamicMap(value interface{}) traits.Mapper {
return &baseMap{value, reflect.ValueOf(value)}
}
var (
// MapType singleton.
MapType = NewTypeValue("map",
traits.ContainerType,
traits.IndexerType,
traits.IterableType,
traits.SizerType)
)
func (m *baseMap) Contains(index ref.Value) ref.Value {
return !Bool(IsError(m.Get(index).Type()))
}
func (m *baseMap) ConvertToNative(refType reflect.Type) (interface{}, error) {
// JSON conversion.
if refType == jsonValueType || refType == jsonStructType {
jsonEntries, err :=
m.ConvertToNative(reflect.TypeOf(map[string]*structpb.Value{}))
if err != nil {
return nil, err
}
jsonMap := &structpb.Struct{
Fields: jsonEntries.(map[string]*structpb.Value)}
if refType == jsonStructType {
return jsonMap, nil
}
return &structpb.Value{
Kind: &structpb.Value_StructValue{
StructValue: jsonMap}}, nil
}
// Non-map conversion.
if refType.Kind() != reflect.Map {
return nil, fmt.Errorf("type conversion error from map to '%v'", refType)
}
// Map conversion.
thisType := m.refValue.Type()
thisKey := thisType.Key()
thisKeyKind := thisKey.Kind()
thisElem := thisType.Elem()
thisElemKind := thisElem.Kind()
otherKey := refType.Key()
otherKeyKind := otherKey.Kind()
otherElem := refType.Elem()
otherElemKind := otherElem.Kind()
if otherKeyKind == thisKeyKind && otherElemKind == thisElemKind {
return m.value, nil
}
elemCount := m.Size().(Int)
nativeMap := reflect.MakeMapWithSize(refType, int(elemCount))
it := m.Iterator()
for it.HasNext() == True {
key := it.Next()
refKeyValue, err := key.ConvertToNative(otherKey)
if err != nil {
return nil, err
}
refElemValue, err := m.Get(key).ConvertToNative(otherElem)
if err != nil {
return nil, err
}
nativeMap.SetMapIndex(
reflect.ValueOf(refKeyValue),
reflect.ValueOf(refElemValue))
}
return nativeMap.Interface(), nil
}
func (m *baseMap) ConvertToType(typeVal ref.Type) ref.Value {
switch typeVal {
case MapType:
return m
case TypeType:
return MapType
}
return NewErr("type conversion error from '%s' to '%s'", MapType, typeVal)
}
func (m *baseMap) Equal(other ref.Value) ref.Value {
if MapType != other.Type() {
return False
}
otherMap := other.(traits.Mapper)
if m.Size() != otherMap.Size() {
return False
}
it := m.Iterator()
for it.HasNext() == True {
key := it.Next()
if otherVal := otherMap.Get(key); IsError(otherVal.Type()) {
return False
} else if thisVal := m.Get(key); IsError(thisVal.Type()) {
return False
} else if thisVal.Equal(otherVal) != True {
return False
}
}
return True
}
func (m *baseMap) Get(key ref.Value) ref.Value {
thisKeyType := m.refValue.Type().Key()
nativeKey, err := key.ConvertToNative(thisKeyType)
if err != nil {
return &Err{err}
}
nativeKeyVal := reflect.ValueOf(nativeKey)
if !nativeKeyVal.Type().AssignableTo(thisKeyType) {
return NewErr("no such key: '%v'", nativeKey)
}
value := m.refValue.MapIndex(nativeKeyVal)
if !value.IsValid() {
return NewErr("no such key: '%v'", nativeKey)
}
return NativeToValue(value.Interface())
}
func (m *baseMap) Iterator() traits.Iterator {
mapKeys := m.refValue.MapKeys()
return &mapIterator{
baseIterator: &baseIterator{},
mapValue: m,
mapKeys: mapKeys,
cursor: 0,
len: int(m.Size().(Int))}
}
func (m *baseMap) Size() ref.Value {
return Int(m.refValue.Len())
}
func (m *baseMap) Type() ref.Type {
return MapType
}
func (m *baseMap) Value() interface{} {
return m.value
}
type mapIterator struct {
*baseIterator
mapValue traits.Mapper
mapKeys []reflect.Value
cursor int
len int
}
func (it *mapIterator) HasNext() ref.Value {
return Bool(it.cursor < it.len)
}
func (it *mapIterator) Next() ref.Value {
if it.HasNext() == True {
index := it.cursor
it.cursor += 1
refKey := it.mapKeys[index]
return NativeToValue(refKey.Interface())
}
return nil
}