mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
feat(encoding): drop HCL, Java properties and INI
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
e033c8e8dc
commit
6e630e548a
2 changed files with 15 additions and 259 deletions
17
encoding.go
17
encoding.go
|
@ -6,9 +6,6 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/spf13/viper/internal/encoding/dotenv"
|
||||
"github.com/spf13/viper/internal/encoding/hcl"
|
||||
"github.com/spf13/viper/internal/encoding/ini"
|
||||
"github.com/spf13/viper/internal/encoding/javaproperties"
|
||||
"github.com/spf13/viper/internal/encoding/json"
|
||||
"github.com/spf13/viper/internal/encoding/toml"
|
||||
"github.com/spf13/viper/internal/encoding/yaml"
|
||||
|
@ -113,20 +110,6 @@ func (r codecRegistry) codec(format string) (Codec, bool) {
|
|||
case "toml":
|
||||
return toml.Codec{}, true
|
||||
|
||||
case "hcl", "tfvars":
|
||||
return hcl.Codec{}, true
|
||||
|
||||
case "ini":
|
||||
return ini.Codec{
|
||||
KeyDelimiter: r.v.keyDelim,
|
||||
LoadOptions: r.v.iniLoadOptions,
|
||||
}, true
|
||||
|
||||
case "properties", "props", "prop": // Note: This breaks writing a properties file.
|
||||
return &javaproperties.Codec{
|
||||
KeyDelimiter: v.keyDelim,
|
||||
}, true
|
||||
|
||||
case "dotenv", "env":
|
||||
return &dotenv.Codec{}, true
|
||||
}
|
||||
|
|
257
viper_test.go
257
viper_test.go
|
@ -84,58 +84,12 @@ var jsonExample = []byte(`{
|
|||
}
|
||||
}`)
|
||||
|
||||
var hclExample = []byte(`
|
||||
id = "0001"
|
||||
type = "donut"
|
||||
name = "Cake"
|
||||
ppu = 0.55
|
||||
foos {
|
||||
foo {
|
||||
key = 1
|
||||
}
|
||||
foo {
|
||||
key = 2
|
||||
}
|
||||
foo {
|
||||
key = 3
|
||||
}
|
||||
foo {
|
||||
key = 4
|
||||
}
|
||||
}`)
|
||||
|
||||
var propertiesExample = []byte(`
|
||||
p_id: 0001
|
||||
p_type: donut
|
||||
p_name: Cake
|
||||
p_ppu: 0.55
|
||||
p_batters.batter.type: Regular
|
||||
`)
|
||||
|
||||
var remoteExample = []byte(`{
|
||||
"id":"0002",
|
||||
"type":"cronut",
|
||||
"newkey":"remote"
|
||||
}`)
|
||||
|
||||
var iniExample = []byte(`; Package name
|
||||
NAME = ini
|
||||
; Package version
|
||||
VERSION = v1
|
||||
; Package import path
|
||||
IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
|
||||
|
||||
# Information about package author
|
||||
# Bio can be written in multiple lines.
|
||||
[author]
|
||||
NAME = Unknown ; Succeeding comment
|
||||
E-MAIL = fake@localhost
|
||||
GITHUB = https://github.com/%(NAME)s
|
||||
BIO = """Gopher.
|
||||
Coding addict.
|
||||
Good man.
|
||||
""" # Succeeding comment`)
|
||||
|
||||
func initConfigs(v *Viper) {
|
||||
var r io.Reader
|
||||
v.SetConfigType("yaml")
|
||||
|
@ -146,14 +100,6 @@ func initConfigs(v *Viper) {
|
|||
r = bytes.NewReader(jsonExample)
|
||||
v.unmarshalReader(r, v.config)
|
||||
|
||||
v.SetConfigType("hcl")
|
||||
r = bytes.NewReader(hclExample)
|
||||
v.unmarshalReader(r, v.config)
|
||||
|
||||
v.SetConfigType("properties")
|
||||
r = bytes.NewReader(propertiesExample)
|
||||
v.unmarshalReader(r, v.config)
|
||||
|
||||
v.SetConfigType("toml")
|
||||
r = bytes.NewReader(tomlExample)
|
||||
v.unmarshalReader(r, v.config)
|
||||
|
@ -165,10 +111,6 @@ func initConfigs(v *Viper) {
|
|||
v.SetConfigType("json")
|
||||
remote := bytes.NewReader(remoteExample)
|
||||
v.unmarshalReader(remote, v.kvstore)
|
||||
|
||||
v.SetConfigType("ini")
|
||||
r = bytes.NewReader(iniExample)
|
||||
v.unmarshalReader(r, v.config)
|
||||
}
|
||||
|
||||
func initConfig(typ, config string, v *Viper) {
|
||||
|
@ -622,22 +564,11 @@ func TestJSON(t *testing.T) {
|
|||
assert.Equal(t, "0001", v.Get("id"))
|
||||
}
|
||||
|
||||
func TestProperties(t *testing.T) {
|
||||
v := New()
|
||||
|
||||
v.SetConfigType("properties")
|
||||
|
||||
// Read the properties data into Viper configuration
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(propertiesExample)), "Error reading properties data")
|
||||
|
||||
assert.Equal(t, "0001", v.Get("p_id"))
|
||||
}
|
||||
|
||||
func TestTOML(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("toml")
|
||||
|
||||
// Read the properties data into Viper configuration
|
||||
// Read the TOML data into Viper configuration
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(tomlExample)), "Error reading toml data")
|
||||
|
||||
assert.Equal(t, "TOML Example", v.Get("title"))
|
||||
|
@ -646,42 +577,16 @@ func TestTOML(t *testing.T) {
|
|||
func TestDotEnv(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("env")
|
||||
// Read the properties data into Viper configuration
|
||||
// Read the dotenv data into Viper configuration
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(dotenvExample)), "Error reading env data")
|
||||
|
||||
assert.Equal(t, "DotEnv Example", v.Get("title_dotenv"))
|
||||
}
|
||||
|
||||
func TestHCL(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("hcl")
|
||||
// Read the properties data into Viper configuration
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(hclExample)), "Error reading hcl data")
|
||||
|
||||
// initHcl()
|
||||
assert.Equal(t, "0001", v.Get("id"))
|
||||
assert.Equal(t, 0.55, v.Get("ppu"))
|
||||
assert.Equal(t, "donut", v.Get("type"))
|
||||
assert.Equal(t, "Cake", v.Get("name"))
|
||||
v.Set("id", "0002")
|
||||
assert.Equal(t, "0002", v.Get("id"))
|
||||
assert.NotEqual(t, "cronut", v.Get("type"))
|
||||
}
|
||||
|
||||
func TestIni(t *testing.T) {
|
||||
// initIni()
|
||||
v := New()
|
||||
v.SetConfigType("ini")
|
||||
// Read the properties data into Viper configuration
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(iniExample)), "Error reading ini data")
|
||||
|
||||
assert.Equal(t, "ini", v.Get("default.name"))
|
||||
}
|
||||
|
||||
func TestRemotePrecedence(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
// Read the properties data into Viper configuration v.config
|
||||
// Read the remote data into Viper configuration v.config
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
||||
|
||||
assert.Equal(t, "0001", v.Get("id"))
|
||||
|
@ -701,7 +606,7 @@ func TestRemotePrecedence(t *testing.T) {
|
|||
func TestEnv(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
// Read the properties data into Viper configuration v.config
|
||||
// Read the JSON data into Viper configuration v.config
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
||||
|
||||
v.BindEnv("id")
|
||||
|
@ -724,7 +629,7 @@ func TestEnv(t *testing.T) {
|
|||
func TestMultipleEnv(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
// Read the properties data into Viper configuration v.config
|
||||
// Read the JSON data into Viper configuration v.config
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
||||
|
||||
v.BindEnv("f", "FOOD", "OLD_FOOD")
|
||||
|
@ -737,7 +642,7 @@ func TestMultipleEnv(t *testing.T) {
|
|||
func TestEmptyEnv(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
// Read the properties data into Viper configuration v.config
|
||||
// Read the JSON data into Viper configuration v.config
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
||||
|
||||
v.BindEnv("type") // Empty environment variable
|
||||
|
@ -752,7 +657,7 @@ func TestEmptyEnv(t *testing.T) {
|
|||
func TestEmptyEnv_Allowed(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
// Read the properties data into Viper configuration v.config
|
||||
// Read the JSON data into Viper configuration v.config
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
||||
|
||||
v.AllowEmptyEnv(true)
|
||||
|
@ -769,7 +674,7 @@ func TestEmptyEnv_Allowed(t *testing.T) {
|
|||
func TestEnvPrefix(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("json")
|
||||
// Read the properties data into Viper configuration v.config
|
||||
// Read the JSON data into Viper configuration v.config
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(jsonExample)), "Error reading json data")
|
||||
|
||||
v.SetEnvPrefix("foo") // will be uppercased automatically
|
||||
|
@ -829,7 +734,7 @@ func TestEnvKeyReplacer(t *testing.T) {
|
|||
func TestEnvSubConfig(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("yaml")
|
||||
// Read the properties data into Viper configuration v.config
|
||||
// Read the YAML data into Viper configuration v.config
|
||||
require.NoError(t, v.ReadConfig(bytes.NewBuffer(yamlExample)), "Error reading json data")
|
||||
v.AutomaticEnv()
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
|
@ -851,10 +756,6 @@ func TestAllKeys(t *testing.T) {
|
|||
|
||||
ks := []string{
|
||||
"title",
|
||||
"author.bio",
|
||||
"author.e-mail",
|
||||
"author.github",
|
||||
"author.name",
|
||||
"newkey",
|
||||
"owner.organization",
|
||||
"owner.dob",
|
||||
|
@ -866,21 +767,12 @@ func TestAllKeys(t *testing.T) {
|
|||
"hobbies",
|
||||
"clothing.jacket",
|
||||
"clothing.trousers",
|
||||
"default.import_path",
|
||||
"default.name",
|
||||
"default.version",
|
||||
"clothing.pants.size",
|
||||
"age",
|
||||
"hacker",
|
||||
"id",
|
||||
"type",
|
||||
"eyes",
|
||||
"p_id",
|
||||
"p_ppu",
|
||||
"p_batters.batter.type",
|
||||
"p_type",
|
||||
"p_name",
|
||||
"foos",
|
||||
"title_dotenv",
|
||||
"type_dotenv",
|
||||
"name_dotenv",
|
||||
|
@ -893,24 +785,13 @@ func TestAllKeys(t *testing.T) {
|
|||
"dob": dob,
|
||||
},
|
||||
"title": "TOML Example",
|
||||
"author": map[string]any{
|
||||
"e-mail": "fake@localhost",
|
||||
"github": "https://github.com/Unknown",
|
||||
"name": "Unknown",
|
||||
"bio": "Gopher.\nCoding addict.\nGood man.\n",
|
||||
},
|
||||
"ppu": 0.55,
|
||||
"eyes": "brown",
|
||||
"ppu": 0.55,
|
||||
"eyes": "brown",
|
||||
"clothing": map[string]any{
|
||||
"trousers": "denim",
|
||||
"jacket": "leather",
|
||||
"pants": map[string]any{"size": "large"},
|
||||
},
|
||||
"default": map[string]any{
|
||||
"import_path": "gopkg.in/ini.v1",
|
||||
"name": "ini",
|
||||
"version": "v1",
|
||||
},
|
||||
"id": "0001",
|
||||
"batters": map[string]any{
|
||||
"batter": []any{
|
||||
|
@ -927,27 +808,10 @@ func TestAllKeys(t *testing.T) {
|
|||
"snowboarding",
|
||||
"go",
|
||||
},
|
||||
"age": 35,
|
||||
"type": "donut",
|
||||
"newkey": "remote",
|
||||
"name": "Cake",
|
||||
"p_id": "0001",
|
||||
"p_ppu": "0.55",
|
||||
"p_name": "Cake",
|
||||
"p_batters": map[string]any{
|
||||
"batter": map[string]any{"type": "Regular"},
|
||||
},
|
||||
"p_type": "donut",
|
||||
"foos": []map[string]any{
|
||||
{
|
||||
"foo": []map[string]any{
|
||||
{"key": 1},
|
||||
{"key": 2},
|
||||
{"key": 3},
|
||||
{"key": 4},
|
||||
},
|
||||
},
|
||||
},
|
||||
"age": 35,
|
||||
"type": "donut",
|
||||
"newkey": "remote",
|
||||
"name": "Cake",
|
||||
"title_dotenv": "DotEnv Example",
|
||||
"type_dotenv": "donut",
|
||||
"name_dotenv": "Cake",
|
||||
|
@ -1611,24 +1475,6 @@ func TestFindsNestedKeys(t *testing.T) {
|
|||
"clothing.trousers": "denim",
|
||||
"owner.dob": dob,
|
||||
"beard": true,
|
||||
"foos": []map[string]any{
|
||||
{
|
||||
"foo": []map[string]any{
|
||||
{
|
||||
"key": 1,
|
||||
},
|
||||
{
|
||||
"key": 2,
|
||||
},
|
||||
{
|
||||
"key": 3,
|
||||
},
|
||||
{
|
||||
"key": 4,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for key, expectedValue := range expected {
|
||||
|
@ -1800,32 +1646,6 @@ func TestSub(t *testing.T) {
|
|||
assert.Equal(t, []string{"clothing", "pants"}, subv.parents)
|
||||
}
|
||||
|
||||
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"`)
|
||||
|
||||
var jsonWriteExpected = []byte(`{
|
||||
"batters": {
|
||||
"batter": [
|
||||
|
@ -1849,13 +1669,6 @@ var jsonWriteExpected = []byte(`{
|
|||
"type": "donut"
|
||||
}`)
|
||||
|
||||
var propertiesWriteExpected = []byte(`p_batters.batter.type = Regular
|
||||
p_id = 0001
|
||||
p_name = Cake
|
||||
p_ppu = 0.55
|
||||
p_type = donut
|
||||
`)
|
||||
|
||||
// var yamlWriteExpected = []byte(`age: 35
|
||||
// beard: true
|
||||
// clothing:
|
||||
|
@ -1882,30 +1695,6 @@ func TestWriteConfig(t *testing.T) {
|
|||
input []byte
|
||||
expectedContent []byte
|
||||
}{
|
||||
"hcl with file extension": {
|
||||
configName: "c",
|
||||
inConfigType: "hcl",
|
||||
outConfigType: "hcl",
|
||||
fileName: "c.hcl",
|
||||
input: hclExample,
|
||||
expectedContent: hclWriteExpected,
|
||||
},
|
||||
"hcl without file extension": {
|
||||
configName: "c",
|
||||
inConfigType: "hcl",
|
||||
outConfigType: "hcl",
|
||||
fileName: "c",
|
||||
input: hclExample,
|
||||
expectedContent: hclWriteExpected,
|
||||
},
|
||||
"hcl with file extension and mismatch type": {
|
||||
configName: "c",
|
||||
inConfigType: "hcl",
|
||||
outConfigType: "json",
|
||||
fileName: "c.hcl",
|
||||
input: hclExample,
|
||||
expectedContent: hclWriteExpected,
|
||||
},
|
||||
"json with file extension": {
|
||||
configName: "c",
|
||||
inConfigType: "json",
|
||||
|
@ -1930,22 +1719,6 @@ func TestWriteConfig(t *testing.T) {
|
|||
input: jsonExample,
|
||||
expectedContent: jsonWriteExpected,
|
||||
},
|
||||
"properties with file extension": {
|
||||
configName: "c",
|
||||
inConfigType: "properties",
|
||||
outConfigType: "properties",
|
||||
fileName: "c.properties",
|
||||
input: propertiesExample,
|
||||
expectedContent: propertiesWriteExpected,
|
||||
},
|
||||
"properties without file extension": {
|
||||
configName: "c",
|
||||
inConfigType: "properties",
|
||||
outConfigType: "properties",
|
||||
fileName: "c",
|
||||
input: propertiesExample,
|
||||
expectedContent: propertiesWriteExpected,
|
||||
},
|
||||
"yaml with file extension": {
|
||||
configName: "c",
|
||||
inConfigType: "yaml",
|
||||
|
|
Loading…
Reference in a new issue