Skip to content

Commit

Permalink
Add func for selecting and mutating map element (#554)
Browse files Browse the repository at this point in the history
Signed-off-by: yuzhipeng <[email protected]>
  • Loading branch information
yuzp1996 authored Mar 14, 2024
1 parent 8380168 commit 0a7967a
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
29 changes: 29 additions & 0 deletions maps/maps_suite_test.go
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")
}
35 changes: 35 additions & 0 deletions maps/select.go
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
}
67 changes: 67 additions & 0 deletions maps/select_test.go
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",
},
),
)
})

0 comments on commit 0a7967a

Please sign in to comment.