Skip to content

Conversation

@tobikris
Copy link

The function was supposed to convert to kebab-case, but named ToSnakeCase. It was removed in favor of using the github.com/stoewer/go-strcase package's KebabCase. This package was already suggested in the documentation anyway.

I verified that this change has no unwanted effects with the following test - no changes in the meaningful cases.

package jsonschema

import (
	"regexp"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestSnakeCase(t *testing.T) {
        // list from https://github.com/stoewer/go-strcase/blob/master/kebab_test.go#L12
        // except test cases which were not producing useful results before
	data := map[string]string{
		"":      "",
		"F":     "f",
		"Foo":   "foo",
		"FooB":  "foo-b",
		"FooID": "foo-id",
		// " FooBar\t":      "foo-bar",
		"HTTPStatusCode": "http-status-code",
		// "ParseURL.DoParse": "parse-url.do-parse",
		// "Convert Space": "convert-space",
		"Convert-dash": "convert-dash",
		// "Skip___MultipleUnderscores": "skip-multiple-underscores",
		// "Skip   MultipleSpaces":      "skip-multiple-spaces",
		// "Skip---MultipleDashes":      "skip-multiple-dashes",
	}

	for camel, snake := range data {
		converted := ToSnakeCase(camel)
		assert.Equal(t, snake, converted)
	}
}

var (
	matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
	matchAllCap   = regexp.MustCompile("([a-z0-9])([A-Z])")
)

// ToSnakeCase converts the provided string into snake case using dashes.
// This is useful for Schema IDs and definitions to be coherent with
// common JSON Schema examples.
func ToSnakeCase(str string) string {
	snake := matchFirstCap.ReplaceAllString(str, "${1}-${2}")
	snake = matchAllCap.ReplaceAllString(snake, "${1}-${2}")
	return strings.ToLower(snake)
}

The function was supposed to convert to kebab-case, but named ToSnakeCase.
It was removed in favor of using the github.com/stoewer/go-strcase package's KebabCase.
This package was already suggested in the documentation anyway.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant