Skip to content

Commit

Permalink
Fixed EOF error call (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koeng101 authored Jun 5, 2021
1 parent 17f27e4 commit f8cbd8f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Binary file modified parsers/uniprot/data/uniprot_sprot_mini.xml.gz
Binary file not shown.
20 changes: 10 additions & 10 deletions parsers/uniprot/uniprot.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ func ParseUniprot(r io.Reader, entries chan<- Entry, errors chan<- error) {
decoder := xml.NewDecoder(r)
for {
decoderToken, err := decoder.Token()

if err != nil {
if err.Error() == "EOF" {
break
}
errors <- err
}
if decoderToken == nil {
break
}
// type assertion
startElement, ok := decoderToken.(xml.StartElement)
if ok && startElement.Name.Local == "entry" {
var e Entry
err = decoder.DecodeElement(&e, &startElement)
if err != nil {
errors <- err
}
entries <- e
var e Entry
err = decoder.DecodeElement(&e, &startElement)
if err != nil {
errors <- err
}
entries <- e
}
}
close(entries)
Expand Down
27 changes: 19 additions & 8 deletions parsers/uniprot/uniprot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package uniprot

import (
"compress/gzip"
"os"
"fmt"
"os"
"testing"
)

Expand All @@ -20,18 +20,18 @@ func ExampleReadUniprot() {

func ExampleParseUniprot() {
xmlFile, _ := os.Open("data/uniprot_sprot_mini.xml.gz")
unzippedBytes, _ := gzip.NewReader(xmlFile)
unzippedBytes, _ := gzip.NewReader(xmlFile)

entries := make(chan Entry, 100) // if you don't have a buffered channel, nothing will be read in loops on the channel.
decoderErrors := make(chan error, 100)
decoderErrors := make(chan error, 100)
go ParseUniprot(unzippedBytes, entries, decoderErrors)

var entry Entry
for singleEntry := range entries {
entry = singleEntry
}
fmt.Println(entry.Accession[0])
// Output: O55723
for singleEntry := range entries {
entry = singleEntry
}
fmt.Println(entry.Accession[0])
// Output: O55723
}

func TestReadUniprot(t *testing.T) {
Expand All @@ -44,4 +44,15 @@ func TestReadUniprot(t *testing.T) {
if err == nil {
t.Errorf("Failed to fail on empty file")
}

_, errors, err := ReadUniprot("data/uniprot_sprot_mini.xml.gz")
if err != nil {
t.Errorf("Failed on real file with error: %v", err)
}

for err := range errors {
if err != nil {
t.Errorf("Failed during parsing with error: %v", err)
}
}
}

0 comments on commit f8cbd8f

Please sign in to comment.