forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up functions and add slice utility functions (GoogleCloudPlatfo…
…rm#10210) * Clean up function all_resourcerefs * Add slice utility functions * Change sensitive_props as resource method
- Loading branch information
1 parent
9c285f1
commit 2b70f10
Showing
4 changed files
with
185 additions
and
44 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
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,41 @@ | ||
// Copyright 2024 Google Inc. | ||
// 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 google | ||
|
||
// Returns a new slice containing all of the elements | ||
// for which the test function returns true in the original slice | ||
func Select[T any](S []T, test func(T) bool) (ret []T) { | ||
for _, s := range S { | ||
if test(s) { | ||
ret = append(ret, s) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Returns a new slice containing all of the elements | ||
// for which the test function returns false in the original slice | ||
func Reject[T any](S []T, test func(T) bool) (ret []T) { | ||
for _, s := range S { | ||
if !test(s) { | ||
ret = append(ret, s) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Concat two slices | ||
func Concat[T any](S1 []T, S2 []T) (ret []T) { | ||
return append(S1, S2...) | ||
} |
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,138 @@ | ||
package google | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestSliceSelect(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
description string | ||
S []int | ||
testFun func(int) bool | ||
expected int | ||
}{ | ||
{ | ||
description: "interger slice selects even numbers", | ||
S: []int{0, 1, 2}, | ||
testFun: func(n int) bool { | ||
return n%2 == 0 | ||
}, | ||
expected: 2, | ||
}, | ||
{ | ||
description: "empty slice", | ||
S: make([]int, 0), | ||
testFun: func(n int) bool { | ||
return n%2 == 0 | ||
}, | ||
expected: 0, | ||
}, | ||
{ | ||
description: "nil slice", | ||
S: nil, | ||
testFun: func(n int) bool { | ||
return n%2 == 0 | ||
}, | ||
expected: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
|
||
t.Run(tc.description, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if got, want := len(Select(tc.S, tc.testFun)), tc.expected; !reflect.DeepEqual(got, want) { | ||
t.Errorf("expected %v to be %v", got, want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSliceReject(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
description string | ||
S []int | ||
testFun func(int) bool | ||
expected int | ||
}{ | ||
{ | ||
description: "interger slice rejects even numbers", | ||
S: []int{0, 1, 2}, | ||
testFun: func(n int) bool { | ||
return n%2 == 0 | ||
}, | ||
expected: 1, | ||
}, | ||
{ | ||
description: "empty slice", | ||
S: make([]int, 0), | ||
testFun: func(n int) bool { | ||
return n%2 == 0 | ||
}, | ||
expected: 0, | ||
}, | ||
{ | ||
description: "nil slice", | ||
S: nil, | ||
testFun: func(n int) bool { | ||
return n%2 == 0 | ||
}, | ||
expected: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
|
||
t.Run(tc.description, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if got, want := len(Reject(tc.S, tc.testFun)), tc.expected; !reflect.DeepEqual(got, want) { | ||
t.Errorf("expected %v to be %v", got, want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSliceConcat(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
description string | ||
S1 []int | ||
S2 []int | ||
expected int | ||
}{ | ||
{ | ||
description: "interger slice rejects even numbers", | ||
S1: []int{0, 1, 2}, | ||
S2: []int{3, 4}, | ||
expected: 5, | ||
}, | ||
{ | ||
description: "empty slice", | ||
S1: nil, | ||
S2: make([]int, 0), | ||
expected: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
tc := tc | ||
|
||
t.Run(tc.description, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if got, want := len(Concat(tc.S1, tc.S2)), tc.expected; !reflect.DeepEqual(got, want) { | ||
t.Errorf("expected %v to be %v", got, want) | ||
} | ||
}) | ||
} | ||
} |
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