coding-socks/ebml

ebml: Provide a way to iterate on elements of an EBML document

Closed this issue · 1 comments

In such cases when reading the whole content of an EBM document into memory there should be a way to scan through it.

Questions:

  1. How to iterate?
  2. How to skip child elements?
  3. How to seek?

In the current design DecodeBody accepts ebml.EBML (EBML Header) as it's second parameter to make sure DecodeHeader is called first. Maybe it would be better to reconsider this decision and store the header info in the decoder struct.

Option 0

Based on encoding/xml

f, err := os.Open("/foo/bar")
// handle error
d := ebml.NewDecoder(f)
var h ebml.EBML
// decode header
for {
	el := d.Element()
	// handle element
}

Question 2 and 3 applies.

Option 1

Based on text/scanner.

f, err := os.Open("/foo/bar")
// handle error
d := ebml.NewDecoder(f)
var h ebml.EBML
// decode header
s := d.Scanner(h)
// decode header
for s.Next(&h) {
	el := s.Element()
	// handle element
}

Question 2 and 3 applies.

Option 2

Based on github.com/yuin/goldmark/ast

f, err := os.Open("/foo/bar")
// handle error
s := ebml.NewDecoder(f)
var h ebml.EBML
// decode header
ast.Walk(h, func(el ebml.Element) (ebml.WalkStatus, error) {
	// handle element
	// return ebml.WalkStop, nil
	// return ebml.WalkSkipChildren, nil
	return ebml.WalkContinue, nil
})

Question 3 applies.

ebml.Reader can be used to iterate on elements.