good morning!!!!

Skip to content
Snippets Groups Projects
Commit 912a42d5 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

Adding methods for getting specific YAML value types.

parent e2f8df60
No related branches found
No related tags found
No related merge requests found
......@@ -51,12 +51,37 @@ func Open(file string) *Yaml {
return yaml
}
// Returns a YAML setting (or nil if the referred name does not exists). Read nested values by using a dot (.) between labels.
// Returns the string value of the YAML path or an empty string, if the path cannot be found.
func (y *Yaml) GetString(path string) string {
return y.Get(path, "").(string)
}
// Returns the integer value of the YAML path or 0, if the path cannot be found.
func (y *Yaml) GetInt(path string) int {
return y.Get(path, 0).(int)
}
// Returns the float value of the YAML path or 0.0, if the path cannot be found.
func (y *Yaml) GetFloat(path string) float64 {
return y.Get(path, 0).(float64)
}
// Returns the boolean value of the YAML path or false, if the path cannot be found.
func (y *Yaml) GetBool(path string) bool {
return y.Get(path, false).(bool)
}
// Returns the sequenced value of the YAML path or an empty sequence, if the path cannot be found.
func (y *Yaml) GetSequence(path string) []interface{} {
return y.Get(path, nil).([]interface{})
}
// Returns a YAML setting (or defaultValue if the referred name does not exists). Read nested values by using a dot (.) between labels.
//
// Example:
//
// yaml.Get("foo.bar")
func (y *Yaml) Get(path string, def interface{}) interface{} {
// yaml.Get("foo.bar", "default")
func (y *Yaml) Get(path string, defaultValue interface{}) interface{} {
var p Tuple
path = strings.ToLower(path)
......@@ -85,17 +110,17 @@ func (y *Yaml) Get(path string, def interface{}) interface{} {
}
default:
{
return def
return defaultValue
}
}
} else {
return def
return defaultValue
}
}
}
return def
return defaultValue
}
// Sets a YAML setting, use dots (.) to nest values inside values.
......
......@@ -37,6 +37,34 @@ func TestGet(t *testing.T) {
t.Errorf("Got %t expecting %t.", val4, test4)
}
test5 := "Hello World!"
val5 := settings.GetString("test_string")
if test5 != val5 {
t.Errorf("Got %t expecting %t.", test5, val5)
}
test6 := 1234
val6 := settings.GetInt("test_int")
if test6 != val6 {
t.Errorf("Got %t expecting %t.", test6, val6)
}
test7 := 1.2
val7 := settings.GetFloat("test_float")
if test7 != val7 {
t.Errorf("Got %t expecting %t.", test7, val7)
}
test8 := true
val8 := settings.GetBool("test_bool")
if test8 != val8 {
t.Errorf("Got %t expecting %t.", test8, val8)
}
}
func TestSet(t *testing.T) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment