go-xmlpath / xmlpath

Strict subset of the XPath specification for the Go language.
http://gopkg.in/xmlpath.v2
Other
115 stars 37 forks source link

No Length Information Available for Iter #19

Closed rbarbey closed 8 years ago

rbarbey commented 8 years ago

I'm using xmlpath for writing tests which verify that a generated XML file contains all required information. Unfortunately, there is currently no way to get the number of matched elements contained in an Iter. I would like to be able to write a test like this (using testify in this case):

path := xmlpath.MustCompile("//path/to/array")
nodes, _ := xmlpath.Parse(bytes.NewReader(xmlAsBytes))
iter := path.Iter(nodes)

require.Equal(3, iter.Length, "Expected 3 matches but got %d", iter.Length)
niemeyer commented 8 years ago

That's an iterator over a lazily computed set of nodes. See the implementation of Iter.Next. If you want to know the full length, you'll need to iterate over it and pay the price. I wouldn't like to hide that complexity over a Length() method, and we definitely cannot have it as a field.