filter

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const TagKey = "~~~filter~~~"

TagKey is the tag key used to marshal and unmarshal filter to and from map[string]any.

Variables

View Source
var (
	// DefaultLimits provides reasonable defaults for most use cases.
	DefaultLimits = &ComplexityLimits{
		MaxDepth:            3,
		MaxTotalFields:      10,
		MaxLogicalOperators: 5,
		MaxLogicalDepth:     2,
		MaxOrBranches:       3,
	}

	// StrictLimits provides tighter limits for security-sensitive contexts.
	StrictLimits = &ComplexityLimits{
		MaxDepth:            2,
		MaxTotalFields:      5,
		MaxLogicalOperators: 3,
		MaxLogicalDepth:     1,
		MaxOrBranches:       2,
	}

	// RelaxedLimits provides looser limits for trusted/internal use.
	RelaxedLimits = &ComplexityLimits{
		MaxDepth:            5,
		MaxTotalFields:      20,
		MaxLogicalOperators: 10,
		MaxLogicalDepth:     3,
		MaxOrBranches:       5,
	}
)

Predefined complexity limits

View Source
var Initialisms = map[string]bool{
	"acl":   true,
	"amqp":  true,
	"api":   true,
	"ascii": true,
	"cpu":   true,
	"css":   true,
	"db":    true,
	"dns":   true,
	"eof":   true,
	"gid":   true,
	"guid":  true,
	"html":  true,
	"http":  true,
	"https": true,
	"id":    true,
	"ip":    true,
	"json":  true,
	"qps":   true,
	"ram":   true,
	"rpc":   true,
	"rtp":   true,
	"sip":   true,
	"sla":   true,
	"smtp":  true,
	"sql":   true,
	"ssh":   true,
	"tcp":   true,
	"tls":   true,
	"ts":    true,
	"ttl":   true,
	"udp":   true,
	"ui":    true,
	"uid":   true,
	"uri":   true,
	"url":   true,
	"utf8":  true,
	"uuid":  true,
	"vm":    true,
	"xml":   true,
	"xmpp":  true,
	"xsrf":  true,
	"xss":   true,
}

Initialisms is a set of common initialisms for identifier naming. Based on https://github.com/dominikh/go-tools/blob/master/config/example.conf This is the canonical list from staticcheck.

Functions

func Capitalize added in v0.9.0

func Capitalize(s string) string

Capitalize simply capitalizes the first letter without acronym handling

func CheckComplexity added in v0.9.0

func CheckComplexity(filterMap map[string]any, limits *ComplexityLimits) error

CheckComplexity validates that a filter map doesn't exceed the specified limits. Returns an error describing which limit was exceeded, or nil if within limits. If limits is nil, no validation is performed.

func Prune added in v0.9.0

func Prune(m map[string]any)

Prune recursively removes nil values, empty slices, and empty nested maps.

func SmartPascalCase added in v0.9.0

func SmartPascalCase(s string) string

SmartPascalCase converts a string to PascalCase with proper handling of common acronyms. It handles consecutive uppercase letters specially: a sequence like "HTMLParser" is split into "HTML" + "Parser" rather than individual letters. Uses Initialisms from staticcheck.

func ToMap added in v0.7.4

func ToMap(v any) (map[string]any, error)

ToMap converts a filter to a map[string]any.

func Transform added in v0.9.0

func Transform(sourceMap map[string]any, transform TransformFunc) (map[string]any, error)

Transform applies transformations to a filter map.

func WithSmartPascalCase added in v0.9.0

func WithSmartPascalCase() func(next TransformFunc) TransformFunc

WithSmartPascalCase creates a transform hook that uses SmartPascalCase for all keys

Types

type Boolean

type Boolean struct {
	Eq     *bool
	Neq    *bool
	IsNull *bool
}

Boolean provides filtering operations for boolean fields.

type ComplexityLimits added in v0.9.0

type ComplexityLimits struct {
	MaxDepth            int // Maximum nesting depth of relationship filters
	MaxTotalFields      int // Maximum total number of field filters
	MaxLogicalOperators int // Maximum number of logical operators (And/Or/Not)
	MaxLogicalDepth     int // Maximum nesting depth of logical operators
	MaxOrBranches       int // Maximum branches in a single Or operator
}

ComplexityLimits defines limits for filter complexity. A value of 0 means no limit for that metric.

type ComplexityResult added in v0.9.0

type ComplexityResult struct {
	Depth            int // Deepest nesting level reached
	TotalFields      int // Total number of field filters
	LogicalOperators int // Total number of logical operators
	LogicalDepth     int // Deepest logical operator nesting
	OrBranches       int // Maximum branches found in any Or operator
}

ComplexityResult contains the calculated complexity metrics of a filter.

func CalculateComplexity added in v0.9.0

func CalculateComplexity(filterMap map[string]any) (*ComplexityResult, error)

CalculateComplexity analyzes a filter map and returns its complexity metrics.

type Float

type Float struct {
	Eq     *float64
	Neq    *float64
	Lt     *float64
	Lte    *float64
	Gt     *float64
	Gte    *float64
	In     []float64
	NotIn  []float64
	IsNull *bool
}

Float provides filtering operations for float64 fields.

type ID

type ID String

ID is an alias for String, used for identifier fields.

type Int

type Int struct {
	Eq     *int
	Neq    *int
	Lt     *int
	Lte    *int
	Gt     *int
	Gte    *int
	In     []int
	NotIn  []int
	IsNull *bool
}

Int provides filtering operations for integer fields.

type KeyPath added in v0.9.0

type KeyPath []string

func (KeyPath) Last added in v0.9.0

func (k KeyPath) Last() string

func (KeyPath) String added in v0.9.0

func (k KeyPath) String() string

String returns a human-readable representation of the key path. Array indices like "[0]" are appended directly without a dot separator. Example: ["And", "[0]", "Name", "Eq"] -> "And[0].Name.Eq"

type KeyType added in v0.9.0

type KeyType string

KeyType represents the type of a filter key

const (
	// KeyTypeLogical represents a logical operator key (And, Or, Not)
	KeyTypeLogical KeyType = "LOGICAL"

	// KeyTypeField represents a scalar field filter key (Name, Code, CategoryID, etc.)
	KeyTypeField KeyType = "FIELD"

	// KeyTypeRelationship represents a relationship filter key (Category, Author, etc.)
	KeyTypeRelationship KeyType = "RELATIONSHIP"

	// KeyTypeOperator represents a filter operator key (Eq, Contains, In, Gt, etc.)
	KeyTypeOperator KeyType = "OPERATOR"

	// KeyTypeModifier represents a modifier key that changes operator behavior (Fold, etc.)
	KeyTypeModifier KeyType = "MODIFIER"
)

type String

type String struct {
	Eq         *string
	Neq        *string
	Lt         *string
	Lte        *string
	Gt         *string
	Gte        *string
	In         []string
	NotIn      []string
	IsNull     *bool
	Contains   *string
	StartsWith *string
	EndsWith   *string
	Fold       bool
}

String provides filtering operations for string fields. All comparison operations can be case-insensitive when Fold is set to true.

type Time

type Time struct {
	Eq     *time.Time
	Neq    *time.Time
	Lt     *time.Time
	Lte    *time.Time
	Gt     *time.Time
	Gte    *time.Time
	In     []time.Time
	NotIn  []time.Time
	IsNull *bool
}

Time provides filtering operations for time.Time fields.

type TransformFunc added in v0.9.0

type TransformFunc func(input *TransformInput) (*TransformOutput, error)

TransformFunc is a function that transforms keys and values.

type TransformInput added in v0.9.0

type TransformInput struct {
	KeyPath    KeyPath
	KeyType    KeyType
	Value      any
	Containers []any // Containers[i] is the container that holds KeyPath[i]
}

TransformInput provides input information for transformation

func (*TransformInput) ContainerAt added in v0.9.0

func (t *TransformInput) ContainerAt(index int) any

func (*TransformInput) Current added in v0.9.0

func (t *TransformInput) Current() any

func (*TransformInput) CurrentMap added in v0.9.0

func (t *TransformInput) CurrentMap() map[string]any

func (*TransformInput) Parent added in v0.9.0

func (t *TransformInput) Parent() any

func (*TransformInput) ParentMap added in v0.9.0

func (t *TransformInput) ParentMap() map[string]any

func (*TransformInput) Root added in v0.9.0

func (t *TransformInput) Root() any

func (*TransformInput) RootMap added in v0.9.0

func (t *TransformInput) RootMap() map[string]any

type TransformOutput added in v0.9.0

type TransformOutput struct {
	Key   string
	Value any
}

TransformOutput represents the result of transformation. Return nil or empty Key to skip this key.

Directories

Path Synopsis
Package gormfilter provides powerful and type-safe filtering capabilities for GORM queries.
Package gormfilter provides powerful and type-safe filtering capabilities for GORM queries.

Jump to

Keyboard shortcuts

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