mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 21:19:19 +00:00
feed refactoring
This commit is contained in:
parent
e78c028d20
commit
7ca9415322
@ -1,6 +1,7 @@
|
|||||||
package feed
|
package feed
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -12,11 +13,10 @@ var UnknownFormat = errors.New("unknown feed format")
|
|||||||
|
|
||||||
type processor func(r io.Reader) (*Feed, error)
|
type processor func(r io.Reader) (*Feed, error)
|
||||||
|
|
||||||
func detect(lookup string) (string, processor) {
|
func sniff(lookup string) (string, processor) {
|
||||||
lookup = strings.TrimSpace(lookup)
|
lookup = strings.TrimSpace(lookup)
|
||||||
if lookup[0] == '{' {
|
switch lookup[0] {
|
||||||
return "json", ParseJSON
|
case '<':
|
||||||
}
|
|
||||||
decoder := xml.NewDecoder(strings.NewReader(lookup))
|
decoder := xml.NewDecoder(strings.NewReader(lookup))
|
||||||
for {
|
for {
|
||||||
token, _ := decoder.Token()
|
token, _ := decoder.Token()
|
||||||
@ -34,20 +34,25 @@ func detect(lookup string) (string, processor) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case '{':
|
||||||
|
return "json", ParseJSON
|
||||||
|
}
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Parse(r io.Reader) (*Feed, error) {
|
func Parse(r io.Reader) (*Feed, error) {
|
||||||
var x [1024]byte
|
chunk := make([]byte, 64)
|
||||||
numread, err := r.Read(x[:])
|
numread, err := r.Read(chunk)
|
||||||
fmt.Println(numread, err)
|
fmt.Println(numread, err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to read: %s", err)
|
return nil, fmt.Errorf("Failed to read: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, callback := detect(string(x[:]))
|
_, callback := sniff(string(chunk))
|
||||||
if callback == nil {
|
if callback == nil {
|
||||||
return nil, UnknownFormat
|
return nil, UnknownFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r = io.MultiReader(bytes.NewReader(chunk), r)
|
||||||
return callback(r)
|
return callback(r)
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package feed
|
|||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestDetect(t *testing.T) {
|
func TestSniff(t *testing.T) {
|
||||||
testcases := [][2]string{
|
testcases := [][2]string{
|
||||||
{
|
{
|
||||||
`<?xml version="1.0"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"></rdf:RDF>`,
|
`<?xml version="1.0"?><rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"></rdf:RDF>`,
|
||||||
@ -26,7 +26,7 @@ func TestDetect(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, testcase := range testcases {
|
for _, testcase := range testcases {
|
||||||
have, _ := detect(testcase[0])
|
have, _ := sniff(testcase[0])
|
||||||
want := testcase[1]
|
want := testcase[1]
|
||||||
if want != have {
|
if want != have {
|
||||||
t.Log(testcase[0])
|
t.Log(testcase[0])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user