mirror of
https://github.com/spf13/viper
synced 2025-05-07 12:47:18 +00:00
Revert "Modify to only support HCL write in Go 1.7"
This reverts commit 12b34bc4eb92cbf8ebfd56b79519f448607e3e51.
This commit is contained in:
parent
01cef45259
commit
543b9ec4c1
6 changed files with 56 additions and 156 deletions
|
@ -1,37 +0,0 @@
|
||||||
// +build go1.7
|
|
||||||
|
|
||||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
||||||
//
|
|
||||||
// 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
|
|
||||||
}
|
|
|
@ -1,62 +0,0 @@
|
||||||
// +build go1.7
|
|
||||||
|
|
||||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
||||||
//
|
|
||||||
// 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)
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
// +build !go1.7
|
|
||||||
|
|
||||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
||||||
//
|
|
||||||
// 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")}
|
|
||||||
}
|
|
|
@ -1,31 +0,0 @@
|
||||||
// +build !go1.7
|
|
||||||
|
|
||||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
|
||||||
//
|
|
||||||
// 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())
|
|
||||||
}
|
|
11
viper.go
11
viper.go
|
@ -36,6 +36,7 @@ import (
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/hashicorp/hcl"
|
"github.com/hashicorp/hcl"
|
||||||
|
"github.com/hashicorp/hcl/hcl/printer"
|
||||||
"github.com/magiconair/properties"
|
"github.com/magiconair/properties"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
toml "github.com/pelletier/go-toml"
|
toml "github.com/pelletier/go-toml"
|
||||||
|
@ -1337,7 +1338,15 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "hcl":
|
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":
|
case "prop", "props", "properties":
|
||||||
if v.properties == nil {
|
if v.properties == nil {
|
||||||
|
|
|
@ -848,6 +848,52 @@ func TestSub(t *testing.T) {
|
||||||
assert.Equal(t, (*Viper)(nil), subv)
|
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(`{
|
var jsonWriteExpected = []byte(`{
|
||||||
"batters": {
|
"batters": {
|
||||||
"batter": [
|
"batter": [
|
||||||
|
|
Loading…
Add table
Reference in a new issue