summaryrefslogtreecommitdiff
path: root/gohadoopxml.go
blob: 336a673d06ac5608603a8e715bfd01ef70ed0a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package gohadoopxml

import (
	"encoding/xml"
	"errors"
	"io/ioutil"
	"log"
	"os"
)

var Version string

type Property struct {
	XMLName xml.Name `xml:"property"`
	Name    string   `xml:"name"`
	Value   string   `xml:"value"`
}

type Configuration struct {
	XMLName    xml.Name   `xml:"configuration"`
	Properties []Property `xml:"property"`
}

func ParseXML(filename string) (Configuration, error) {
	xmlFile, err := os.Open(filename)
	if err != nil {
		log.Println("Error occurred while opening xml file")
		return Configuration{}, err
	}
	defer xmlFile.Close()

	xmlData, _ := ioutil.ReadAll(xmlFile)

	var config Configuration
	xml.Unmarshal(xmlData, &config)

	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 {
		new_config.Properties = append(new_config.Properties,
			config.Properties...)
	}
	return new_config
}