diff options
author | Teddy Wing | 2017-06-04 00:05:39 +0200 |
---|---|---|
committer | Teddy Wing | 2017-06-04 00:05:39 +0200 |
commit | c113de965dea24c44023a9aabcff7f8ee1f0caa0 (patch) | |
tree | 6dca9378b3ac2d54e0ff1b965202b58320a7b303 /timetask | |
parent | aff9a869ffab0eeb8c644826b9250a500468ff1a (diff) | |
download | timetasker-c113de965dea24c44023a9aabcff7f8ee1f0caa0.tar.bz2 |
Add module.go for parsing Module XML
A new function that parses the XML returned by the `RequestModules()`
function.
It provides a `Module` type that allows us to interact with modules more
easily in code.
The `ParseXML()` function will take an XML string and return a slice of
`Module`s.
Added a test just to facilitate development. Wasn't able to find an easy
way to compare slices in Go, so just printed the values and checked the
result visually. Not a useful test for future use, but it served its
purpose. Eventually it would be nice to find a way to compare structs
and have a real pass/fail condition.
Diffstat (limited to 'timetask')
-rw-r--r-- | timetask/module.go | 24 | ||||
-rw-r--r-- | timetask/module_test.go | 50 |
2 files changed, 74 insertions, 0 deletions
diff --git a/timetask/module.go b/timetask/module.go new file mode 100644 index 0000000..adef4e5 --- /dev/null +++ b/timetask/module.go @@ -0,0 +1,24 @@ +package timetask + +import ( + "encoding/xml" +) + +type Module struct { + ID int `xml:"moduleid"` + Name string `xml:"modulename"` +} + +type moduleXML struct { + Modules []Module `xml:"response>item"` +} + +func ParseXML(xml_str string) ([]Module, error) { + modules := moduleXML{} + err := xml.Unmarshal([]byte(xml_str), &modules) + if err != nil { + return nil, err + } + + return modules.Modules, nil +} diff --git a/timetask/module_test.go b/timetask/module_test.go new file mode 100644 index 0000000..6dcfa94 --- /dev/null +++ b/timetask/module_test.go @@ -0,0 +1,50 @@ +package timetask + +import "testing" + +const modules_xml = `<?xml version="1.0" encoding="UTF-8" ?> +<ajax-response> + <response type="object" id="ModuleList"> + <item> + <moduleid><![CDATA[55555]]></moduleid> + <modulename><![CDATA[R&D]]></modulename> + </item> + <item> + <moduleid><![CDATA[77777]]></moduleid> + <modulename><![CDATA[Sprint 1]]></modulename> + </item> + <item> + <moduleid><![CDATA[222222]]></moduleid> + <modulename><![CDATA[Sprint 2]]></modulename> + </item> + </response> +</ajax-response>` + +func TestParseXML(t *testing.T) { + modules, err := ParseXML(modules_xml) + if err != nil { + t.Error(err) + } + + _ = []Module{ // wanted + Module{ + ID: 55555, + Name: "R&D", + }, + Module{ + ID: 77777, + Name: "Sprint 1", + }, + Module{ + ID: 222222, + Name: "Sprint 2", + }, + } + + // Need a way to compare slices + // if modules != wanted { + // t.Errorf("Module parsing failed. Wanted %+v got %+v", wanted, modules) + // } + + t.Logf("%+v\n", modules) +} |