filter

package
v0.21.8 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 8 Imported by: 2

README

Filter

The filter package is designed to allow you to define filters, primarily by yaml to be able to filter resources. This is used in the nuke package to filter resources based on a set of criteria.

Filter's can be optionally added to a group filters within a group are combined with an AND operation. Filters in different group are combined with an OR operation.

There is also the concept of a global filter that is applied to all resources.

Global

You can define a global filter that will be applied to all resources. This is useful for defining a set of filters that should be applied to all resources.

It has a special key called __global__.

This only works when you are defining it as a resource type as part of the Filters map[string][]Filter type.

Types

There are multiple filter types that can be used to filter the resources. These types are used to match against the property.

  • empty
  • exact
  • glob
  • regex
  • contains
  • dateOlderThan
  • dateOlderThanNow
  • suffix
  • prefix
  • In
  • NotIn
empty / exact

These are identical, if you leave your type empty, it will choose exact. Exact will only match if values are identical.

glob

A glob allows for matching values using asterisk for a wild card, you may have more than one asterisk.

regex

A regex allows for matching values with any valid regular expression.

contains

A contains type allows for matching a value if it has the value contained within the property value.

dateOlderThan

This allows you to filter a property's value based on whether it is older

dateOlderThanNow

This allows you to filter a properties value based on whether it is older than the current time in UTC with an addition or subtraction of a duration value.

For example if the property is CreatedDate and the value is 2024-04-14T12:00:00Z and the current time is 2024-04-14T18:00:00Z then you can set a negative duration like -12h. In this case it would not match, as the CreatedDate would be after the adjusted time.

If you adjusted it -4h then it would match as the CreatedDate would be before the adjusted time.

suffix

This allows you to match a value if the value being filtered on is the suffix of the property value.

prefix

This allows you to match a value if the value being filtered on is the prefix of the property value.

In

This allows you to match a value if it is in a list of values.

NotIn

This allows you to match a value if it is not in a list of values.

Documentation

Overview

Package filter provides a way to filter resources based on a set of criteria.

Index

Constants

View Source
const (
	Empty            Type = ""
	Exact            Type = "exact"
	Glob             Type = "glob"
	Regex            Type = "regex"
	Contains         Type = "contains"
	DateOlderThan    Type = "dateOlderThan"
	DateOlderThanNow Type = "dateOlderThanNow"
	Suffix           Type = "suffix"
	Prefix           Type = "prefix"
	NotIn            Type = "NotIn"
	In               Type = "In"

	And OpType = "and"
	Or  OpType = "or"

	Global = "__global__"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Filter

type Filter struct {
	// Group is the name of the group of filters, all filters in a group are ANDed together
	Group string `yaml:"group" json:"group"`

	// Type is the type of filter to apply
	Type Type `yaml:"type" json:"type"`

	// Property is the name of the property to filter on
	Property string `yaml:"property" json:"property"`

	// Value is the value to filter on
	Value string `yaml:"value" json:"value"`

	// Values allows for multiple values to be specified for a filter
	Values []string `yaml:"values" json:"values"`

	// Invert is a flag to invert the filter
	Invert bool `yaml:"invert" json:"invert"`
}

Filter is a filter to apply to a resource

func NewExactFilter

func NewExactFilter(value string) Filter

NewExactFilter creates a new filter that matches the exact value

func (*Filter) GetGroup added in v0.20.0

func (f *Filter) GetGroup() string

GetGroup returns the group name of the filter, if it is empty it returns "default"

func (*Filter) Match

func (f *Filter) Match(o string) (bool, error)

Match checks if the filter matches the given value

func (*Filter) UnmarshalYAML

func (f *Filter) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML unmarshals a filter from YAML data

func (*Filter) Validate

func (f *Filter) Validate() error

Validate checks if the filter is valid

type Filters

type Filters map[string][]Filter

func (Filters) Append added in v0.10.0

func (f Filters) Append(f2 Filters)

Append appends the filters from f2 to f. This is primarily used to append filters from a preset to a set of filters that were defined on a resource type.

func (Filters) Get added in v0.10.0

func (f Filters) Get(resourceType string) []Filter

Get returns the filters for a specific resource type or the global filters if they exist. If there are no filters it returns nil

func (Filters) GetByGroup added in v0.20.0

func (f Filters) GetByGroup(resourceType string) map[string][]Filter

GetByGroup returns the filters grouped by the group name for a specific resource type. If there are no filters it returns nil

func (Filters) Match added in v0.20.0

func (f Filters) Match(resourceType string, p Property) (bool, error)

Match checks if the filters match the given property which is actually a queue item that meats the property interface requirements

func (Filters) Merge

func (f Filters) Merge(f2 Filters)

Merge is an alias of Append for backwards compatibility Deprecated: use Append instead

func (Filters) Validate

func (f Filters) Validate() error

Validate checks if the filters are valid or not and returns an error if they are not

type OpType added in v0.20.0

type OpType string

type Property added in v0.20.0

type Property interface {
	GetProperty(string) (string, error)
}

type Type

type Type string

Jump to

Keyboard shortcuts

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