source.go 918 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package config
  2. import (
  3. "fmt"
  4. "github.com/go-kratos/kratos/v2/config"
  5. "github.com/go-kratos/kratos/v2/config/file"
  6. )
  7. type Source interface {
  8. NewSource() (config.Source, error)
  9. Validate() bool
  10. String() string
  11. }
  12. type Format string
  13. const (
  14. Yaml Format = "yaml"
  15. )
  16. var formatMap = map[Format]string{
  17. Yaml: "yaml",
  18. }
  19. func (f Format) String() string {
  20. return string(f)
  21. }
  22. func (f Format) Validate() bool {
  23. _, ok := formatMap[f]
  24. return ok
  25. }
  26. // =======================================
  27. type FileSource struct {
  28. Format Format
  29. Path string
  30. }
  31. func (s *FileSource) NewSource() (config.Source, error) {
  32. source := file.NewSource(s.Path)
  33. return source, nil
  34. }
  35. func (s *FileSource) Validate() bool {
  36. if !s.Format.Validate() {
  37. return false
  38. }
  39. if s.Path == "" {
  40. return false
  41. }
  42. return true
  43. }
  44. func (s *FileSource) String() string {
  45. return fmt.Sprintf("file source format:%v, path:%v", s.Format, s.Path)
  46. }