Skip to content

Commit

Permalink
fixes bug in uniref parsing that had parser going for all entries at …
Browse files Browse the repository at this point in the history
…once
  • Loading branch information
Koeng101 committed Dec 14, 2024
1 parent eb32010 commit f005bc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
34 changes: 16 additions & 18 deletions lib/bio/uniref/uniref.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ func (u *UniRef) GetUniRefVersion() string {

type Parser struct {
decoder *xml.Decoder
uniref *UniRef
current int
}

func NewParser(r io.Reader) (*Parser, error) {
Expand All @@ -109,7 +107,6 @@ func NewParser(r io.Reader) (*Parser, error) {

return &Parser{
decoder: decoder,
current: -1,
}, nil
}

Expand All @@ -120,25 +117,26 @@ func (p *Parser) Header() (Header, error) {

// Next returns the next Entry from the UniRef file
func (p *Parser) Next() (Entry, error) {
// First time reading
if p.uniref == nil {
p.uniref = &UniRef{}
if err := p.decoder.Decode(p.uniref); err != nil {
for {
token, err := p.decoder.Token()
if err == io.EOF {
return Entry{}, io.EOF
}
if err != nil {
return Entry{}, err
}
p.current = 0
}

// Check if we've reached the end of entries
if p.current >= len(p.uniref.Entries) {
return Entry{}, io.EOF
// Look for start element of an entry
if startElement, ok := token.(xml.StartElement); ok {
if startElement.Name.Local == "entry" {
var entry Entry
if err := p.decoder.DecodeElement(&entry, &startElement); err != nil {
return Entry{}, err
}
return entry, nil
}
}
}

// Get current entry and increment counter
entry := p.uniref.Entries[p.current]
p.current++

return entry, nil
}

// ToXML converts an Entry back to its XML representation
Expand Down
4 changes: 0 additions & 4 deletions lib/bio/uniref/uniref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ func TestUniRefVersions(t *testing.T) {
if entry.ID != expectedID {
t.Errorf("Expected ID %s, got %s", expectedID, entry.ID)
}

if parser.uniref.GetUniRefVersion() != tt.version {
t.Errorf("Expected version %s, got %s", tt.version, parser.uniref.GetUniRefVersion())
}
})
}
}
Expand Down

0 comments on commit f005bc5

Please sign in to comment.