-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add func for selecting and mutating map element (#554)
Signed-off-by: yuzhipeng <[email protected]>
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2024 The Katanomi 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 maps | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestMaps(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Maps Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2024 The Katanomi 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 maps | ||
|
||
// MatchFunc return true if key and value match condition | ||
type MatchFunc func(key, value string) bool | ||
|
||
// MutateFunc return mutated key and value | ||
type MutateFunc func(key, value string) (string, string) | ||
|
||
// SelectAndMutateMap select elements from a map based on a match function and mutate it's key and value | ||
func SelectAndMutateMap(maps map[string]string, match MatchFunc, mutate MutateFunc) map[string]string { | ||
result := make(map[string]string) | ||
for key, value := range maps { | ||
if match(key, value) { | ||
key, value = mutate(key, value) | ||
result[key] = value | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2024 The Katanomi 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 maps contains methods to operate maps of all kinds | ||
package maps | ||
|
||
import ( | ||
"strings" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var source = map[string]string{ | ||
"core-1": "1", | ||
"core-2": "2", | ||
"core-3": "3", | ||
} | ||
var _ = Describe("SelectAndMutateMap", func() { | ||
DescribeTable("SelectAndMutateMap in different situation", | ||
func(source map[string]string, match MatchFunc, mutate MutateFunc, expect map[string]string) { | ||
result := SelectAndMutateMap(source, match, mutate) | ||
Expect(result).To(Equal(expect)) | ||
}, | ||
Entry("return all the element", | ||
source, | ||
func(key string, value string) bool { return true }, | ||
func(key string, value string) (string, string) { return key, value }, | ||
source, | ||
), | ||
Entry("return core-1 element", | ||
source, | ||
func(key string, value string) bool { | ||
return key == "core-1" | ||
}, | ||
func(key string, value string) (string, string) { return key, value }, | ||
map[string]string{ | ||
"core-1": "1", | ||
}, | ||
), | ||
Entry("return mutated core-1 element", | ||
source, | ||
func(key string, value string) bool { | ||
return key == "core-1" | ||
}, | ||
func(key string, value string) (string, string) { | ||
return strings.TrimPrefix(key, "core-"), value | ||
}, | ||
map[string]string{ | ||
"1": "1", | ||
}, | ||
), | ||
) | ||
}) |