add memory config source options for json/yaml

This commit is contained in:
Asim Aslam 2019-05-31 13:44:28 +01:00
parent ca769444e7
commit 082d0b9f05

View File

@ -3,11 +3,23 @@ package memory
import ( import (
"context" "context"
"github.com/micro/go-micro/config/source" "github.com/micro/go-config/source"
) )
type changeSetKey struct{} type changeSetKey struct{}
func withData(d []byte, f string) source.Option {
return func(o *source.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, changeSetKey{}, &source.ChangeSet{
Data: d,
Format: f,
})
}
}
// WithChangeSet allows a changeset to be set // WithChangeSet allows a changeset to be set
func WithChangeSet(cs *source.ChangeSet) source.Option { func WithChangeSet(cs *source.ChangeSet) source.Option {
return func(o *source.Options) { return func(o *source.Options) {
@ -18,15 +30,12 @@ func WithChangeSet(cs *source.ChangeSet) source.Option {
} }
} }
// WithData allows the source data to be set // WithJson allows the source data to be set to json
func WithData(d []byte) source.Option { func WithJson(d []byte) source.Option {
return func(o *source.Options) { return withData(d, "json")
if o.Context == nil { }
o.Context = context.Background()
} // WithYaml allows the source data to be set to yaml
o.Context = context.WithValue(o.Context, changeSetKey{}, &source.ChangeSet{ func WithYaml(d []byte) source.Option {
Data: d, return withData(d, "yaml")
Format: "json",
})
}
} }