mirror of
https://github.com/spf13/viper
synced 2025-05-06 20:27:17 +00:00
Modify to only support HCL write in Go 1.7
This commit is contained in:
parent
cf3aa482da
commit
01cef45259
6 changed files with 156 additions and 56 deletions
37
go1.7Supported.go
Normal file
37
go1.7Supported.go
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
// +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
|
||||||
|
}
|
62
go1.7Supported_test.go
Normal file
62
go1.7Supported_test.go
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
// +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)
|
||||||
|
}
|
25
go1.7Unsupported.go
Normal file
25
go1.7Unsupported.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
// +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")}
|
||||||
|
}
|
31
go1.7Unsupported_test.go
Normal file
31
go1.7Unsupported_test.go
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
// +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,7 +36,6 @@ 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"
|
||||||
|
@ -1338,15 +1337,7 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "hcl":
|
case "hcl":
|
||||||
b, err := json.Marshal(c)
|
return v.marshalWriterHCL(f)
|
||||||
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,52 +848,6 @@ 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