make environment variable interpolation preprocessor optional (#1715)

This commit is contained in:
Colin Hoglund 2020-07-06 15:13:35 -04:00 committed by GitHub
parent 97ae2979ad
commit 90dca65f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 12 deletions

View File

@ -66,7 +66,7 @@ func (j *jsonReader) Values(ch *source.ChangeSet) (reader.Values, error) {
if ch.Format != "json" { if ch.Format != "json" {
return nil, errors.New("unsupported format") return nil, errors.New("unsupported format")
} }
return newValues(ch) return newValues(ch, j.opts)
} }
func (j *jsonReader) String() string { func (j *jsonReader) String() string {

View File

@ -3,6 +3,7 @@ package json
import ( import (
"testing" "testing"
"github.com/micro/go-micro/v2/config/reader"
"github.com/micro/go-micro/v2/config/source" "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{}) c, err := r.Merge(&source.ChangeSet{Data: data}, &source.ChangeSet{})
if err != nil { if err != nil {
@ -35,9 +75,5 @@ func TestReader(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
for _, test := range testData { return values
if v := values.Get(test.path...).String(""); v != test.value {
t.Fatalf("Expected %s got %s for path %v", test.value, v, test.path)
}
}
} }

View File

@ -21,9 +21,14 @@ type jsonValue struct {
*simple.Json *simple.Json
} }
func newValues(ch *source.ChangeSet) (reader.Values, error) { func newValues(ch *source.ChangeSet, opts reader.Options) (reader.Values, error) {
sj := simple.New() 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 { if err := sj.UnmarshalJSON(data); err != nil {
sj.SetPath(nil, string(ch.Data)) sj.SetPath(nil, string(ch.Data))
} }

View File

@ -4,6 +4,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/micro/go-micro/v2/config/reader"
"github.com/micro/go-micro/v2/config/source" "github.com/micro/go-micro/v2/config/source"
) )
@ -32,7 +33,7 @@ func TestValues(t *testing.T) {
for idx, test := range testData { for idx, test := range testData {
values, err := newValues(&source.ChangeSet{ values, err := newValues(&source.ChangeSet{
Data: test.csdata, Data: test.csdata,
}) }, reader.Options{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -69,7 +70,7 @@ func TestStructArray(t *testing.T) {
for idx, test := range testData { for idx, test := range testData {
values, err := newValues(&source.ChangeSet{ values, err := newValues(&source.ChangeSet{
Data: test.csdata, Data: test.csdata,
}) }, reader.Options{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -11,6 +11,7 @@ import (
type Options struct { type Options struct {
Encoding map[string]encoder.Encoder Encoding map[string]encoder.Encoder
DisableReplaceEnvVars bool
} }
type Option func(o *Options) type Option func(o *Options)
@ -40,3 +41,10 @@ func WithEncoder(e encoder.Encoder) Option {
o.Encoding[e.String()] = e o.Encoding[e.String()] = e
} }
} }
// WithDisableReplaceEnvVars disables the environment variable interpolation preprocessor
func WithDisableReplaceEnvVars() Option {
return func(o *Options) {
o.DisableReplaceEnvVars = true
}
}