From 37c0c14215c54831d78ad472c99657c0e390cf2a Mon Sep 17 00:00:00 2001 From: deads2k Date: Wed, 4 Jan 2017 11:28:47 -0500 Subject: [PATCH] snip pkg/util/strings dependency --- pkg/genericapiserver/api/BUILD | 1 - pkg/genericapiserver/api/installer.go | 33 +++++++++- pkg/genericapiserver/api/installer_test.go | 70 ++++++++++++++++++++++ pkg/util/strings/strings.go | 17 ------ pkg/util/strings/strings_test.go | 46 -------------- 5 files changed, 100 insertions(+), 67 deletions(-) diff --git a/pkg/genericapiserver/api/BUILD b/pkg/genericapiserver/api/BUILD index de414a21e7a..aef5d6c67be 100644 --- a/pkg/genericapiserver/api/BUILD +++ b/pkg/genericapiserver/api/BUILD @@ -35,7 +35,6 @@ go_library( "//pkg/runtime:go_default_library", "//pkg/runtime/schema:go_default_library", "//pkg/util/errors:go_default_library", - "//pkg/util/strings:go_default_library", "//vendor:github.com/emicklei/go-restful", ], ) diff --git a/pkg/genericapiserver/api/installer.go b/pkg/genericapiserver/api/installer.go index 5486bd7761f..63c5b466e3b 100644 --- a/pkg/genericapiserver/api/installer.go +++ b/pkg/genericapiserver/api/installer.go @@ -26,6 +26,7 @@ import ( "sort" "strings" "time" + "unicode" "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" @@ -40,7 +41,6 @@ import ( "k8s.io/kubernetes/pkg/genericapiserver/api/request" "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime/schema" - utilstrings "k8s.io/kubernetes/pkg/util/strings" "github.com/emicklei/go-restful" ) @@ -654,7 +654,7 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag handler = handlers.CreateResource(creater, reqScope, a.group.Typer, admit) } handler = metrics.InstrumentRouteFunc(action.Verb, resource, handler) - article := utilstrings.GetArticleForNoun(kind, " ") + article := getArticleForNoun(kind, " ") doc := "create" + article + kind if hasSubresource { doc = "create " + subresource + " of" + article + kind @@ -670,7 +670,7 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag addParams(route, action.Params) ws.Route(route) case "DELETE": // Delete a resource. - article := utilstrings.GetArticleForNoun(kind, " ") + article := getArticleForNoun(kind, " ") doc := "delete" + article + kind if hasSubresource { doc = "delete " + subresource + " of" + article + kind @@ -1080,3 +1080,30 @@ func splitSubresource(path string) (string, string, error) { } return resource, subresource, nil } + +// getArticleForNoun returns the article needed for the given noun. +func getArticleForNoun(noun string, padding string) string { + if noun[len(noun)-2:] != "ss" && noun[len(noun)-1:] == "s" { + // Plurals don't have an article. + // Don't catch words like class + return fmt.Sprintf("%v", padding) + } + + article := "a" + if isVowel(rune(noun[0])) { + article = "an" + } + + return fmt.Sprintf("%s%s%s", padding, article, padding) +} + +// isVowel returns true if the rune is a vowel (case insensitive). +func isVowel(c rune) bool { + vowels := []rune{'a', 'e', 'i', 'o', 'u'} + for _, value := range vowels { + if value == unicode.ToLower(c) { + return true + } + } + return false +} diff --git a/pkg/genericapiserver/api/installer_test.go b/pkg/genericapiserver/api/installer_test.go index 4e89a5c9c45..bef56e09184 100644 --- a/pkg/genericapiserver/api/installer_test.go +++ b/pkg/genericapiserver/api/installer_test.go @@ -56,3 +56,73 @@ func TestScopeNamingGenerateLink(t *testing.T) { t.Errorf("Unexpected error %v", err) } } + +func TestIsVowel(t *testing.T) { + tests := []struct { + name string + arg rune + want bool + }{ + { + name: "yes", + arg: 'E', + want: true, + }, + { + name: "no", + arg: 'n', + want: false, + }, + } + for _, tt := range tests { + if got := isVowel(tt.arg); got != tt.want { + t.Errorf("%q. IsVowel() = %v, want %v", tt.name, got, tt.want) + } + } +} + +func TestGetArticleForNoun(t *testing.T) { + type args struct { + } + tests := []struct { + noun string + padding string + want string + }{ + { + noun: "Frog", + padding: " ", + want: " a ", + }, + { + noun: "frogs", + padding: " ", + want: " ", + }, + { + noun: "apple", + padding: "", + want: "an", + }, + { + noun: "Apples", + padding: " ", + want: " ", + }, + { + noun: "Ingress", + padding: " ", + want: " an ", + }, + { + noun: "Class", + padding: " ", + want: " a ", + }, + } + for _, tt := range tests { + if got := getArticleForNoun(tt.noun, tt.padding); got != tt.want { + t.Errorf("%q. GetArticleForNoun() = %v, want %v", tt.noun, got, tt.want) + } + } +} diff --git a/pkg/util/strings/strings.go b/pkg/util/strings/strings.go index 8de8619b343..1015671aa91 100644 --- a/pkg/util/strings/strings.go +++ b/pkg/util/strings/strings.go @@ -17,7 +17,6 @@ limitations under the License. package strings import ( - "fmt" "path" "strings" "unicode" @@ -48,22 +47,6 @@ func ShortenString(str string, n int) string { } } -// GetArticleForNoun returns the article needed for the given noun. -func GetArticleForNoun(noun string, padding string) string { - if noun[len(noun)-2:] != "ss" && noun[len(noun)-1:] == "s" { - // Plurals don't have an article. - // Don't catch words like class - return fmt.Sprintf("%v", padding) - } - - article := "a" - if isVowel(rune(noun[0])) { - article = "an" - } - - return fmt.Sprintf("%s%s%s", padding, article, padding) -} - // isVowel returns true if the rune is a vowel (case insensitive). func isVowel(c rune) bool { vowels := []rune{'a', 'e', 'i', 'o', 'u'} diff --git a/pkg/util/strings/strings_test.go b/pkg/util/strings/strings_test.go index c31ea5f5219..c8d856a1ece 100644 --- a/pkg/util/strings/strings_test.go +++ b/pkg/util/strings/strings_test.go @@ -95,49 +95,3 @@ func TestIsVowel(t *testing.T) { } } } - -func TestGetArticleForNoun(t *testing.T) { - type args struct { - } - tests := []struct { - noun string - padding string - want string - }{ - { - noun: "Frog", - padding: " ", - want: " a ", - }, - { - noun: "frogs", - padding: " ", - want: " ", - }, - { - noun: "apple", - padding: "", - want: "an", - }, - { - noun: "Apples", - padding: " ", - want: " ", - }, - { - noun: "Ingress", - padding: " ", - want: " an ", - }, - { - noun: "Class", - padding: " ", - want: " a ", - }, - } - for _, tt := range tests { - if got := GetArticleForNoun(tt.noun, tt.padding); got != tt.want { - t.Errorf("%q. GetArticleForNoun() = %v, want %v", tt.noun, got, tt.want) - } - } -}