From 01cef45259a4f842ea9ea83ca741b222f04320bc Mon Sep 17 00:00:00 2001 From: Herkermer Sherwood Date: Thu, 15 Dec 2016 13:12:09 -0800 Subject: [PATCH] Modify to only support HCL write in Go 1.7 --- go1.7Supported.go | 37 ++++++++++++++++++++++++ go1.7Supported_test.go | 62 ++++++++++++++++++++++++++++++++++++++++ go1.7Unsupported.go | 25 ++++++++++++++++ go1.7Unsupported_test.go | 31 ++++++++++++++++++++ viper.go | 11 +------ viper_test.go | 46 ----------------------------- 6 files changed, 156 insertions(+), 56 deletions(-) create mode 100644 go1.7Supported.go create mode 100644 go1.7Supported_test.go create mode 100644 go1.7Unsupported.go create mode 100644 go1.7Unsupported_test.go diff --git a/go1.7Supported.go b/go1.7Supported.go new file mode 100644 index 0000000..680f0d2 --- /dev/null +++ b/go1.7Supported.go @@ -0,0 +1,37 @@ +// +build go1.7 + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +// This file is to house features only supported since Go 1.7. +// You can put its counterpart in go1.7Unsupported.go. + +package viper + +import ( + "encoding/json" + + "github.com/hashicorp/hcl" + "github.com/hashicorp/hcl/hcl/printer" + "github.com/spf13/afero" +) + +// Marshal a map into Writer. +func marshalWriterHCL(f afero.File) error { + return v.marshalWriterHCL(f) +} +func (v *Viper) marshalWriterHCL(f afero.File) error { + c := v.config + b, err := json.Marshal(c) + ast, err := hcl.Parse(string(b)) + if err != nil { + return ConfigMarshalError{err} + } + err = printer.Fprint(f, ast.Node) + if err != nil { + return ConfigMarshalError{err} + } + return nil +} diff --git a/go1.7Supported_test.go b/go1.7Supported_test.go new file mode 100644 index 0000000..aea2ba0 --- /dev/null +++ b/go1.7Supported_test.go @@ -0,0 +1,62 @@ +// +build go1.7 + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +package viper + +import ( + "bytes" + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" +) + +var hclWriteExpected = []byte(`"foos" = { + "foo" = { + "key" = 1 + } + + "foo" = { + "key" = 2 + } + + "foo" = { + "key" = 3 + } + + "foo" = { + "key" = 4 + } +} + +"id" = "0001" + +"name" = "Cake" + +"ppu" = 0.55 + +"type" = "donut"`) + +func TestWriteConfigHCL(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.SetConfigName("c") + v.SetConfigType("hcl") + err := v.ReadConfig(bytes.NewBuffer(hclExample)) + if err != nil { + t.Fatal(err) + } + if err := v.WriteConfigAs("c.hcl"); err != nil { + t.Fatal(err) + } + read, err := afero.ReadFile(fs, "c.hcl") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, hclWriteExpected, read) +} diff --git a/go1.7Unsupported.go b/go1.7Unsupported.go new file mode 100644 index 0000000..ce49cba --- /dev/null +++ b/go1.7Unsupported.go @@ -0,0 +1,25 @@ +// +build !go1.7 + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +// This file is to return errors when attempting to use unsupported features. +// You can put its supported counterparts into go1.7Supported.go. + +package viper + +import ( + "errors" + + "github.com/spf13/afero" +) + +// Marshal a map into Writer. +func marshalWriterHCL(f afero.File) error { + return v.marshalWriterHCL(f) +} +func (v *Viper) marshalWriterHCL(f afero.File) error { + return ConfigMarshalError{errors.New("HCL output unsupported before Go 1.7")} +} diff --git a/go1.7Unsupported_test.go b/go1.7Unsupported_test.go new file mode 100644 index 0000000..95bdaaa --- /dev/null +++ b/go1.7Unsupported_test.go @@ -0,0 +1,31 @@ +// +build !go1.7 + +// Copyright © 2014 Steve Francia . +// +// Use of this source code is governed by an MIT-style +// license that can be found in the LICENSE file. + +package viper + +import ( + "bytes" + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" +) + +func TestWriteConfigHCLError(t *testing.T) { + v := New() + fs := afero.NewMemMapFs() + v.SetFs(fs) + v.SetConfigName("c") + v.SetConfigType("hcl") + err := v.ReadConfig(bytes.NewBuffer(hclExample)) + if err != nil { + t.Fatal(err) + } + errExpected := "While marshaling config: HCL output unsupported before Go 1.7" + err = v.WriteConfigAs("c.hcl") + assert.Equal(t, errExpected, err.Error()) +} diff --git a/viper.go b/viper.go index 18606fb..615f38f 100644 --- a/viper.go +++ b/viper.go @@ -36,7 +36,6 @@ import ( "github.com/fsnotify/fsnotify" "github.com/hashicorp/hcl" - "github.com/hashicorp/hcl/hcl/printer" "github.com/magiconair/properties" "github.com/mitchellh/mapstructure" toml "github.com/pelletier/go-toml" @@ -1338,15 +1337,7 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error { } case "hcl": - b, err := json.Marshal(c) - ast, err := hcl.Parse(string(b)) - if err != nil { - return ConfigMarshalError{err} - } - err = printer.Fprint(f, ast.Node) - if err != nil { - return ConfigMarshalError{err} - } + return v.marshalWriterHCL(f) case "prop", "props", "properties": if v.properties == nil { diff --git a/viper_test.go b/viper_test.go index 1679dd5..879f953 100644 --- a/viper_test.go +++ b/viper_test.go @@ -848,52 +848,6 @@ func TestSub(t *testing.T) { assert.Equal(t, (*Viper)(nil), subv) } -var hclWriteExpected = []byte(`"foos" = { - "foo" = { - "key" = 1 - } - - "foo" = { - "key" = 2 - } - - "foo" = { - "key" = 3 - } - - "foo" = { - "key" = 4 - } -} - -"id" = "0001" - -"name" = "Cake" - -"ppu" = 0.55 - -"type" = "donut"`) - -func TestWriteConfigHCL(t *testing.T) { - v := New() - fs := afero.NewMemMapFs() - v.SetFs(fs) - v.SetConfigName("c") - v.SetConfigType("hcl") - err := v.ReadConfig(bytes.NewBuffer(hclExample)) - if err != nil { - t.Fatal(err) - } - if err := v.WriteConfigAs("c.hcl"); err != nil { - t.Fatal(err) - } - read, err := afero.ReadFile(fs, "c.hcl") - if err != nil { - t.Fatal(err) - } - assert.Equal(t, hclWriteExpected, read) -} - var jsonWriteExpected = []byte(`{ "batters": { "batter": [