File: mapiter.go

package info (click to toggle)
golang-github-huandu-go-clone 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: makefile: 2
file content (41 lines) | stat: -rw-r--r-- 609 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2019 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.

// +build !go1.12

package clone

import (
	"reflect"
)

type iter struct {
	m    reflect.Value
	k    reflect.Value
	keys []reflect.Value
}

func mapIter(m reflect.Value) *iter {
	return &iter{
		m:    m,
		keys: m.MapKeys(),
	}
}

func (it *iter) Next() bool {
	if len(it.keys) == 0 {
		return false
	}

	it.k = it.keys[0]
	it.keys = it.keys[1:]
	return true
}

func (it *iter) Key() reflect.Value {
	return it.k
}

func (it *iter) Value() reflect.Value {
	return it.m.MapIndex(it.k)
}