codec_map_go111.go 720 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !go1.12
  5. package impl
  6. import "reflect"
  7. type mapIter struct {
  8. v reflect.Value
  9. keys []reflect.Value
  10. }
  11. // mapRange provides a less-efficient equivalent to
  12. // the Go 1.12 reflect.Value.MapRange method.
  13. func mapRange(v reflect.Value) *mapIter {
  14. return &mapIter{v: v}
  15. }
  16. func (i *mapIter) Next() bool {
  17. if i.keys == nil {
  18. i.keys = i.v.MapKeys()
  19. } else {
  20. i.keys = i.keys[1:]
  21. }
  22. return len(i.keys) > 0
  23. }
  24. func (i *mapIter) Key() reflect.Value {
  25. return i.keys[0]
  26. }
  27. func (i *mapIter) Value() reflect.Value {
  28. return i.v.MapIndex(i.keys[0])
  29. }