From 30e02da227bac041cf4ea0c918ad81d360c05084 Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Wed, 24 Jul 2024 14:13:32 +0100 Subject: [PATCH] fix: Don't panic when trying `ToSnake` on `s` character (#1816) #### Summary Discovered in https://github.com/cloudquery/cloudquery/actions/runs/10076384689/job/27856647330#step:6:18 as a result of https://github.com/cloudquery/cloudquery-private/pull/3487 --- --- caser/caser.go | 2 +- caser/caser_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/caser/caser.go b/caser/caser.go index 25778e0658..5bd2e7d8d9 100644 --- a/caser/caser.go +++ b/caser/caser.go @@ -113,7 +113,7 @@ func (c *Caser) ToSnake(s string) string { // append the last word if s[lastPos:] != "" { // handle plurals of initialisms like CDNs, ARNs, IDs - if w := s[lastPos:]; w == "s" { + if w := s[lastPos:]; w == "s" && words != nil { words[len(words)-1] = words[len(words)-1] + w } else { words = append(words, s[lastPos:]) diff --git a/caser/caser_test.go b/caser/caser_test.go index 213e26a1f6..e33409bc2d 100644 --- a/caser/caser_test.go +++ b/caser/caser_test.go @@ -42,6 +42,8 @@ func Test_ToSnake(t *testing.T) { {Camel: " H e l l o", Snake: "h_e_l_l_o"}, {Camel: " H e l l o ", Snake: "h_e_l_l_o"}, {Camel: "Hello World", Snake: "hello_world"}, + {Camel: "s", Snake: "s"}, + {Camel: "S", Snake: "s"}, } t.Parallel() c := New() @@ -67,6 +69,7 @@ func Test_ToCamel(t *testing.T) { {Camel: "testCamelCaseLongString", Snake: "test_camel_case_long_string"}, {Camel: "testCamelCaseLongString", Snake: "test_camel_case_long_string"}, {Camel: "testIPv4", Snake: "test_ipv4"}, + {Camel: "s", Snake: "s"}, } t.Parallel() c := New() @@ -94,6 +97,7 @@ func Test_ToTitle(t *testing.T) { {Title: "Test IPv4", Snake: "test_ipv4"}, {Title: "AWS Test Table", Snake: "aws_test_table"}, {Title: "Gcp Test Table", Snake: "gcp_test_table"}, // no exception specified + {Title: "S", Snake: "s"}, } t.Parallel() c := New(WithCustomExceptions(map[string]string{ @@ -125,6 +129,7 @@ func Test_ToPascal(t *testing.T) { {Pascal: "TestIPv4", Snake: "test_ipv4"}, {Pascal: "Ec2", Snake: "ec2"}, {Pascal: "S3", Snake: "s3"}, + {Pascal: "S", Snake: "s"}, } t.Parallel() c := New()