From 8a2373b354fabe19495609ab2b3fff6904e38b06 Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Sun, 17 Jul 2016 13:33:16 +1000 Subject: [PATCH] Add more uppercase words to titlecase --- strmangle/strmangle.go | 34 ++++++++++++++++++++++++++-------- strmangle/strmangle_test.go | 26 +++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/strmangle/strmangle.go b/strmangle/strmangle.go index 7ab5498..165dcad 100644 --- a/strmangle/strmangle.go +++ b/strmangle/strmangle.go @@ -13,11 +13,11 @@ import ( ) var ( - idAlphabet = []byte("abcdefghijklmnopqrstuvwxyz") + idAlphabet = []byte("abcdefghijklmnopqrstuvwxyz") + uppercaseWords = []string{"id", "uid", "uuid", "guid", "ssn", "tz"} ) -// Identifier creates an identifier useful for a query -// This is essentially a base conversion from Base 10 integers to Base 26 +// Identifier is a base conversion from Base 10 integers to Base 26 // integers that are represented by an alphabet from a-z // See tests for example outputs. func Identifier(in int) string { @@ -64,8 +64,16 @@ func TitleCase(name string) string { splits := strings.Split(name, "_") for i, split := range splits { - if split == "id" { - splits[i] = "ID" + found := false + for _, uc := range uppercaseWords { + if split == uc { + splits[i] = strings.ToUpper(uc) + found = true + break + } + } + + if found { continue } @@ -83,12 +91,22 @@ func CamelCase(name string) string { splits := strings.Split(name, "_") for i, split := range splits { - if split == "id" && i > 0 { - splits[i] = "ID" + if i == 0 { continue } - if i == 0 { + found := false + if i > 0 { + for _, uc := range uppercaseWords { + if split == uc { + splits[i] = strings.ToUpper(uc) + found = true + break + } + } + } + + if found { continue } diff --git a/strmangle/strmangle_test.go b/strmangle/strmangle_test.go index 8d64d43..626979b 100644 --- a/strmangle/strmangle_test.go +++ b/strmangle/strmangle_test.go @@ -96,6 +96,15 @@ func TestTitleCase(t *testing.T) { {"hello_there", "HelloThere"}, {"", ""}, {"fun_id", "FunID"}, + {"uid", "UID"}, + {"guid", "GUID"}, + {"uid", "UID"}, + {"uuid", "UUID"}, + {"ssn", "SSN"}, + {"tz", "TZ"}, + {"thing_guid", "ThingGUID"}, + {"guid_thing", "GUIDThing"}, + {"thing_guid_thing", "ThingGUIDThing"}, } for i, test := range tests { @@ -115,6 +124,15 @@ func TestCamelCase(t *testing.T) { {"hello_there_sunny", "helloThereSunny"}, {"", ""}, {"fun_id_times", "funIDTimes"}, + {"uid", "uid"}, + {"guid", "guid"}, + {"uid", "uid"}, + {"uuid", "uuid"}, + {"ssn", "ssn"}, + {"tz", "tz"}, + {"thing_guid", "thingGUID"}, + {"guid_thing", "guidThing"}, + {"thing_guid_thing", "thingGUIDThing"}, } for i, test := range tests { @@ -140,10 +158,12 @@ func TestMakeStringMap(t *testing.T) { } r = MakeStringMap(m) - e := `"TestOne": "interval", "TestTwo": "integer"` - if r != e { - t.Errorf("Expected %s, got %s", e, r) + e1 := `"TestOne": "interval", "TestTwo": "integer"` + e2 := `"TestTwo": "integer", "TestOne": "interval"` + + if r != e1 && r != e2 { + t.Errorf("Got %s", r) } }