utils.go 774 B

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