From b210eb7dcdcb1b5c79d22ee07298b8ffb647c337 Mon Sep 17 00:00:00 2001 From: Nobi Date: Sat, 26 Mar 2022 03:20:24 +0000 Subject: [PATCH 1/2] Allow marshal to bytes buffer --- viper.go | 9 +++++++++ viper_test.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/viper.go b/viper.go index 0158a7f..571ea52 100644 --- a/viper.go +++ b/viper.go @@ -1704,6 +1704,15 @@ func (v *Viper) SafeWriteConfigAs(filename string) error { return v.writeConfig(filename, false) } +// Marshal will return a Buffer containing the content that should have been written to the file +func (v *Viper) Marshal() (*bytes.Buffer, error) { + data, err := v.encoderRegistry.Encode(v.getConfigType(), v.AllSettings()) + if err != nil { + return nil, err + } + return bytes.NewBuffer(data), nil +} + func (v *Viper) writeConfig(filename string, force bool) error { v.logger.Info("attempting to write configuration to file") diff --git a/viper_test.go b/viper_test.go index e0bfc57..ffcd5fe 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1979,6 +1979,15 @@ func TestSafeWriteConfigAsWithExistingFile(t *testing.T) { assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") } +func TestMarshal(t *testing.T) { + v := New() + v.SetConfigType("yaml") + require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample))) + c, err := v.Marshal() + require.NoError(t, err) + assert.Equal(t, c.Bytes(), yamlWriteExpected) +} + func TestWriteHiddenFile(t *testing.T) { v := New() fs := afero.NewMemMapFs() From e648540a6ac25a5a17bafba9e65c9096b6f6b34b Mon Sep 17 00:00:00 2001 From: Nobi Date: Tue, 25 Jul 2023 22:43:18 +0700 Subject: [PATCH 2/2] Add new method `Encode` to Viper that allows loading rawVal into config and merging it with the current config map --- viper.go | 10 ++++++++++ viper_test.go | 26 ++++++++++++++++++++++++++ viper_yaml_test.go | 4 ++++ 3 files changed, 40 insertions(+) diff --git a/viper.go b/viper.go index 571ea52..3e8f819 100644 --- a/viper.go +++ b/viper.go @@ -1704,6 +1704,16 @@ func (v *Viper) SafeWriteConfigAs(filename string) error { return v.writeConfig(filename, false) } +// Encode will load the rawVal into config, then you can write to file +func (v *Viper) Encode(rawVal interface{}, opts ...DecoderConfigOption) error { + var cfg map[string]interface{} + err := decode(rawVal, defaultDecoderConfig(&cfg, opts...)) + if err != nil { + return err + } + return v.MergeConfigMap(cfg) +} + // Marshal will return a Buffer containing the content that should have been written to the file func (v *Viper) Marshal() (*bytes.Buffer, error) { data, err := v.encoderRegistry.Encode(v.getConfigType(), v.AllSettings()) diff --git a/viper_test.go b/viper_test.go index ffcd5fe..d2a03b6 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1979,6 +1979,18 @@ func TestSafeWriteConfigAsWithExistingFile(t *testing.T) { assert.True(t, ok, "Expected ConfigFileAlreadyExistsError") } +func TestEncode(t *testing.T) { + type config struct { + Age int + Beard bool + } + C := &config{35, true} + v := New() + require.NoError(t, v.Encode(C)) + expected := map[string]interface{}{"age": 35, "beard": true} + assert.Equal(t, v.config, expected) +} + func TestMarshal(t *testing.T) { v := New() v.SetConfigType("yaml") @@ -1988,6 +2000,20 @@ func TestMarshal(t *testing.T) { assert.Equal(t, c.Bytes(), yamlWriteExpected) } +func TestEncodeAndMarshal(t *testing.T) { + type config struct { + Age int + Beard bool + } + C := &config{35, true} + v := New() + require.NoError(t, v.Encode(C)) + v.SetConfigType("yaml") + c, err := v.Marshal() + require.NoError(t, err) + assert.Equal(t, c.Bytes(), yamlEncodeWriteExpected) +} + func TestWriteHiddenFile(t *testing.T) { v := New() fs := afero.NewMemMapFs() diff --git a/viper_yaml_test.go b/viper_yaml_test.go index 264446b..ec1ce1c 100644 --- a/viper_yaml_test.go +++ b/viper_yaml_test.go @@ -51,3 +51,7 @@ emails: created: 01/02/03 active: true `) + +var yamlEncodeWriteExpected = []byte(`age: 35 +beard: true +`)