From 277733731a407a7790a02494ec372fff721fc348 Mon Sep 17 00:00:00 2001 From: kballou Date: Tue, 21 Apr 2015 12:42:28 -0600 Subject: Add `GetPropertyValue` function Linearly search through properties for given key, if found, return value, else return error object. --- gohadoopxml.go | 10 ++++++++++ gohadoopxml_test.go | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/gohadoopxml.go b/gohadoopxml.go index d6d5721..336a673 100644 --- a/gohadoopxml.go +++ b/gohadoopxml.go @@ -2,6 +2,7 @@ package gohadoopxml import ( "encoding/xml" + "errors" "io/ioutil" "log" "os" @@ -36,6 +37,15 @@ func ParseXML(filename string) (Configuration, error) { return config, nil } +func GetPropertyValue(key string, config Configuration) (string, error) { + for _, p := range config.Properties { + if key == p.Name { + return p.Value, nil + } + } + return "", errors.New("Key not found") +} + func MergeConfigurations(configs ...Configuration) Configuration { var new_config Configuration for _, config := range configs { diff --git a/gohadoopxml_test.go b/gohadoopxml_test.go index aa81010..ab6c36e 100644 --- a/gohadoopxml_test.go +++ b/gohadoopxml_test.go @@ -38,3 +38,22 @@ func TestMergeConfiguration(t *testing.T) { t.Fail() } } + +func TestGetPropertyValue(t *testing.T) { + var config = Configuration{ + Properties: []Property{ + { + Name: "foo", + Value: "bar", + }, + }, + } + result, err := GetPropertyValue("foo", config) + if err != nil || result != "bar" { + t.Fail() + } + result, err = GetPropertyValue("fizz", config) + if err == nil { + t.Fail() + } +} -- cgit v1.2.1