config.go 1.1 KB

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