utils.go

 1package jsonschema
 2
 3import (
 4	"regexp"
 5	"strings"
 6
 7	orderedmap "github.com/wk8/go-ordered-map/v2"
 8)
 9
10var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
11var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
12
13// ToSnakeCase converts the provided string into snake case using dashes.
14// This is useful for Schema IDs and definitions to be coherent with
15// common JSON Schema examples.
16func ToSnakeCase(str string) string {
17	snake := matchFirstCap.ReplaceAllString(str, "${1}-${2}")
18	snake = matchAllCap.ReplaceAllString(snake, "${1}-${2}")
19	return strings.ToLower(snake)
20}
21
22// NewProperties is a helper method to instantiate a new properties ordered
23// map.
24func NewProperties() *orderedmap.OrderedMap[string, *Schema] {
25	return orderedmap.New[string, *Schema]()
26}