From 543b9ec4c1cee00a66106bb5f10ca098a65c0043 Mon Sep 17 00:00:00 2001 From: Herkermer Sherwood Date: Thu, 15 Dec 2016 20:12:53 -0800 Subject: [PATCH] Revert "Modify to only support HCL write in Go 1.7" This reverts commit 12b34bc4eb92cbf8ebfd56b79519f448607e3e51. --- 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, 56 insertions(+), 156 deletions(-) delete mode 100644 go1.7Supported.go delete mode 100644 go1.7Supported_test.go delete mode 100644 go1.7Unsupported.go delete mode 100644 go1.7Unsupported_test.go diff --git a/go1.7Supported.go b/go1.7Supported.go deleted file mode 100644 index 680f0d2..0000000 --- a/go1.7Supported.go +++ /dev/null @@ -1,37 +0,0 @@ -// +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 deleted file mode 100644 index aea2ba0..0000000 --- a/go1.7Supported_test.go +++ /dev/null @@ -1,62 +0,0 @@ -// +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 deleted file mode 100644 index ce49cba..0000000 --- a/go1.7Unsupported.go +++ /dev/null @@ -1,25 +0,0 @@ -// +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 deleted file mode 100644 index 95bdaaa..0000000 --- a/go1.7Unsupported_test.go +++ /dev/null @@ -1,31 +0,0 @@ -// +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 615f38f..18606fb 100644 --- a/viper.go +++ b/viper.go @@ -36,6 +36,7 @@ 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" @@ -1337,7 +1338,15 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error { } case "hcl": - return v.marshalWriterHCL(f) + 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} + } case "prop", "props", "properties": if v.properties == nil { diff --git a/viper_test.go b/viper_test.go index 879f953..1679dd5 100644 --- a/viper_test.go +++ b/viper_test.go @@ -848,6 +848,52 @@ 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": [