github.com/lestrrat-go/jwx/v2@v2.0.21/internal/iter/mapiter.go (about)

     1  package iter
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/lestrrat-go/iter/mapiter"
     8  )
     9  
    10  // MapVisitor is a specialized visitor for our purposes.
    11  // Whereas mapiter.Visitor supports any type of key, this
    12  // visitor assumes the key is a string
    13  type MapVisitor interface {
    14  	Visit(string, interface{}) error
    15  }
    16  
    17  type MapVisitorFunc func(string, interface{}) error
    18  
    19  func (fn MapVisitorFunc) Visit(s string, v interface{}) error {
    20  	return fn(s, v)
    21  }
    22  
    23  func WalkMap(ctx context.Context, src mapiter.Source, visitor MapVisitor) error {
    24  	return mapiter.Walk(ctx, src, mapiter.VisitorFunc(func(k, v interface{}) error {
    25  		//nolint:forcetypeassert
    26  		return visitor.Visit(k.(string), v)
    27  	}))
    28  }
    29  
    30  func AsMap(ctx context.Context, src mapiter.Source) (map[string]interface{}, error) {
    31  	var m map[string]interface{}
    32  	if err := mapiter.AsMap(ctx, src, &m); err != nil {
    33  		return nil, fmt.Errorf(`mapiter.AsMap failed: %w`, err)
    34  	}
    35  	return m, nil
    36  }