File: mapiter.go

package info (click to toggle)
golang-github-lestrrat-go-jwx 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,872 kB
  • sloc: sh: 222; makefile: 86; perl: 62
file content (36 lines) | stat: -rw-r--r-- 924 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
package iter

import (
	"context"
	"fmt"

	"github.com/lestrrat-go/iter/mapiter"
)

// MapVisitor is a specialized visitor for our purposes.
// Whereas mapiter.Visitor supports any type of key, this
// visitor assumes the key is a string
type MapVisitor interface {
	Visit(string, interface{}) error
}

type MapVisitorFunc func(string, interface{}) error

func (fn MapVisitorFunc) Visit(s string, v interface{}) error {
	return fn(s, v)
}

func WalkMap(ctx context.Context, src mapiter.Source, visitor MapVisitor) error {
	return mapiter.Walk(ctx, src, mapiter.VisitorFunc(func(k, v interface{}) error {
		//nolint:forcetypeassert
		return visitor.Visit(k.(string), v)
	}))
}

func AsMap(ctx context.Context, src mapiter.Source) (map[string]interface{}, error) {
	var m map[string]interface{}
	if err := mapiter.AsMap(ctx, src, &m); err != nil {
		return nil, fmt.Errorf(`mapiter.AsMap failed: %w`, err)
	}
	return m, nil
}