1
0

options.go 900 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package reader
  2. import (
  3. "gogs.baozhida.cn/zoie/OAuth-core/config/encoder"
  4. "gogs.baozhida.cn/zoie/OAuth-core/config/encoder/json"
  5. "gogs.baozhida.cn/zoie/OAuth-core/config/encoder/toml"
  6. "gogs.baozhida.cn/zoie/OAuth-core/config/encoder/xml"
  7. "gogs.baozhida.cn/zoie/OAuth-core/config/encoder/yaml"
  8. )
  9. type Options struct {
  10. Encoding map[string]encoder.Encoder
  11. }
  12. type Option func(o *Options)
  13. func NewOptions(opts ...Option) Options {
  14. options := Options{
  15. Encoding: map[string]encoder.Encoder{
  16. "json": json.NewEncoder(),
  17. "yaml": yaml.NewEncoder(),
  18. "toml": toml.NewEncoder(),
  19. "xml": xml.NewEncoder(),
  20. "yml": yaml.NewEncoder(),
  21. },
  22. }
  23. for _, o := range opts {
  24. o(&options)
  25. }
  26. return options
  27. }
  28. func WithEncoder(e encoder.Encoder) Option {
  29. return func(o *Options) {
  30. if o.Encoding == nil {
  31. o.Encoding = make(map[string]encoder.Encoder)
  32. }
  33. o.Encoding[e.String()] = e
  34. }
  35. }