Skip to content

Commit

Permalink
Merge 469f2a4 into e5d20d5
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatali committed Sep 18, 2019
2 parents e5d20d5 + 469f2a4 commit 7592af1
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 33 deletions.
58 changes: 53 additions & 5 deletions codegen/casing.go
Expand Up @@ -25,6 +25,7 @@ import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)

// CommonInitialisms is taken from https://github.com/golang/lint/blob/206c0f020eba0f7fbcfbc467a5eb808037df2ed6/lint.go#L731
Expand Down Expand Up @@ -133,12 +134,59 @@ func ensureGolangAncronymCasing(segment []byte) []byte {

// PascalCase converts the given string to pascal case
func PascalCase(src string) string {
byteSrc := []byte(src)
chunks := camelingRegex.FindAll(byteSrc, -1)
for idx, val := range chunks {
chunks[idx] = ensureGolangAncronymCasing(bytes.Title(val))
// borrow pascal casing logic from thriftrw-go since the two implementations
// must match otherwise the thriftrw-go generated field name does not match
// the RequestType/ResponseType we use in the endpoint/client templates.
// https://github.com/thriftrw/thriftrw-go/blob/1c52f516bdc5ca90dc090ba2a8ee0bd11bf04f96/gen/string.go#L48
words := strings.Split(src, "_")
return pascalCase(len(words) == 1 /* all caps */, words...)
}

// pascalCase combines the given words using PascalCase.
//
// If allowAllCaps is true, when an all-caps word that is not a known
// abbreviation is encountered, it is left unchanged. Otherwise, it is
// Titlecased.
func pascalCase(allowAllCaps bool, words ...string) string {
for i, chunk := range words {
if len(chunk) == 0 {
// foo__bar
continue
}

// known initalism
init := strings.ToUpper(chunk)
if _, ok := CommonInitialisms[init]; ok {
words[i] = init
continue
}

// Was SCREAMING_SNAKE_CASE and not a known initialism so Titlecase it.
if isAllCaps(chunk) && !allowAllCaps {
// A single ALLCAPS word does not count as SCREAMING_SNAKE_CASE.
// There must be at least one underscore.
words[i] = strings.Title(strings.ToLower(chunk))
continue
}

// Just another word, but could already be camelCased somehow, so just
// change the first letter.
head, headIndex := utf8.DecodeRuneInString(chunk)
words[i] = string(unicode.ToUpper(head)) + string(chunk[headIndex:])
}
return string(bytes.Join(chunks, nil))

return strings.Join(words, "")
}

// isAllCaps checks if a string contains all capital letters only. Non-letters
// are not considered.
func isAllCaps(s string) bool {
for _, r := range s {
if unicode.IsLetter(r) && !unicode.IsUpper(r) {
return false
}
}
return true
}

// CamelToSnake converts a given string to snake case, based on
Expand Down
24 changes: 12 additions & 12 deletions codegen/type_converter_test.go
Expand Up @@ -1012,14 +1012,14 @@ func TestConvertMapStringToStruct(t *testing.T) {

assert.NoError(t, err)
assertPrettyEqual(t, trim(`
out.UUIDMap = make(map[string]*structs.MapValue, len(in.UUIDMap))
for key1, value2 := range in.UUIDMap {
out.UuidMap = make(map[string]*structs.MapValue, len(in.UuidMap))
for key1, value2 := range in.UuidMap {
if value2 != nil {
out.UUIDMap[key1] = &structs.MapValue{}
out.UUIDMap[key1].One = string(in.UUIDMap[key1].One)
out.UUIDMap[key1].Two = (*string)(in.UUIDMap[key1].Two)
out.UuidMap[key1] = &structs.MapValue{}
out.UuidMap[key1].One = string(in.UuidMap[key1].One)
out.UuidMap[key1].Two = (*string)(in.UuidMap[key1].Two)
} else {
out.UUIDMap[key1] = nil
out.UuidMap[key1] = nil
}
}
`), lines)
Expand Down Expand Up @@ -1051,14 +1051,14 @@ func TestConvertMapTypeDefToStruct(t *testing.T) {

assert.NoError(t, err)
assertPrettyEqual(t, trim(`
out.UUIDMap = make(map[structs.UUID]*structs.MapValue, len(in.UUIDMap))
for key1, value2 := range in.UUIDMap {
out.UuidMap = make(map[structs.UUID]*structs.MapValue, len(in.UuidMap))
for key1, value2 := range in.UuidMap {
if value2 != nil {
out.UUIDMap[key1] = &structs.MapValue{}
out.UUIDMap[key1].One = string(in.UUIDMap[key1].One)
out.UUIDMap[key1].Two = (*string)(in.UUIDMap[key1].Two)
out.UuidMap[key1] = &structs.MapValue{}
out.UuidMap[key1].One = string(in.UuidMap[key1].One)
out.UuidMap[key1].Two = (*string)(in.UuidMap[key1].Two)
} else {
out.UUIDMap[key1] = nil
out.UuidMap[key1] = nil
}
}
`), lines)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions runtime/tchannel_client_test.go
Expand Up @@ -50,14 +50,26 @@ func TestNilCallReferenceForLogger(t *testing.T) {
// one field for each of the:
// timestamp-started, timestamp-finished, remoteAddr, requestHeader, responseHeader
assert.Len(t, fields, 6)
assert.Equal(t, fields[0].Key, "remoteAddr")
// nil call should cause remoteAddr to be set to unknown
assert.Equal(t, fields[0].String, "unknown")

assert.Equal(t, fields[3].Key, "Client-Req-Header-header-key")
assert.Equal(t, fields[3].String, "header-value")
assert.Equal(t, fields[4].Key, "Client-Res-Header-header-key")
assert.Equal(t, fields[4].String, "header-value")
assert.Equal(t, fields[5].Key, "foo")
assert.Equal(t, fields[5].String, "bar")
var addr, reqKey, resKey, foo bool
for _, f := range fields {
switch f.Key {
case "remoteAddr":
assert.Equal(t, f.String, "unknown")
addr = true
case "Client-Req-Header-header-key":
assert.Equal(t, f.String, "header-value")
reqKey = true
case "Client-Res-Header-header-key":
assert.Equal(t, f.String, "header-value")
resKey = true
case "foo":
assert.Equal(t, f.String, "bar")
foo = true
}
}
assert.True(t, addr, "remoteAddr key not present")
assert.True(t, reqKey, "Client-Req-Header-header-key key not present")
assert.True(t, resKey, "Client-Res-Header-header-key key not present")
assert.True(t, foo, "foo key not present")
}
2 changes: 1 addition & 1 deletion test/endpoints/bar/bar_arg_with_query_params_test.go
Expand Up @@ -730,7 +730,7 @@ func TestBarWithManyQueryParamsRequiredCall(t *testing.T) {
logs := gateway.AllLogs()

assert.Equal(t, 1, len(logs["Finished an incoming server HTTP request"]))
assert.Equal(t, 1, len(logs["Started ExampleGateway"]))
assert.Equal(t, 1, len(logs["Started Example-gateway"]))
assert.Equal(t, 1, len(logs["Got request with missing query string value"]))

assert.Equal(t,
Expand Down
2 changes: 1 addition & 1 deletion test/endpoints/baz/baz_simpleservice_method_ping_test.go
Expand Up @@ -202,7 +202,7 @@ func TestPingWithInvalidResponse(t *testing.T) {

assert.Equal(t, `{"error":"Unexpected server error"}`, string(bytes))

assert.Len(t, gateway.Logs("info", "Started ExampleGateway"), 1)
assert.Len(t, gateway.Logs("info", "Started Example-gateway"), 1)
assert.Len(t, gateway.Logs("info", "Created new active connection."), 1)
assert.Len(t, gateway.Logs("info", "Failed after non-retriable error."), 1)
assert.Len(t, gateway.Logs("warn", "Client failure: TChannel client call returned error"), 1)
Expand Down
Expand Up @@ -105,7 +105,7 @@ func TestCallTChannelSuccessfulRequestOKResponse(t *testing.T) {
assert.True(t, success)

allLogs := gateway.AllLogs()
assert.Equal(t, 1, len(allLogs["Started ExampleGateway"]))
assert.Equal(t, 1, len(allLogs["Started Example-gateway"]))
assert.Equal(t, 2, len(allLogs["Created new active connection."]))
assert.Equal(t, 1, len(allLogs["Finished an outgoing client TChannel request"]))
assert.Equal(t, 1, len(allLogs["Finished an incoming server TChannel request"]))
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestCallTChannelTimeout(t *testing.T) {
assert.Nil(t, resHeaders)
assert.False(t, success)

assert.Len(t, gateway.Logs("info", "Started ExampleGateway"), 1)
assert.Len(t, gateway.Logs("info", "Started Example-gateway"), 1)
assert.Len(t, gateway.Logs("info", "Created new active connection."), 2)

// logged from tchannel client runtime
Expand Down

0 comments on commit 7592af1

Please sign in to comment.