From 77ecbd418f5ee871e425d3a87daa1bb64ead2ec7 Mon Sep 17 00:00:00 2001 From: Dave Shanley Date: Sat, 5 Nov 2022 09:54:34 -0400 Subject: [PATCH] Updated model builder to use lowercase checks only. **breaking change** The case checking seems kinda dumb now looking back at this code. I am not sure why I felt the need to do that, however after being aware of it for some time and not being happy with the extra cycles it puts the code through. This change removes ConvertCase from the utils package, as it's no longer used or needed right now. If it needs co come back, we can re-add the code, but deleting code always makes me happy. It also removed a dependency from the project, which reduces the footprint, great! --- datamodel/low/model_builder.go | 15 ++------------- go.mod | 1 - go.sum | 2 -- utils/utils.go | 23 +---------------------- utils/utils_test.go | 18 ------------------ 5 files changed, 3 insertions(+), 56 deletions(-) diff --git a/datamodel/low/model_builder.go b/datamodel/low/model_builder.go index ac64bf9..e729f35 100644 --- a/datamodel/low/model_builder.go +++ b/datamodel/low/model_builder.go @@ -9,6 +9,7 @@ import ( "gopkg.in/yaml.v3" "reflect" "strconv" + "strings" "sync" ) @@ -39,19 +40,7 @@ func BuildModel(node *yaml.Node, model interface{}) error { continue // internal construct } - // we need to find a matching field in the YAML, the cases may be off, so take no chances. - // TODO: investigate if a straight up to_lower will speed things up here, will it decrease or increase accuracy? - cases := []utils.Case{utils.CamelCase, utils.PascalCase, utils.ScreamingSnakeCase, - utils.SnakeCase, utils.KebabCase, utils.RegularCase} - - var vn, kn *yaml.Node - for _, tryCase := range cases { - kn, vn = utils.FindKeyNodeTop(utils.ConvertCase(fName, tryCase), node.Content) - if vn != nil { - break - } - } - + kn, vn := utils.FindKeyNodeTop(strings.ToLower(fName), node.Content) if vn == nil { // no point in going on. continue diff --git a/go.mod b/go.mod index 71611ff..6addddc 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/pb33f/libopenapi go 1.18 require ( - github.com/iancoleman/strcase v0.2.0 github.com/stretchr/testify v1.8.0 github.com/vmware-labs/yaml-jsonpath v0.3.2 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 2d64ebc..cf27655 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,6 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= -github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0= -github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= diff --git a/utils/utils.go b/utils/utils.go index eb68aaa..c6fe555 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,7 +3,6 @@ package utils import ( "encoding/json" "fmt" - "github.com/iancoleman/strcase" "github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath" "gopkg.in/yaml.v3" "regexp" @@ -206,7 +205,7 @@ type KeyNodeSearch struct { func FindKeyNodeTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node) { for i, v := range nodes { - if key == v.Value { + if strings.ToLower(key) == strings.ToLower(v.Value) { return v, nodes[i+1] // next node is what we need. } } @@ -538,26 +537,6 @@ func RenderCodeSnippet(startNode *yaml.Node, specData []string, before, after in return buf.String() } -func ConvertCase(input string, convert Case) string { - if input == "" { - return "" - } - switch convert { - case PascalCase: - return strcase.ToCamel(input) - case CamelCase: - return strcase.ToLowerCamel(input) - case ScreamingKebabCase: - return strcase.ToScreamingKebab(input) - case ScreamingSnakeCase: - return strcase.ToScreamingSnake(input) - case SnakeCase: - return strcase.ToSnake(input) - default: - return input - } -} - func DetectCase(input string) Case { trim := strings.TrimSpace(input) if trim == "" { diff --git a/utils/utils_test.go b/utils/utils_test.go index c93cf25..03f869c 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -601,24 +601,6 @@ func TestDetectCase(t *testing.T) { assert.Equal(t, RegularCase, DetectCase("kebab-TimeIn_london-TOWN")) } -func TestConvertCase(t *testing.T) { - str1 := "chicken-nuggets-chicken-soup" - assert.Equal(t, "chickenNuggetsChickenSoup", ConvertCase(str1, CamelCase)) - assert.Equal(t, "ChickenNuggetsChickenSoup", ConvertCase(str1, PascalCase)) - assert.Equal(t, "chicken_nuggets_chicken_soup", ConvertCase(str1, SnakeCase)) - assert.Equal(t, str1, ConvertCase(str1, KebabCase)) - assert.Equal(t, "CHICKEN-NUGGETS-CHICKEN-SOUP", ConvertCase(str1, ScreamingKebabCase)) - assert.Equal(t, "CHICKEN_NUGGETS_CHICKEN_SOUP", ConvertCase(str1, ScreamingSnakeCase)) -} - -func TestConvertCase_NoInput(t *testing.T) { - assert.Empty(t, ConvertCase("", ScreamingKebabCase)) -} - -func TestDetectCase_NoInput(t *testing.T) { - assert.Equal(t, UnknownCase, DetectCase("")) -} - func TestIsNodeRefValue(t *testing.T) { f := &yaml.Node{