changed Extensions api

This commit is contained in:
Duke 2014-01-23 14:57:26 -02:00
parent 7daa266b18
commit e895d2d708
3 changed files with 36 additions and 21 deletions

View File

@ -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") content, _ := ioutil.ReadFile("testdata/extension.rss")
feed := New(1, true, chanHandler, itemHandler) feed := New(1, true, chanHandler, itemHandler)
feed.FetchBytes("http://example.com", content, nil) 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." companyExpected := "Cellular Biomedicine Group, Inc."
companyName := *extension.Childrens[0] companyName := edgarExtension["companyName"][0]
if companyName.Value != companyExpected { if companyName.Value != companyExpected {
t.Errorf("Expected company to be %s but found %s", companyExpected, companyName.Value) 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 fileSizeExpected := 10
if len(files.Childrens) != 10 { if len(files) != 10 {
t.Errorf("Expected files size to be %s but found %s", fileSizeExpected, len(files.Childrens)) 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" fileExpected := "cbmg_10qa.htm"
if file.Attrs["file"] != fileExpected { if file.Attrs["file"] != fileExpected {
t.Errorf("Expected file to be %s but found %s", fileExpected, len(file.Attrs["file"])) t.Errorf("Expected file to be %s but found %s", fileExpected, len(file.Attrs["file"]))

View File

@ -24,7 +24,7 @@ type Item struct {
Contributors []string Contributors []string
Content *Content Content *Content
Extensions map[string][]*Extension Extensions map[string]map[string][]Extension
} }
func (i *Item) Key() string { func (i *Item) Key() string {

39
rss.go
View File

@ -10,7 +10,7 @@ type Extension struct {
Name string Name string
Value string Value string
Attrs map[string]string Attrs map[string]string
Childrens []*Extension Childrens map[string][]Extension
} }
var days = map[string]int{ var days = map[string]int{
@ -182,11 +182,11 @@ func (this *Feed) readRss2(doc *xmlx.Document) (err error) {
} }
tl = item.SelectNodes("*", "*") tl = item.SelectNodes("*", "*")
i.Extensions = make(map[string][]*Extension) i.Extensions = make(map[string]map[string][]Extension)
for _, lv := range tl { for _, lv := range tl {
e, ok := getExtension(lv) e, ok := getExtentions(lv)
if ok { 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 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 != "" { 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 var extension Extension
if node.Name.Space != "" {
extension = Extension{Name: node.Name.Local, Value: node.GetValue()} extension = Extension{Name: node.Name.Local, Value: node.GetValue()}
extension.Attrs = make(map[string]string) extension.Attrs = make(map[string]string)
extension.Childrens = make(map[string][]Extension, 0)
for _, x := range node.Attributes { for _, x := range node.Attributes {
extension.Attrs[x.Name.Local] = x.Value extension.Attrs[x.Name.Local] = x.Value
} }
for _, y := range node.Children { for _, y := range node.Children {
if c, ok := getExtension(y); ok { children, ok := getExtention(y)
extension.Childrens = append(extension.Childrens, c) if ok {
extension.Childrens[y.Name.Local] = append(extension.Childrens[y.Name.Local], children)
} }
} }
return extension, true
return &extension, true
} else { } else {
return nil, false return extension, false
} }
} }