Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 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 | |
| 5 | // +build !go1.12 |
| 6 | |
| 7 | package impl |
| 8 | |
| 9 | import "reflect" |
| 10 | |
| 11 | type mapIter struct { |
| 12 | v reflect.Value |
| 13 | keys []reflect.Value |
| 14 | } |
| 15 | |
| 16 | // mapRange provides a less-efficient equivalent to |
| 17 | // the Go 1.12 reflect.Value.MapRange method. |
| 18 | func mapRange(v reflect.Value) *mapIter { |
| 19 | return &mapIter{v: v} |
| 20 | } |
| 21 | |
| 22 | func (i *mapIter) Next() bool { |
| 23 | if i.keys == nil { |
| 24 | i.keys = i.v.MapKeys() |
| 25 | } else { |
| 26 | i.keys = i.keys[1:] |
| 27 | } |
| 28 | return len(i.keys) > 0 |
| 29 | } |
| 30 | |
| 31 | func (i *mapIter) Key() reflect.Value { |
| 32 | return i.keys[0] |
| 33 | } |
| 34 | |
| 35 | func (i *mapIter) Value() reflect.Value { |
| 36 | return i.v.MapIndex(i.keys[0]) |
| 37 | } |