From e895d2d708d585ab12e612a1a814fdadc0d71433 Mon Sep 17 00:00:00 2001 From: Duke Date: Thu, 23 Jan 2014 14:57:26 -0200 Subject: [PATCH] changed Extensions api --- feed_test.go | 14 +++++++------- item.go | 2 +- rss.go | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/feed_test.go b/feed_test.go index dc84b76..a1345e8 100644 --- a/feed_test.go +++ b/feed_test.go @@ -76,26 +76,26 @@ func Test_RssAuthor(t *testing.T) { } } -func Test_Extensions(t *testing.T) { +func Test_ItemExtensions(t *testing.T) { content, _ := ioutil.ReadFile("testdata/extension.rss") feed := New(1, true, chanHandler, itemHandler) feed.FetchBytes("http://example.com", content, nil) - extension := feed.Channels[0].Items[0].Extensions["http://www.sec.gov/Archives/edgar"][0] + edgarExtension := feed.Channels[0].Items[0].Extensions["http://www.sec.gov/Archives/edgar"] companyExpected := "Cellular Biomedicine Group, Inc." - companyName := *extension.Childrens[0] + companyName := edgarExtension["companyName"][0] if companyName.Value != companyExpected { t.Errorf("Expected company to be %s but found %s", companyExpected, companyName.Value) } - files := *extension.Childrens[11] + files := edgarExtension["xbrlFiles"][0].Childrens["xbrlFile"] fileSizeExpected := 10 - if len(files.Childrens) != 10 { - t.Errorf("Expected files size to be %s but found %s", fileSizeExpected, len(files.Childrens)) + if len(files) != 10 { + t.Errorf("Expected files size to be %s but found %s", fileSizeExpected, len(files)) } - file := *files.Childrens[0] + file := files[0] fileExpected := "cbmg_10qa.htm" if file.Attrs["file"] != fileExpected { t.Errorf("Expected file to be %s but found %s", fileExpected, len(file.Attrs["file"])) diff --git a/item.go b/item.go index a9b274f..74045e4 100644 --- a/item.go +++ b/item.go @@ -24,7 +24,7 @@ type Item struct { Contributors []string Content *Content - Extensions map[string][]*Extension + Extensions map[string]map[string][]Extension } func (i *Item) Key() string { diff --git a/rss.go b/rss.go index 34e1eee..a4797de 100644 --- a/rss.go +++ b/rss.go @@ -10,7 +10,7 @@ type Extension struct { Name string Value string Attrs map[string]string - Childrens []*Extension + Childrens map[string][]Extension } var days = map[string]int{ @@ -182,11 +182,11 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) { } tl = item.SelectNodes("*", "*") - i.Extensions = make(map[string][]*Extension) + i.Extensions = make(map[string]map[string][]Extension) for _, lv := range tl { - e, ok := getExtension(lv) + e, ok := getExtentions(lv) if ok { - i.Extensions[lv.Name.Space] = append(i.Extensions[lv.Name.Space], e) + i.Extensions[lv.Name.Space] = e } } @@ -197,24 +197,39 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) { return } -func getExtension(node *xmlx.Node) (*Extension, bool) { +func getExtentions(node *xmlx.Node) (extentions map[string][]Extension, ok bool) { + extentions = make(map[string][]Extension, 0) + if node.Name.Space != "" { + for _, y := range node.Children { + extension, ok := getExtention(y) + if ok { + extentions[y.Name.Local] = append(extentions[y.Name.Local], extension) + } + } + ok = true + } else { + ok = false + } + return +} + +func getExtention(node *xmlx.Node) (Extension, bool) { + var extension Extension if node.Name.Space != "" { - var extension Extension extension = Extension{Name: node.Name.Local, Value: node.GetValue()} extension.Attrs = make(map[string]string) - + extension.Childrens = make(map[string][]Extension, 0) for _, x := range node.Attributes { extension.Attrs[x.Name.Local] = x.Value } - for _, y := range node.Children { - if c, ok := getExtension(y); ok { - extension.Childrens = append(extension.Childrens, c) + children, ok := getExtention(y) + if ok { + extension.Childrens[y.Name.Local] = append(extension.Childrens[y.Name.Local], children) } } - - return &extension, true + return extension, true } else { - return nil, false + return extension, false } }