From a5187e7d9856a6f15513a5a968929b0ee5472f7e Mon Sep 17 00:00:00 2001 From: Evgeniy Litvin Date: Mon, 13 Aug 2018 14:09:16 +0300 Subject: [PATCH] Convert atlas.rpc.Identifier to snake case instead on lowercase (#99) * Move CamelToSnake to util package * Convert atlas.rpc.Identifier to snake case instead on lowercase --- .../mappers/validationerrors/interceptor.go | 14 ++--- .../validationerrors/interceptor_test.go | 49 ----------------- gorm/resource/example_test.go | 5 +- gorm/resource/resource.go | 3 +- gorm/resource/resource_test.go | 4 +- util/snake.go | 16 ++++++ util/snake_test.go | 52 +++++++++++++++++++ 7 files changed, 78 insertions(+), 65 deletions(-) create mode 100644 util/snake.go create mode 100644 util/snake_test.go diff --git a/errors/mappers/validationerrors/interceptor.go b/errors/mappers/validationerrors/interceptor.go index 2192d7c8..296027c5 100644 --- a/errors/mappers/validationerrors/interceptor.go +++ b/errors/mappers/validationerrors/interceptor.go @@ -4,10 +4,11 @@ import ( "context" "fmt" "reflect" - "regexp" "strings" "google.golang.org/grpc" + + "github.com/infobloxopen/atlas-app-toolkit/util" ) type validator interface { @@ -48,7 +49,7 @@ func GetValidationError(err error) error { !valueOfCause.FieldByName("Cause").IsNil() { // Retrieve the field and reason from the error field := valueOfCause.FieldByName("Field").String() - field = CamelToSnake(field) + field = util.CamelToSnake(field) reason := valueOfCause.FieldByName("Reason").String() key := valueOfCause.FieldByName("Key").Bool() causeValue := valueOfCause.FieldByName("Cause").Interface() @@ -98,12 +99,3 @@ func (e ValidationError) Error() string { } var _ error = ValidationError{} - -// CamelToSnake takes a camelcase string and returns a snake case string. -func CamelToSnake(str string) string { - var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") - var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") - snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") - snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") - return strings.ToLower(snake) -} diff --git a/errors/mappers/validationerrors/interceptor_test.go b/errors/mappers/validationerrors/interceptor_test.go index ceb921de..eda5bd5e 100644 --- a/errors/mappers/validationerrors/interceptor_test.go +++ b/errors/mappers/validationerrors/interceptor_test.go @@ -169,55 +169,6 @@ func TestUnaryServerInterceptor_Success(t *testing.T) { } } -// TestUnaryServerInterceptor_ValidationErrors will run mock validation errors to see if it parses correctly. -func TestCamelToSnake(t *testing.T) { - tests := []struct { - name string - actual string - expected string - }{ - // Test cases - { - "Testing CamelCase", - "CamelCase", - "camel_case", - }, - { - "Testing AnotherCamel123", - "AnotherCamel123", - "another_camel123", - }, - { - "Testing testCase", - "testCase", - "test_case", - }, - { - "Testing testcase", - "testcase", - "testcase", - }, - { - "Testing JSONData", - "TestCaseUUID", - "test_case_uuid", - }, - { - "Testing JSONData", - "JSONData", - "json_data", - }, - } - for _, tt := range tests { - actual := CamelToSnake(tt.actual) - expected := tt.expected - if actual != expected { - t.Errorf("CamelToSnake failed for test %s, expected: \"%s\", actual: \"%s\"", tt.name, expected, actual) - } - - } -} - // testResponse represents a mock response. type testResponse struct{} diff --git a/gorm/resource/example_test.go b/gorm/resource/example_test.go index 8c157ee6..002659fe 100644 --- a/gorm/resource/example_test.go +++ b/gorm/resource/example_test.go @@ -4,6 +4,7 @@ import ( "fmt" resourcepb "github.com/infobloxopen/atlas-app-toolkit/rpc/resource" + "github.com/infobloxopen/atlas-app-toolkit/util" ) type ExampleGoType struct { @@ -104,7 +105,7 @@ func Example() { } fmt.Printf("application name of internal id: %s\n", pb.Id.GetApplicationName()) - fmt.Printf("resource type of internal id: %s\n", pb.Id.GetResourceType()) + fmt.Printf("resource type of internal id: %s\n", util.CamelToSnake(pb.Id.GetResourceType())) fmt.Printf("resource id of internal id: %s\n", pb.Id.GetResourceId()) fmt.Printf("application name of fqstring id: %s\n", pb.ExternalId.GetApplicationName()) fmt.Printf("resource type of fqstring id: %s\n", pb.ExternalId.GetResourceType()) @@ -114,7 +115,7 @@ func Example() { //application name of integer id: 12 //application name of fqstring id: externalapp/external_resource/id //application name of internal id: app - //resource type of internal id: exampleprotomessage + //resource type of internal id: example_proto_message //resource id of internal id: 12 //application name of fqstring id: externalapp //resource type of fqstring id: external_resource diff --git a/gorm/resource/resource.go b/gorm/resource/resource.go index 62758dd9..e2c2ed5b 100644 --- a/gorm/resource/resource.go +++ b/gorm/resource/resource.go @@ -10,6 +10,7 @@ import ( "github.com/golang/protobuf/proto" resourcepb "github.com/infobloxopen/atlas-app-toolkit/rpc/resource" + "github.com/infobloxopen/atlas-app-toolkit/util" ) const defaultResource = "" @@ -261,7 +262,7 @@ func Name(pb proto.Message) string { name := proto.MessageName(pb) v := strings.Split(name, ".") - name = strings.ToLower(v[len(v)-1]) + name = util.CamelToSnake(v[len(v)-1]) if Plural() { name += "s" } diff --git a/gorm/resource/resource_test.go b/gorm/resource/resource_test.go index 87f3d380..f835c78e 100644 --- a/gorm/resource/resource_test.go +++ b/gorm/resource/resource_test.go @@ -505,7 +505,7 @@ func TestName(t *testing.T) { }, { Message: TestProtoMessage{}, - ExpectedName: "testprotomessage", + ExpectedName: "test_proto_message", }, } for n, tc := range tcases { @@ -529,7 +529,7 @@ func TestNamePlural(t *testing.T) { }, { Message: TestProtoMessage{}, - ExpectedName: "testprotomessages", + ExpectedName: "test_proto_messages", }, } for n, tc := range tcases { diff --git a/util/snake.go b/util/snake.go new file mode 100644 index 00000000..56927b00 --- /dev/null +++ b/util/snake.go @@ -0,0 +1,16 @@ +package util + +import ( + "regexp" + "strings" +) + +var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") +var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") + +// CamelToSnake takes a camelcase string and returns a snake case string. +func CamelToSnake(str string) string { + snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") + snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") + return strings.ToLower(snake) +} diff --git a/util/snake_test.go b/util/snake_test.go new file mode 100644 index 00000000..7883105b --- /dev/null +++ b/util/snake_test.go @@ -0,0 +1,52 @@ +package util + +import "testing" + +// TestUnaryServerInterceptor_ValidationErrors will run mock validation errors to see if it parses correctly. +func TestCamelToSnake(t *testing.T) { + tests := []struct { + name string + actual string + expected string + }{ + // Test cases + { + "Testing CamelCase", + "CamelCase", + "camel_case", + }, + { + "Testing AnotherCamel123", + "AnotherCamel123", + "another_camel123", + }, + { + "Testing testCase", + "testCase", + "test_case", + }, + { + "Testing testcase", + "testcase", + "testcase", + }, + { + "Testing JSONData", + "TestCaseUUID", + "test_case_uuid", + }, + { + "Testing JSONData", + "JSONData", + "json_data", + }, + } + for _, tt := range tests { + actual := CamelToSnake(tt.actual) + expected := tt.expected + if actual != expected { + t.Errorf("CamelToSnake failed for test %s, expected: \"%s\", actual: \"%s\"", tt.name, expected, actual) + } + + } +}