This repository has been archived by the owner on Jan 10, 2021. It is now read-only.
-
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.
feat(defaults): added workarround when fields are structs with pointers
Workarround for jinzhu/configor#57 jinzhu/configor#57
- Loading branch information
Showing
4 changed files
with
96 additions
and
40 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 |
---|---|---|
|
@@ -16,49 +16,40 @@ type Anonymous struct { | |
Description string | ||
} | ||
|
||
type testConfig struct { | ||
APPName string `default:"configor" json:",omitempty"` | ||
Hosts []string | ||
|
||
DB struct { | ||
Name string | ||
User string `default:"root"` | ||
Password string `required:"true" env:"DBPassword"` | ||
Port uint `default:"3306" json:",omitempty"` | ||
SSL bool `default:"true" json:",omitempty"` | ||
} | ||
type Database struct { | ||
Name string | ||
User string `yaml:",omitempty" default:"root"` | ||
Password string `required:"true" env:"DBPassword"` | ||
Port uint `default:"3306" yaml:",omitempty" json:",omitempty"` | ||
SSL bool `default:"true" yaml:",omitempty" json:",omitempty"` | ||
} | ||
|
||
Contacts []struct { | ||
Name string | ||
Email string `required:"true"` | ||
} | ||
type Contact struct { | ||
Name string | ||
Email string `required:"true"` | ||
} | ||
|
||
type testConfig struct { | ||
APPName string `default:"configor" yaml:",omitempty" json:",omitempty"` | ||
Hosts []string | ||
DB *Database | ||
Contacts []Contact | ||
Anonymous `anonymous:"true"` | ||
|
||
private string | ||
private string | ||
} | ||
|
||
func generateDefaultConfig() testConfig { | ||
return testConfig{ | ||
APPName: "configor", | ||
Hosts: []string{"http://example.org", "http://jinzhu.me"}, | ||
DB: struct { | ||
Name string | ||
User string `default:"root"` | ||
Password string `required:"true" env:"DBPassword"` | ||
Port uint `default:"3306" json:",omitempty"` | ||
SSL bool `default:"true" json:",omitempty"` | ||
}{ | ||
DB: &Database{ | ||
Name: "configor", | ||
User: "configor", | ||
Password: "configor", | ||
Port: 3306, | ||
SSL: true, | ||
}, | ||
Contacts: []struct { | ||
Name string | ||
Email string `required:"true"` | ||
}{ | ||
Contacts: []Contact{ | ||
{ | ||
Name: "Jinzhu", | ||
Email: "[email protected]", | ||
|
@@ -203,6 +194,30 @@ func TestUnmatchedKeyInYamltestConfigFile(t *testing.T) { | |
} | ||
} | ||
|
||
func TestYamlDefaultValue(t *testing.T) { | ||
config := generateDefaultConfig() | ||
config.APPName = "" | ||
config.DB.Port = 0 | ||
config.DB.SSL = false | ||
|
||
if bytes, err := yaml.Marshal(config); err == nil { | ||
if file, err := ioutil.TempFile("/tmp", "configor.*.yaml"); err == nil { | ||
defer file.Close() | ||
defer os.Remove(file.Name()) | ||
file.Write(bytes) | ||
|
||
var result testConfig | ||
Load(&result, file.Name()) | ||
|
||
if !reflect.DeepEqual(result, generateDefaultConfig()) { | ||
t.Errorf("result should be set default value correctly") | ||
} | ||
} | ||
} else { | ||
t.Errorf("failed to marshal config") | ||
} | ||
} | ||
|
||
func TestLoadtestConfigurationByEnvironment(t *testing.T) { | ||
config := generateDefaultConfig() | ||
config2 := struct { | ||
|
@@ -546,7 +561,7 @@ func TestValidation(t *testing.T) { | |
Slient bool | ||
} | ||
|
||
cfg := &config{Email: "[email protected]", Email2: " ", AuthorIP: "1.1"} | ||
cfg := &config{Email: "[email protected]", Email2: "", AuthorIP: "1.1"} | ||
err := Load(cfg) | ||
fmt.Printf("%+v\n", cfg) | ||
if err != nil { | ||
|
@@ -569,12 +584,12 @@ func TestUsePkger(t *testing.T) { | |
file.Write(bytes) | ||
|
||
var result testConfig | ||
New(&Config{UsePkger: true}).Load(&result, "/" + file.Name()) | ||
New(&Config{UsePkger: true}).Load(&result, "/"+file.Name()) | ||
if !reflect.DeepEqual(result, config) { | ||
t.Errorf("result should equal to original configuration") | ||
} | ||
} | ||
} else { | ||
t.Errorf("failed to marshal config") | ||
} | ||
} | ||
} |
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