naming.go 976 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package name
  2. import (
  3. "strings"
  4. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  5. )
  6. type NamingStyle = string
  7. const (
  8. NamingLower NamingStyle = "lower"
  9. NamingCamel NamingStyle = "camel"
  10. NamingSnake NamingStyle = "snake"
  11. )
  12. // IsNamingValid validates whether the namingStyle is valid or not,return
  13. // namingStyle and true if it is valid, or else return empty string
  14. // and false, and it is a valid value even namingStyle is empty string
  15. func IsNamingValid(namingStyle string) (NamingStyle, bool) {
  16. if len(namingStyle) == 0 {
  17. namingStyle = NamingLower
  18. }
  19. switch namingStyle {
  20. case NamingLower, NamingCamel, NamingSnake:
  21. return namingStyle, true
  22. default:
  23. return "", false
  24. }
  25. }
  26. func FormatFilename(filename string, style NamingStyle) string {
  27. switch style {
  28. case NamingCamel:
  29. return stringx.From(filename).ToCamel()
  30. case NamingSnake:
  31. return stringx.From(filename).ToSnake()
  32. default:
  33. return strings.ToLower(stringx.From(filename).ToCamel())
  34. }
  35. }