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