goversion_maprange_lt_go112.go 742 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. // +build !go1.12
  4. // +build !go1.7 safe
  5. package codec
  6. import "reflect"
  7. type mapIter struct {
  8. m reflect.Value
  9. keys []reflect.Value
  10. j int
  11. values bool
  12. }
  13. func (t *mapIter) Next() (r bool) {
  14. t.j++
  15. return t.j < len(t.keys)
  16. }
  17. func (t *mapIter) Key() reflect.Value {
  18. return t.keys[t.j]
  19. }
  20. func (t *mapIter) Value() (r reflect.Value) {
  21. if t.values {
  22. return t.m.MapIndex(t.keys[t.j])
  23. }
  24. return
  25. }
  26. func (t *mapIter) Done() {}
  27. func mapRange(m, k, v reflect.Value, values bool) *mapIter {
  28. return &mapIter{
  29. m: m,
  30. keys: m.MapKeys(),
  31. values: values,
  32. j: -1,
  33. }
  34. }