Skip to content

Commit 49b4f72

Browse files
committed
propagate the FunctionTarget to the cache level and align interfaces functions declarations
1 parent c3c9b10 commit 49b4f72

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

pkg/ingresscache/ingresscache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (ic *IngressCache) Set(host, path, function string) error {
5050
return errors.Errorf("cache set failed: invalid path tree value: got: %t", urlTree)
5151
}
5252

53-
if err := ingressHostsTree.SetFunctionName(path, function); err != nil {
53+
if err := ingressHostsTree.Set(path, function); err != nil {
5454
return errors.Wrap(err, "failed to set function name in the ingress host tree")
5555
}
5656

@@ -72,7 +72,7 @@ func (ic *IngressCache) Delete(host, path, function string) error {
7272
return errors.Errorf("cache delete failed: invalid path tree value: got: %t", urlTree)
7373
}
7474

75-
if err := ingressHostsTree.DeleteFunctionName(path, function); err != nil {
75+
if err := ingressHostsTree.Delete(path, function); err != nil {
7676
return errors.Wrap(err, "failed to delete function name from the ingress host tree")
7777
}
7878

@@ -97,10 +97,10 @@ func (ic *IngressCache) Get(host, path string) ([]string, error) {
9797
return nil, errors.Errorf("cache get failed: invalid path tree value: got: %t", urlTree)
9898
}
9999

100-
result, err := ingressHostsTree.GetFunctionNames(path)
100+
result, err := ingressHostsTree.Get(path)
101101
if err != nil {
102102
return nil, errors.Wrap(err, "failed to get the function name from the ingress host tree")
103103
}
104104

105-
return result, nil
105+
return result.ToSliceString(), nil
106106
}

pkg/ingresscache/safetrie.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func NewSafeTrie() *SafeTrie {
4040
}
4141
}
4242

43-
// SetFunctionName sets a function for a given path. If the path does not exist, it creates it
44-
func (st *SafeTrie) SetFunctionName(path string, function string) error {
43+
// Set adds a function for a given path. If the path does not exist, it creates it
44+
func (st *SafeTrie) Set(path string, function string) error {
4545
if path == "" {
4646
return errors.New("path is empty")
4747
}
@@ -79,8 +79,8 @@ func (st *SafeTrie) SetFunctionName(path string, function string) error {
7979
return nil
8080
}
8181

82-
// DeleteFunctionName removes a function from a path and also deletes the path if the function is the only one associated with that path
83-
func (st *SafeTrie) DeleteFunctionName(path string, function string) error {
82+
// Delete removes a function from a path and also deletes the path if the function is the only one associated with that path
83+
func (st *SafeTrie) Delete(path string, function string) error {
8484
st.rwMutex.Lock()
8585
defer st.rwMutex.Unlock()
8686

@@ -112,8 +112,8 @@ func (st *SafeTrie) DeleteFunctionName(path string, function string) error {
112112
return nil
113113
}
114114

115-
// GetFunctionNames retrieve the closest prefix matching the path and returns the associated functions
116-
func (st *SafeTrie) GetFunctionNames(path string) ([]string, error) {
115+
// Get retrieve the closest prefix matching the path and returns the associated functions
116+
func (st *SafeTrie) Get(path string) (FunctionTarget, error) {
117117
var walkPathResult interface{}
118118
if path == "" {
119119
return nil, errors.New("path is empty")
@@ -137,7 +137,7 @@ func (st *SafeTrie) GetFunctionNames(path string) ([]string, error) {
137137
return nil, errors.Errorf("walkPathResult value should be FunctionTarget, got %v", walkPathResult)
138138
}
139139

140-
return functionNames.ToSliceString(), nil
140+
return functionNames, nil
141141
}
142142

143143
// IsEmpty return true if the SafeTrie is empty

pkg/ingresscache/safetrie_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (suite *SafeTrieTestSuite) TestPathTreeSet() {
185185
suite.Run(testCase.name, func() {
186186
testSafeTrie := suite.generateSafeTrieForTest([]safeTrieFunctionArgs{})
187187
for _, setArgs := range testCase.args {
188-
err := testSafeTrie.SetFunctionName(setArgs.path, setArgs.function)
188+
err := testSafeTrie.Set(setArgs.path, setArgs.function)
189189
if testCase.expectError {
190190
suite.Require().Error(err)
191191
suite.Require().ErrorContains(err, testCase.errorMessage)
@@ -212,53 +212,53 @@ func (suite *SafeTrieTestSuite) TestPathTreeGet() {
212212
}
213213
for _, testCase := range []struct {
214214
name string
215-
arg string
216-
expectedResult []string
215+
path string
216+
expectedResult FunctionTarget
217217
expectError bool
218218
errorMessage string
219219
}{
220220
{
221221
name: "get root path",
222-
arg: "/",
223-
expectedResult: []string{"test-function"},
222+
path: "/",
223+
expectedResult: &SingleTarget{"test-function"},
224224
}, {
225225
name: "get regular path",
226-
arg: "/path/to/function1",
227-
expectedResult: []string{"test-function1"},
226+
path: "/path/to/function1",
227+
expectedResult: &SingleTarget{"test-function1"},
228228
}, {
229229
name: "get nested path",
230-
arg: "/path/to/function1/nested",
231-
expectedResult: []string{"test-function2"},
230+
path: "/path/to/function1/nested",
231+
expectedResult: &SingleTarget{"test-function2"},
232232
}, {
233233
name: "get closest match",
234-
arg: "/path/to/function1/nested/extra",
235-
expectedResult: []string{"test-function2"},
234+
path: "/path/to/function1/nested/extra",
235+
expectedResult: &SingleTarget{"test-function2"},
236236
}, {
237237
name: "get empty path",
238-
arg: "",
238+
path: "",
239239
expectError: true,
240240
errorMessage: "path is empty",
241241
}, {
242242
name: "get closest match with different suffix",
243-
arg: "/path/to/function1/something/else",
244-
expectedResult: []string{"test-function1"},
243+
path: "/path/to/function1/something/else",
244+
expectedResult: &SingleTarget{"test-function1"},
245245
}, {
246246
name: "get path with dots",
247-
arg: "/path/./to/./function/",
248-
expectedResult: []string{"test-function1"},
247+
path: "/path/./to/./function/",
248+
expectedResult: &SingleTarget{"test-function1"},
249249
}, {
250250
name: "get path with slash",
251-
arg: "path//to//function/",
252-
expectedResult: []string{"test-function1"},
251+
path: "path//to//function/",
252+
expectedResult: &SingleTarget{"test-function1"},
253253
}, {
254254
name: "get multiple functions for the same path",
255-
arg: "/path/to/multiple/functions",
256-
expectedResult: []string{"test-function1", "test-function2"},
255+
path: "/path/to/multiple/functions",
256+
expectedResult: &CanaryTarget{[2]string{"test-function1", "test-function2"}},
257257
},
258258
} {
259259
suite.Run(testCase.name, func() {
260260
testSafeTrie := suite.generateSafeTrieForTest(initialStateGetTest)
261-
result, err := testSafeTrie.GetFunctionNames(testCase.arg)
261+
result, err := testSafeTrie.Get(testCase.path)
262262
if testCase.expectError {
263263
suite.Require().Error(err)
264264
suite.Require().ErrorContains(err, testCase.errorMessage)
@@ -343,7 +343,7 @@ func (suite *SafeTrieTestSuite) TestPathTreeDelete() {
343343
suite.Run(testCase.name, func() {
344344
testSafeTrie := suite.generateSafeTrieForTest(testCase.initialState)
345345

346-
err := testSafeTrie.DeleteFunctionName(testCase.deleteArgs.path, testCase.deleteArgs.function)
346+
err := testSafeTrie.Delete(testCase.deleteArgs.path, testCase.deleteArgs.function)
347347
if testCase.expectError {
348348
suite.Require().Error(err)
349349
suite.Require().ErrorContains(err, testCase.errorMessage)
@@ -437,7 +437,7 @@ func (suite *SafeTrieTestSuite) generateSafeTrieForTest(initialSafeTrieState []s
437437

438438
// set path tree with the provided required state
439439
for _, args := range initialSafeTrieState {
440-
err = safeTrie.SetFunctionName(args.path, args.function)
440+
err = safeTrie.Set(args.path, args.function)
441441
suite.Require().NoError(err)
442442
}
443443

pkg/ingresscache/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ type IngressHostCache interface {
3232
}
3333

3434
type IngressHostsTree interface {
35-
// SetFunctionName sets a function for a given path. Will overwrite existing values if the path already exists
36-
SetFunctionName(path string, function string) error
35+
// Set sets a function for a given path. Will overwrite existing values if the path already exists
36+
Set(path string, function string) error
3737

38-
// DeleteFunctionName removes the function from the given path and deletes the deepest suffix used only by that function; does nothing if the path or function doesn't exist.
39-
DeleteFunctionName(path string, function string) error
38+
// Delete removes the function from the given path and deletes the deepest suffix used only by that function; does nothing if the path or function doesn't exist.
39+
Delete(path string, function string) error
4040

41-
// GetFunctionNames retrieves the best matching function names for a given path based on longest prefix match
42-
GetFunctionNames(path string) ([]string, error)
41+
// Get retrieves the best matching function names for a given path based on longest prefix match
42+
Get(path string) (FunctionTarget, error)
4343

4444
// IsEmpty checks if the tree is empty
4545
IsEmpty() bool

0 commit comments

Comments
 (0)