iter.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package context
  2. import (
  3. "bytes"
  4. "github.com/syndtr/goleveldb/leveldb/iterator"
  5. )
  6. // Iter allows iteration of the named routes in the store.
  7. type Iter struct {
  8. it iterator.Iterator
  9. name string
  10. rt *Route
  11. err error
  12. }
  13. // Valid indicates whether the current values of the iterator are valid.
  14. func (i *Iter) Valid() bool {
  15. return i.it.Valid() && i.err == nil
  16. }
  17. // Next advances the iterator to the next value.
  18. func (i *Iter) Next() bool {
  19. it := i.it
  20. i.name = ""
  21. i.rt = nil
  22. if !it.Next() {
  23. return false
  24. }
  25. rt := &Route{}
  26. if err := rt.read(bytes.NewBuffer(it.Value())); err != nil {
  27. i.err = err
  28. return false
  29. }
  30. i.name = string(i.it.Key())
  31. i.rt = rt
  32. return true
  33. }
  34. // Error returns any active error that has stopped the iterator.
  35. func (i *Iter) Error() error {
  36. if err := i.it.Error(); err != nil {
  37. return err
  38. }
  39. return i.err
  40. }
  41. // Name is the name of the current route.
  42. func (i *Iter) Name() string {
  43. return i.name
  44. }
  45. // Route is the current route.
  46. func (i *Iter) Route() *Route {
  47. return i.rt
  48. }
  49. // Release disposes of the resources in the iterator.
  50. func (i *Iter) Release() {
  51. i.it.Release()
  52. }