Do not add passed top level structure name to environment variables.

This commit is contained in:
Stanislav Nikitin 2019-09-18 00:36:30 +05:00
parent 2b9eb769d6
commit 1e54436f69
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
3 changed files with 42 additions and 41 deletions

View File

@ -15,7 +15,7 @@ func TestParseString(t *testing.T) {
StringData string
}
os.Setenv("TESTSTRUCT_STRINGDATA", "test")
os.Setenv("STRINGDATA", "test")
s := &testStruct{}
err := Parse(s, nil)
@ -51,7 +51,7 @@ func TestParseBoolean(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_BOOLDATA", testCase.TestData)
os.Setenv("BOOLDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -82,7 +82,7 @@ func TestParseBoolean(t *testing.T) {
require.Equal(t, errNotBool, err1)
}
os.Unsetenv("TESTSTRUCT_BOOLDATA")
os.Unsetenv("BOOLDATA")
}
}
@ -107,7 +107,7 @@ func TestParseInt8(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_INTDATA", testCase.TestData)
os.Setenv("INTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -148,7 +148,7 @@ func TestParseInt8(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_INTDATA")
os.Unsetenv("INTDATA")
}
}
@ -173,7 +173,7 @@ func TestParseInt16(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_INTDATA", testCase.TestData)
os.Setenv("INTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -214,7 +214,7 @@ func TestParseInt16(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_INTDATA")
os.Unsetenv("INTDATA")
}
}
@ -239,7 +239,7 @@ func TestParseInt32(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_INTDATA", testCase.TestData)
os.Setenv("INTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -280,7 +280,7 @@ func TestParseInt32(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_INTDATA")
os.Unsetenv("INTDATA")
}
}
@ -305,7 +305,7 @@ func TestParseInt64(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_INTDATA", testCase.TestData)
os.Setenv("INTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -346,7 +346,7 @@ func TestParseInt64(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_INTDATA")
os.Unsetenv("INTDATA")
}
}
@ -370,7 +370,7 @@ func TestParseUint8(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_UINTDATA", testCase.TestData)
os.Setenv("UINTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -411,7 +411,7 @@ func TestParseUint8(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_UINTDATA")
os.Unsetenv("UINTDATA")
}
}
@ -435,7 +435,7 @@ func TestParseUint16(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_UINTDATA", testCase.TestData)
os.Setenv("UINTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -476,7 +476,7 @@ func TestParseUint16(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_UINTDATA")
os.Unsetenv("UINTDATA")
}
}
@ -500,7 +500,7 @@ func TestParseUint32(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_UINTDATA", testCase.TestData)
os.Setenv("UINTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -541,7 +541,7 @@ func TestParseUint32(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_UINTDATA")
os.Unsetenv("UINTDATA")
}
}
@ -565,7 +565,7 @@ func TestParseUint64(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_UINTDATA", testCase.TestData)
os.Setenv("UINTDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -606,7 +606,7 @@ func TestParseUint64(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_UINTDATA")
os.Unsetenv("UINTDATA")
}
}
@ -629,7 +629,7 @@ func TestParseFloat32(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_FLOATDATA", testCase.TestData)
os.Setenv("FLOATDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -670,7 +670,7 @@ func TestParseFloat32(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_FLOATDATA")
os.Unsetenv("FLOATDATA")
}
}
@ -692,7 +692,7 @@ func TestParseFloat64(t *testing.T) {
for _, testCase := range testCases {
t.Logf("Testing: %+v", testCase)
os.Setenv("TESTSTRUCT_FLOATDATA", testCase.TestData)
os.Setenv("FLOATDATA", testCase.TestData)
// If ErrorsAreCritical == false, then we should check only
// equality of parsed data and valid data.
@ -733,7 +733,7 @@ func TestParseFloat64(t *testing.T) {
}
}
os.Unsetenv("TESTSTRUCT_FLOATDATA")
os.Unsetenv("FLOATDATA")
}
}

5
sec.go
View File

@ -7,7 +7,6 @@ import (
"os"
"reflect"
"strconv"
"strings"
)
var (
@ -75,7 +74,9 @@ func Parse(structure interface{}, config *Options) error {
}
// Parse structure.
composeTree(value, strings.ToUpper(value.Type().Name()))
// As this is a very first function launch we should not use any
// prefixes.
composeTree(value, "")
return parseEnv()
}

View File

@ -116,14 +116,14 @@ func unsetenv(prefix string) {
}
func TestParseValidData(t *testing.T) {
setenv("TESTSTRUCT1_")
setenv("TESTSTRUCT1_TESTNEST_")
setenv("TESTSTRUCT1_TESTNESTANONYMOUS_")
setenv("TESTSTRUCT1_TESTNESTANONYMOUSPOINTER_")
setenv("TESTSTRUCT1_TESTNESTINTERFACE_")
setenv("TESTSTRUCT1_TESTNESTINTERFACEPOINTER_")
setenv("TESTSTRUCT1_TESTNESTPOINTER_")
setenv("TESTSTRUCT1_TESTUNEXPORTEDNEST_")
setenv("")
setenv("TESTNEST_")
setenv("TESTNESTANONYMOUS_")
setenv("TESTNESTANONYMOUSPOINTER_")
setenv("TESTNESTINTERFACE_")
setenv("TESTNESTINTERFACEPOINTER_")
setenv("TESTNESTPOINTER_")
setenv("TESTUNEXPORTEDNEST_")
ts := &testStruct1{}
err := Parse(ts, nil)
@ -134,14 +134,14 @@ func TestParseValidData(t *testing.T) {
require.Nil(t, err)
require.Equal(t, testBool, ts.TestBool)
unsetenv("TESTSTRUCT1_")
unsetenv("TESTSTRUCT1_TESTNEST_")
unsetenv("TESTSTRUCT1_TESTNESTANONYMOUS_")
unsetenv("TESTSTRUCT1_TESTNESTANONYMOUSPOINTER_")
unsetenv("TESTSTRUCT1_TESTNESTINTERFACE_")
unsetenv("TESTSTRUCT1_TESTNESTINTERFACEPOINTER_")
unsetenv("TESTSTRUCT1_TESTNESTPOINTER_")
unsetenv("TESTSTRUCT1_TESTUNEXPORTEDNEST_")
unsetenv("")
unsetenv("TESTNEST_")
unsetenv("TESTNESTANONYMOUS_")
unsetenv("TESTNESTANONYMOUSPOINTER_")
unsetenv("TESTNESTINTERFACE_")
unsetenv("TESTNESTINTERFACEPOINTER_")
unsetenv("TESTNESTPOINTER_")
unsetenv("TESTUNEXPORTEDNEST_")
}
func TestParseNotPointerToStructurePassed(t *testing.T) {