options.go 900 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package memory
  2. import (
  3. "context"
  4. "gogs.baozhida.cn/zoie/OAuth-core/config/source"
  5. )
  6. type changeSetKey struct{}
  7. func withData(d []byte, f string) source.Option {
  8. return func(o *source.Options) {
  9. if o.Context == nil {
  10. o.Context = context.Background()
  11. }
  12. o.Context = context.WithValue(o.Context, changeSetKey{}, &source.ChangeSet{
  13. Data: d,
  14. Format: f,
  15. })
  16. }
  17. }
  18. // WithChangeSet allows a changeset to be set
  19. func WithChangeSet(cs *source.ChangeSet) source.Option {
  20. return func(o *source.Options) {
  21. if o.Context == nil {
  22. o.Context = context.Background()
  23. }
  24. o.Context = context.WithValue(o.Context, changeSetKey{}, cs)
  25. }
  26. }
  27. // WithJSON allows the source data to be set to json
  28. func WithJSON(d []byte) source.Option {
  29. return withData(d, "json")
  30. }
  31. // WithYAML allows the source data to be set to yaml
  32. func WithYAML(d []byte) source.Option {
  33. return withData(d, "yaml")
  34. }