forked from jinzhu/configor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
anonymous:"true"
tag to not include struct name in embedded struct
- Loading branch information
1 parent
fb7a318
commit a7da422
Showing
3 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,10 @@ import ( | |
"github.com/BurntSushi/toml" | ||
) | ||
|
||
type Anonymous struct { | ||
Description string | ||
} | ||
|
||
type Config struct { | ||
APPName string `default:"configor"` | ||
|
||
|
@@ -28,6 +32,8 @@ type Config struct { | |
Name string | ||
Email string `required:"true"` | ||
} | ||
|
||
Anonymous `anonymous:"true"` | ||
} | ||
|
||
func generateDefaultConfig() Config { | ||
|
@@ -53,6 +59,9 @@ func generateDefaultConfig() Config { | |
Email: "[email protected]", | ||
}, | ||
}, | ||
Anonymous: Anonymous{ | ||
Description: "This is an anonymous embedded struct whose environment variables should NOT include 'ANONYMOUS'", | ||
}, | ||
} | ||
return config | ||
} | ||
|
@@ -275,7 +284,7 @@ func TestReadFromEnvironmentWithSpecifiedEnvName(t *testing.T) { | |
file.Write(bytes) | ||
var result Config | ||
os.Setenv("DBPassword", "db_password") | ||
defer os.Setenv("DBPassword", "db_password") | ||
defer os.Setenv("DBPassword", "") | ||
configor.Load(&result, file.Name()) | ||
|
||
var defaultConfig = generateDefaultConfig() | ||
|
@@ -287,6 +296,28 @@ func TestReadFromEnvironmentWithSpecifiedEnvName(t *testing.T) { | |
} | ||
} | ||
|
||
func TestAnonymousStruct(t *testing.T) { | ||
config := generateDefaultConfig() | ||
|
||
if bytes, err := json.Marshal(config); err == nil { | ||
if file, err := ioutil.TempFile("/tmp", "configor"); err == nil { | ||
defer file.Close() | ||
defer os.Remove(file.Name()) | ||
file.Write(bytes) | ||
var result Config | ||
os.Setenv("CONFIGOR_DESCRIPTION", "environment description") | ||
defer os.Setenv("CONFIGOR_DESCRIPTION", "") | ||
configor.Load(&result, file.Name()) | ||
|
||
var defaultConfig = generateDefaultConfig() | ||
defaultConfig.Anonymous.Description = "environment description" | ||
if !reflect.DeepEqual(result, defaultConfig) { | ||
t.Errorf("result should equal to original configuration") | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestENV(t *testing.T) { | ||
if configor.ENV() != "test" { | ||
t.Errorf("Env should be test when running `go test`") | ||
|