filter

package
v0.0.0-...-9fbc4cb Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 11 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMapOfValues

func GetMapOfValues(val any) map[string]any

GetMapOfValues takes an object and returns a map of all the fields of the object as KV pairs. Any field that is a primitive and cast down to its underlying type, to ensure that type aliases are considered equivalent to their underlying types.

Types

type Operator

type Operator struct {
	Eq      any `json:"eq,omitempty"`
	Ne      any `json:"ne,omitempty"`
	Lte     any `json:"lte,omitempty"`
	Gte     any `json:"gte,omitempty"`
	Lt      any `json:"lt,omitempty"`
	Gt      any `json:"gt,omitempty"`
	Matches any `json:"matches,omitempty"`
}

type TypeFilter

type TypeFilter[T any] struct {
	Any any
}

TypeFilter is an object that allows a caller to filter a list of objects by their fields, using a JSON-compatible expression language.

func NewTypeFilter

func NewTypeFilter[T any]() TypeFilter[T]

NewTypeFilter creates a dynamic struct that mirrors the input type T, and can be used for filtering lists of objects of type T by the fields of T.

func NewTypeFilterFromJson

func NewTypeFilterFromJson[T any](inputJson string) TypeFilter[T]

NewTypeFilterFromJson is a convenience method to create a dynamic TypeFilter struct, and instantiate an object of that type with the provided JSON string.

func (TypeFilter[T]) Filter

func (tf TypeFilter[T]) Filter(vals iter.Seq[T]) iter.Seq[T]

Filter takes a sequence iterator to the filtered type T, and returns an iterator function that can be applied to that input sequence.

func (TypeFilter[T]) FilterAll

func (tf TypeFilter[T]) FilterAll(vals []T) []T

FilterAll is a wrapper around Filter that accepts and returns slices instead of iterators.

func (TypeFilter[T]) GetExpression

func (tf TypeFilter[T]) GetExpression() string

GetExpression turns the JSON representation of this TypeFilter object into a boolean expression that can be used in the "gval.Evaluate" library. For example, if the JSON of the TypeFilter object looks something like this:

`[{"kind": {"eq": "MOVIE"}}]`

then the equivalent expression will be something like this:

`(kind == "MOVIE")`

Note that, due to the simplistic nature of the conversion, there may be innocuous artififacts in the resulting expression, such as extra unnecessary parentheses. However, the expression will be usable and correct.

func (TypeFilter[T]) MarshalJSON

func (tf TypeFilter[T]) MarshalJSON() ([]byte, error)

MarshalJSON overrides the default JSON marshal function so that the inner type is marshalled instead of the TypeFilter outer wrapper.

func (TypeFilter[T]) ShouldInclude

func (tf TypeFilter[T]) ShouldInclude(val any) bool

ShouldInclude accepts an object of type T and determines whether it passes the TypeFilter for type T.

func (TypeFilter[T]) UnmarshalJSON

func (tf TypeFilter[T]) UnmarshalJSON(data []byte) error

MarshalJSON overrides the default JSON unmarshal function so that the inner type is unmarshalled instead of the TypeFilter outer wrapper.

Jump to

Keyboard shortcuts

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