mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-30 01:46:23 +00:00
Retain field order after running any arbitrary functions on resources (#4021)
* Reorder resource fields * Fix comment conflict * Update e2e test ordering * Suggested changes
This commit is contained in:
25
kyaml/sliceutil/slice.go
Normal file
25
kyaml/sliceutil/slice.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2021 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sliceutil
|
||||
|
||||
// Contains return true if string e is present in slice s
|
||||
func Contains(s []string, e string) bool {
|
||||
for _, a := range s {
|
||||
if a == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Remove removes the first occurrence of r in slice s
|
||||
// and returns remaining slice
|
||||
func Remove(s []string, r string) []string {
|
||||
for i, v := range s {
|
||||
if v == r {
|
||||
return append(s[:i], s[i+1:]...)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
25
kyaml/sliceutil/sliceutil_test.go
Normal file
25
kyaml/sliceutil/sliceutil_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2021 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sliceutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestContains(t *testing.T) {
|
||||
assert.True(t, Contains([]string{"foo", "bar"}, "bar"))
|
||||
assert.False(t, Contains([]string{"foo", "bar"}, "baz"))
|
||||
assert.False(t, Contains([]string{}, "bar"))
|
||||
assert.False(t, Contains([]string{}, ""))
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
assert.Equal(t, Remove([]string{"foo", "bar"}, "bar"), []string{"foo"})
|
||||
assert.Equal(t, Remove([]string{"foo", "bar", "foo"}, "foo"), []string{"bar", "foo"})
|
||||
assert.Equal(t, Remove([]string{"foo"}, "foo"), []string{})
|
||||
assert.Equal(t, Remove([]string{}, "foo"), []string{})
|
||||
assert.Equal(t, Remove([]string{"foo", "bar", "foo"}, "baz"), []string{"foo", "bar", "foo"})
|
||||
}
|
||||
Reference in New Issue
Block a user