make environment variable interpolation preprocessor optional (#1715)
This commit is contained in:
parent
97ae2979ad
commit
90dca65f55
@ -66,7 +66,7 @@ func (j *jsonReader) Values(ch *source.ChangeSet) (reader.Values, error) {
|
||||
if ch.Format != "json" {
|
||||
return nil, errors.New("unsupported format")
|
||||
}
|
||||
return newValues(ch)
|
||||
return newValues(ch, j.opts)
|
||||
}
|
||||
|
||||
func (j *jsonReader) String() string {
|
||||
|
@ -3,6 +3,7 @@ package json
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/v2/config/reader"
|
||||
"github.com/micro/go-micro/v2/config/source"
|
||||
)
|
||||
|
||||
@ -23,7 +24,46 @@ func TestReader(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
r := NewReader()
|
||||
values := newTestValues(t, data)
|
||||
|
||||
for _, test := range testData {
|
||||
if v := values.Get(test.path...).String(""); v != test.value {
|
||||
t.Fatalf("Expected %s got %s for path %v", test.value, v, test.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisableReplaceEnvVars(t *testing.T) {
|
||||
data := []byte(`{"foo": "bar", "baz": {"bar": "test/${test}"}}`)
|
||||
|
||||
tests := []struct {
|
||||
path []string
|
||||
value string
|
||||
opts []reader.Option
|
||||
}{
|
||||
{
|
||||
[]string{"baz", "bar"},
|
||||
"test/",
|
||||
nil,
|
||||
},
|
||||
{
|
||||
[]string{"baz", "bar"},
|
||||
"test/${test}",
|
||||
[]reader.Option{reader.WithDisableReplaceEnvVars()},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
values := newTestValues(t, data, test.opts...)
|
||||
|
||||
if v := values.Get(test.path...).String(""); v != test.value {
|
||||
t.Fatalf("Expected %s got %s for path %v", test.value, v, test.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newTestValues(t *testing.T, data []byte, opts ...reader.Option) reader.Values {
|
||||
r := NewReader(opts...)
|
||||
|
||||
c, err := r.Merge(&source.ChangeSet{Data: data}, &source.ChangeSet{})
|
||||
if err != nil {
|
||||
@ -35,9 +75,5 @@ func TestReader(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, test := range testData {
|
||||
if v := values.Get(test.path...).String(""); v != test.value {
|
||||
t.Fatalf("Expected %s got %s for path %v", test.value, v, test.path)
|
||||
}
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
@ -21,9 +21,14 @@ type jsonValue struct {
|
||||
*simple.Json
|
||||
}
|
||||
|
||||
func newValues(ch *source.ChangeSet) (reader.Values, error) {
|
||||
func newValues(ch *source.ChangeSet, opts reader.Options) (reader.Values, error) {
|
||||
sj := simple.New()
|
||||
data, _ := reader.ReplaceEnvVars(ch.Data)
|
||||
data := ch.Data
|
||||
|
||||
if !opts.DisableReplaceEnvVars {
|
||||
data, _ = reader.ReplaceEnvVars(ch.Data)
|
||||
}
|
||||
|
||||
if err := sj.UnmarshalJSON(data); err != nil {
|
||||
sj.SetPath(nil, string(ch.Data))
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/micro/go-micro/v2/config/reader"
|
||||
"github.com/micro/go-micro/v2/config/source"
|
||||
)
|
||||
|
||||
@ -32,7 +33,7 @@ func TestValues(t *testing.T) {
|
||||
for idx, test := range testData {
|
||||
values, err := newValues(&source.ChangeSet{
|
||||
Data: test.csdata,
|
||||
})
|
||||
}, reader.Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -69,7 +70,7 @@ func TestStructArray(t *testing.T) {
|
||||
for idx, test := range testData {
|
||||
values, err := newValues(&source.ChangeSet{
|
||||
Data: test.csdata,
|
||||
})
|
||||
}, reader.Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
|
||||
type Options struct {
|
||||
Encoding map[string]encoder.Encoder
|
||||
DisableReplaceEnvVars bool
|
||||
}
|
||||
|
||||
type Option func(o *Options)
|
||||
@ -40,3 +41,10 @@ func WithEncoder(e encoder.Encoder) Option {
|
||||
o.Encoding[e.String()] = e
|
||||
}
|
||||
}
|
||||
|
||||
// WithDisableReplaceEnvVars disables the environment variable interpolation preprocessor
|
||||
func WithDisableReplaceEnvVars() Option {
|
||||
return func(o *Options) {
|
||||
o.DisableReplaceEnvVars = true
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user