From 0a7967a68b63d4b4bae4787847efef98ef2467a5 Mon Sep 17 00:00:00 2001 From: yuzhipeng Date: Thu, 14 Mar 2024 16:12:35 +0800 Subject: [PATCH] Add func for selecting and mutating map element (#554) Signed-off-by: yuzhipeng --- maps/maps_suite_test.go | 29 ++++++++++++++++++ maps/select.go | 35 +++++++++++++++++++++ maps/select_test.go | 67 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 maps/maps_suite_test.go create mode 100644 maps/select.go create mode 100644 maps/select_test.go diff --git a/maps/maps_suite_test.go b/maps/maps_suite_test.go new file mode 100644 index 00000000..659e82c1 --- /dev/null +++ b/maps/maps_suite_test.go @@ -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") +} diff --git a/maps/select.go b/maps/select.go new file mode 100644 index 00000000..a6984939 --- /dev/null +++ b/maps/select.go @@ -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 +} diff --git a/maps/select_test.go b/maps/select_test.go new file mode 100644 index 00000000..829cf935 --- /dev/null +++ b/maps/select_test.go @@ -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", + }, + ), + ) +})