From 90dca65f5535d39c20660cb277facb583392df1b Mon Sep 17 00:00:00 2001 From: Colin Hoglund Date: Mon, 6 Jul 2020 15:13:35 -0400 Subject: [PATCH] make environment variable interpolation preprocessor optional (#1715) --- config/reader/json/json.go | 2 +- config/reader/json/json_test.go | 48 +++++++++++++++++++++++++++---- config/reader/json/values.go | 9 ++++-- config/reader/json/values_test.go | 5 ++-- config/reader/options.go | 10 ++++++- 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/config/reader/json/json.go b/config/reader/json/json.go index 96b2c7aa..db96cb39 100644 --- a/config/reader/json/json.go +++ b/config/reader/json/json.go @@ -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 { diff --git a/config/reader/json/json_test.go b/config/reader/json/json_test.go index 2afff0fc..75d1bf7e 100644 --- a/config/reader/json/json_test.go +++ b/config/reader/json/json_test.go @@ -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 } diff --git a/config/reader/json/values.go b/config/reader/json/values.go index ce7601c7..3ff9118a 100644 --- a/config/reader/json/values.go +++ b/config/reader/json/values.go @@ -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)) } diff --git a/config/reader/json/values_test.go b/config/reader/json/values_test.go index 3ace3eb9..7d3ccaeb 100644 --- a/config/reader/json/values_test.go +++ b/config/reader/json/values_test.go @@ -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) } diff --git a/config/reader/options.go b/config/reader/options.go index 23f9c3fd..716dfcac 100644 --- a/config/reader/options.go +++ b/config/reader/options.go @@ -10,7 +10,8 @@ import ( ) type Options struct { - Encoding map[string]encoder.Encoder + 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 + } +}