/
fakebag.go
124 lines (102 loc) · 3.17 KB
/
fakebag.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
// Copyright 2017 Istio Authors
//
// 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 ilt
import (
"fmt"
"sort"
"sync"
"istio.io/istio/mixer/pkg/attribute"
"istio.io/istio/mixer/pkg/il"
)
// NewFakeBag creates a FakeBag and converts map[string]string to StringMap
func NewFakeBag(attrs map[string]interface{}) *FakeBag {
bag := &FakeBag{
Attrs: make(map[string]interface{}, len(attrs)),
}
for k, v := range attrs {
if sm, ok := v.(map[string]string); ok {
bag.Attrs[k] = NewStringMap(k, sm, bag)
} else {
bag.Attrs[k] = v
}
}
bag.referenced = make(map[string]bool)
return bag
}
// FakeBag is a fake implementation of the Bag for testing purposes.
type FakeBag struct {
Attrs map[string]interface{}
referenced map[string]bool
referencedLock sync.RWMutex
}
var _ attribute.Bag = (*FakeBag)(nil)
// Get returns an attribute value.
func (b *FakeBag) Get(name string) (interface{}, bool) {
c, found := b.Attrs[name]
b.referencedLock.Lock()
b.referenced[name] = found
b.referencedLock.Unlock()
return c, found
}
// Names return the names of all the attributes known to this bag.
func (b *FakeBag) Names() []string {
return []string{}
}
// Done indicates the bag can be reclaimed.
func (b *FakeBag) Done() {}
// Contains returns true if the key is present in the bag.
func (b *FakeBag) Contains(key string) bool {
_, found := b.Attrs[key]
return found
}
// String is needed to implement the Bag interface.
func (b *FakeBag) String() string { return "" }
// ReferencedList returns the sorted list of attributes that were referenced. Attribute references through
// string maps are encoded as mapname[keyname]. Absent values are prefixed with "-".
func (b *FakeBag) ReferencedList() []string {
attributes := make([]string, 0, len(b.referenced))
b.referencedLock.RLock()
for k, found := range b.referenced {
attr := k
if !found {
attr = "-" + attr
}
attributes = append(attributes, attr)
}
b.referencedLock.RUnlock()
sort.Strings(attributes)
return attributes
}
// NewStringMap creates an il.StringMap given map[string]string
func NewStringMap(name string, entries map[string]string, parent *FakeBag) il.StringMap {
return stringMap{Name: name, Entries: entries, parent: parent}
}
type stringMap struct {
// Name of the stringmap -- request.headers
Name string
// Entries in the stringmap
Entries map[string]string
parent *FakeBag
}
// Get returns a stringmap value and records access
func (s stringMap) Get(key string) (string, bool) {
str, found := s.Entries[key]
if s.parent != nil {
name := fmt.Sprintf(`%s[%s]`, s.Name, key)
s.parent.referencedLock.Lock()
s.parent.referenced[name] = found
s.parent.referencedLock.Unlock()
}
return str, found
}