Skip to content

Commit

Permalink
Convert atlas.rpc.Identifier to snake case instead on lowercase (#99)
Browse files Browse the repository at this point in the history
* Move CamelToSnake to util package

* Convert atlas.rpc.Identifier to snake case instead on lowercase
  • Loading branch information
Evgeniy-L authored Aug 13, 2018
1 parent 3237ef0 commit a5187e7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 65 deletions.
14 changes: 3 additions & 11 deletions errors/mappers/validationerrors/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"fmt"
"reflect"
"regexp"
"strings"

"google.golang.org/grpc"

"github.com/infobloxopen/atlas-app-toolkit/util"
)

type validator interface {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
49 changes: 0 additions & 49 deletions errors/mappers/validationerrors/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
5 changes: 3 additions & 2 deletions gorm/resource/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion gorm/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<default>"
Expand Down Expand Up @@ -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"
}
Expand Down
4 changes: 2 additions & 2 deletions gorm/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func TestName(t *testing.T) {
},
{
Message: TestProtoMessage{},
ExpectedName: "testprotomessage",
ExpectedName: "test_proto_message",
},
}
for n, tc := range tcases {
Expand All @@ -529,7 +529,7 @@ func TestNamePlural(t *testing.T) {
},
{
Message: TestProtoMessage{},
ExpectedName: "testprotomessages",
ExpectedName: "test_proto_messages",
},
}
for n, tc := range tcases {
Expand Down
16 changes: 16 additions & 0 deletions util/snake.go
Original file line number Diff line number Diff line change
@@ -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)
}
52 changes: 52 additions & 0 deletions util/snake_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

}
}

0 comments on commit a5187e7

Please sign in to comment.