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!
This commit is contained in:
Dave Shanley
2022-11-05 09:54:34 -04:00
parent 9e0c5cd2e9
commit 77ecbd418f
5 changed files with 3 additions and 56 deletions

View File

@@ -9,6 +9,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"sync" "sync"
) )
@@ -39,19 +40,7 @@ func BuildModel(node *yaml.Node, model interface{}) error {
continue // internal construct continue // internal construct
} }
// we need to find a matching field in the YAML, the cases may be off, so take no chances. kn, vn := utils.FindKeyNodeTop(strings.ToLower(fName), node.Content)
// 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
}
}
if vn == nil { if vn == nil {
// no point in going on. // no point in going on.
continue continue

1
go.mod
View File

@@ -3,7 +3,6 @@ module github.com/pb33f/libopenapi
go 1.18 go 1.18
require ( require (
github.com/iancoleman/strcase v0.2.0
github.com/stretchr/testify v1.8.0 github.com/stretchr/testify v1.8.0
github.com/vmware-labs/yaml-jsonpath v0.3.2 github.com/vmware-labs/yaml-jsonpath v0.3.2
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1

2
go.sum
View File

@@ -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/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/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/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/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 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=

View File

@@ -3,7 +3,6 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/iancoleman/strcase"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath" "github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"regexp" "regexp"
@@ -206,7 +205,7 @@ type KeyNodeSearch struct {
func FindKeyNodeTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node) { func FindKeyNodeTop(key string, nodes []*yaml.Node) (keyNode *yaml.Node, valueNode *yaml.Node) {
for i, v := range nodes { 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. 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() 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 { func DetectCase(input string) Case {
trim := strings.TrimSpace(input) trim := strings.TrimSpace(input)
if trim == "" { if trim == "" {

View File

@@ -601,24 +601,6 @@ func TestDetectCase(t *testing.T) {
assert.Equal(t, RegularCase, DetectCase("kebab-TimeIn_london-TOWN")) 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) { func TestIsNodeRefValue(t *testing.T) {
f := &yaml.Node{ f := &yaml.Node{