config.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package config
  2. import (
  3. "errors"
  4. "strings"
  5. )
  6. const (
  7. // DefaultFormat defines a default naming style
  8. DefaultFormat = "gozero"
  9. )
  10. // Config defines the file naming style
  11. type Config struct {
  12. // NamingFormat is used to define the naming format of the generated file name.
  13. // just like time formatting, you can specify the formatting style through the
  14. // two format characters go, and zero. for example: snake format you can
  15. // define as go_zero, camel case format you can it is defined as goZero,
  16. // and even split characters can be specified, such as go#zero. in theory,
  17. // any combination can be used, but the prerequisite must meet the naming conventions
  18. // of each operating system file name.
  19. // Note: NamingFormat is based on snake or camel string
  20. NamingFormat string `yaml:"namingFormat"`
  21. }
  22. // NewConfig creates an instance for Config
  23. func NewConfig(format string) (*Config, error) {
  24. if len(format) == 0 {
  25. format = DefaultFormat
  26. }
  27. cfg := &Config{NamingFormat: format}
  28. err := validate(cfg)
  29. return cfg, err
  30. }
  31. func validate(cfg *Config) error {
  32. if len(strings.TrimSpace(cfg.NamingFormat)) == 0 {
  33. return errors.New("missing namingFormat")
  34. }
  35. return nil
  36. }