From 912a42d5660295cec5d72d5f2700bd3f41d59b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <xiam@menteslibres.org> Date: Thu, 9 Aug 2012 04:08:38 -0500 Subject: [PATCH] Adding methods for getting specific YAML value types. --- yaml/yaml.go | 37 +++++++++++++++++++++++++++++++------ yaml/yaml_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/yaml/yaml.go b/yaml/yaml.go index 9bf1acba..482b8bc7 100644 --- a/yaml/yaml.go +++ b/yaml/yaml.go @@ -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. diff --git a/yaml/yaml_test.go b/yaml/yaml_test.go index f9ec2b01..da62497a 100644 --- a/yaml/yaml_test.go +++ b/yaml/yaml_test.go @@ -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) { -- GitLab