-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfaketr.go
80 lines (66 loc) · 2.28 KB
/
faketr.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
/**
* Copyright 2019 Comcast Cable Communications Management, 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 tr18b1e
import (
"errors"
"strings"
)
type fakeLibrary struct {
lib *library
}
// NewFake creates a fakeLibrary that implements the same interface but
// has behavior that delivers fake information to satisfy tests
func NewFake() (Library, error) {
fakeLib := &fakeLibrary{}
fakeLib.lib = &library{}
fakeLib.lib.libraryData = make(map[string]*TRData)
return fakeLib, nil
}
// Populate just makes a call to the Populate implemented by library
func (m *fakeLibrary) Populate(inData []TRData) error {
return m.lib.Populate(inData)
}
// Get will return an error if given a "wildcard" it has no data for,
// it will return the value if it exists already,
// and will create a single value and return a single value if it does not yet exist
func (m *fakeLibrary) Get(key string) ([]TRData, error) {
myData, err := m.lib.Get(key)
if err != nil || len(myData) == 0 {
if (strings.LastIndex(key, ".") + 1) == len(key) {
return nil, errors.New("Using a wildcard in the fake tr18b1e w/no data supplied.")
}
m.lib.Put(key, key)
return m.lib.Get(key)
}
return myData, nil
}
// Put just does the same thing that the library put function does
func (m *fakeLibrary) Put(key string, data interface{}) error {
return m.lib.Put(key, data)
}
// Update will update the entry if it exists, and will make a new one if
// it doesn't yet exist
func (m *fakeLibrary) Update(key string, data interface{}) error {
if err := m.lib.Update(key, data); err != nil {
return m.lib.Put(key, data)
}
return m.lib.Update(key, data)
}
// Delete will delete the entry if it exists and do nothing if it does not
func (m *fakeLibrary) Delete(key string) error {
return m.lib.Delete(key)
}