visitor

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2025 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package visitor offers generic visitors for common container types. It provides reflection-backed iteration over structs, maps, and slices, with simple callback-based traversal.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyMapVisitor

type AnyMapVisitor[K comparable, E any] struct {
	// contains filtered or unexported fields
}

AnyMapVisitor defins any map visitor

func (*AnyMapVisitor[K, E]) Visit

func (v *AnyMapVisitor[K, E]) Visit(f func(key K, element E) (bool, error)) error

Visit iterates over the map via reflection and calls f for each entry.

type AnySliceVisitor

type AnySliceVisitor[E any] struct {
	// contains filtered or unexported fields
}

AnySliceVisitor implements Visitor[int, interface{}] for slices of any type.

func (*AnySliceVisitor[E]) Visit

func (v *AnySliceVisitor[E]) Visit(f func(key int, element E) (bool, error)) error

Visit iterates over any slice type via reflection.

type MapVisitor

type MapVisitor[K comparable, E any] struct {
	// contains filtered or unexported fields
}

MapVisitor holds a map of type map[K]E and implements the Visitor interface.

func (*MapVisitor[K, E]) Visit

func (v *MapVisitor[K, E]) Visit(f func(key K, element E) (bool, error)) error

Visit iterates over the map and calls f for each (key, element). - If f returns (true, nil), iteration continues. - If f returns (false, nil), iteration stops early. - If f returns an error, iteration stops with that error.

type SliceVisitor

type SliceVisitor[E any] struct {
	// contains filtered or unexported fields
}

SliceVisitor implements Visitor[int, interface{}] for []interface{}

func (*SliceVisitor[E]) Visit

func (sw *SliceVisitor[E]) Visit(f func(key int, element E) (bool, error)) error

Visit iterates over the slice, calling the provided function for each element. The key is the slice index.

type StructVisitor

type StructVisitor struct {
	// contains filtered or unexported fields
}

StructVisitor implements Visitor[string, interface{}] for structs using reflection.

func (*StructVisitor) Visit

func (w *StructVisitor) Visit(f func(key string, element interface{}) (bool, error)) error

Visit iterates over struct fields, calling the provided function with each field name and value.

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SyncMap is a thread-safe map backed by sync.Map

func NewSyncMap

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

func (*SyncMap[K, V]) Get

func (m *SyncMap[K, V]) Get(k K) (V, bool)

Get returns a value from the map

func (*SyncMap[K, V]) Put

func (m *SyncMap[K, V]) Put(k K, v V)

Put adds a value to the map

type Visitor

type Visitor[K comparable, E any] func(func(key K, element E) (bool, error)) error

Visitor is an interface that Visits over pairs of (key, element). The Visit method calls the provided callback for each pair. If the callback returns (false, nil), the Visit stops. If the callback returns an error, the Visit stops and returns that error.

func AnyMapVisitorOf

func AnyMapVisitorOf(value interface{}) (Visitor[any, any], error)

AnyMapVisitorOf dynamically creates a MapVisitor from any map value.

func AnySliceVisitorOf

func AnySliceVisitorOf(value interface{}) (Visitor[int, any], error)

AnySliceVisitorOf dynamically creates a SliceVisitor from any slice value.

func AnyTypedMapVisitorOf

func AnyTypedMapVisitorOf[K comparable, V any](aMap map[K]V) Visitor[any, any]

AnyTypedMapVisitorOf return

func AnyTypedSliceVisitorOf

func AnyTypedSliceVisitorOf[E any](slice []E) Visitor[int, any]

AnyTypedSliceVisitorOf return visitor

func MapVisitorOf

func MapVisitorOf[K comparable, E any](aMap map[K]E) Visitor[K, E]

MapVisitorOf creates a new MapVisitor from an interface{}. It must be map[K]E, or an error is returned.

func SliceVisitorOf

func SliceVisitorOf[E any](value interface{}) (Visitor[int, E], error)

SliceVisitorOf creates a Visitor for []interface{}

func StructVisitorOf

func StructVisitorOf(value interface{}) (Visitor[string, interface{}], error)

StructVisitorOf creates a StructVisitor from any struct value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL