naming.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Package name provides methods to verify naming style and format naming style
  2. // See the method IsNamingValid, FormatFilename
  3. package name
  4. import (
  5. "strings"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/util/stringx"
  7. )
  8. // NamingStyle the type of string
  9. type NamingStyle = string
  10. const (
  11. // NamingLower defines the lower spell case
  12. NamingLower NamingStyle = "lower"
  13. // NamingCamel defines the camel spell case
  14. NamingCamel NamingStyle = "camel"
  15. // NamingSnake defines the snake spell case
  16. NamingSnake NamingStyle = "snake"
  17. )
  18. // IsNamingValid validates whether the namingStyle is valid or not,return
  19. // namingStyle and true if it is valid, or else return empty string
  20. // and false, and it is a valid value even namingStyle is empty string
  21. func IsNamingValid(namingStyle string) (NamingStyle, bool) {
  22. if len(namingStyle) == 0 {
  23. namingStyle = NamingLower
  24. }
  25. switch namingStyle {
  26. case NamingLower, NamingCamel, NamingSnake:
  27. return namingStyle, true
  28. default:
  29. return "", false
  30. }
  31. }
  32. // FormatFilename converts the filename string to the target
  33. // naming style by calling method of stringx
  34. func FormatFilename(filename string, style NamingStyle) string {
  35. switch style {
  36. case NamingCamel:
  37. return stringx.From(filename).ToCamel()
  38. case NamingSnake:
  39. return stringx.From(filename).ToSnake()
  40. default:
  41. return strings.ToLower(stringx.From(filename).ToCamel())
  42. }
  43. }