regexpext

package
v0.0.0-...-fb5e452 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Package regexpext provides Regexp wrapper types that automatically compile regex strings while they are being unmarshaled from YAML or JSON files. If the compilation fails, the error will be reported as part of the Unmarshal operation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundedRegexp

type BoundedRegexp string

BoundedRegexp is like PlainRegexp, but ^ and $ anchors will automatically be added to the start and end of the regexp, respectively. For example, when unmarshaling the value "foo|bar" into a BoundedRegexp, the unmarshaled regexp will be "^(?:foo|bar)$".

During unmarshaling, absent string values will behave the same as empty string values. In both cases, the Regexp will be identical to "^$" and only match empty inputs.

func (BoundedRegexp) FindStringSubmatch

func (r BoundedRegexp) FindStringSubmatch(in string) []string

FindStringSubmatch is a shorthand for `r.Regexp()` followed by `rx.FindStringSubmatch()`. If regex parsing returns an error, this function returns nil.

func (BoundedRegexp) MarshalJSON

func (r BoundedRegexp) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (BoundedRegexp) MarshalYAML

func (r BoundedRegexp) MarshalYAML() (any, error)

MarshalYAML implements the yaml.Marshaler interface.

func (BoundedRegexp) MatchString

func (r BoundedRegexp) MatchString(in string) bool

MatchString is a shorthand for `r.Regexp()` followed by `rx.MatchString()`. If regex parsing returns an error, this function returns false.

func (BoundedRegexp) Regexp

func (r BoundedRegexp) Regexp() (*regexp.Regexp, error)

Regexp returns the parsed regexp.Regexp instance for this BoundedRegexp. An error is returned if the regular expression string inside this BoundedRegexp is invalid. If the BoundedRegexp was unmarshaled from JSON or YAML, no error will ever be returned because invalid regex strings were already rejected during unmarshalling.

func (*BoundedRegexp) UnmarshalJSON

func (r *BoundedRegexp) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*BoundedRegexp) UnmarshalYAML

func (r *BoundedRegexp) UnmarshalYAML(u func(any) error) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type ConfigSet

type ConfigSet[K ~string, V any] []struct {
	Key   BoundedRegexp `json:"key" yaml:"key"`
	Value V             `json:"value" yaml:"value"`
}

ConfigSet works similar to map[K]V in that it picks values of type V for keys of type K, but the keys in the data structure are actually regexes that can apply to an entire set of K instead of just one specific value of K.

This type is intended to use in configuration, as a substitute for map[K]V that satisfies the DRY rule (Don't Repeat Yourself), or when the full set of relevant keys is not known at config editing time.

func (ConfigSet[K, V]) Pick

func (cs ConfigSet[K, V]) Pick(key K) Option[V]

Pick returns the first value entry whose key regex matches the supplied key, or None if no entry in the ConfigSet matches the key.

func (ConfigSet[K, V]) PickAndFill

func (cs ConfigSet[K, V]) PickAndFill(key K, fill func(value *V, expand func(string) string)) Option[V]

PickAndFill is like Pick, but if the regex in the matching entry contains parenthesized subexpressions (also known as capture groups), the fill callback is used to expand references to the captured texts in the value.

Usage in code looks like this:

type ObjectType string
type MetricSource struct {
	PrometheusQuery string
}
var cs ConfigSet[ObjectType, MetricSource]
// omitted: fill `cs` by parsing configuration

objectName := "foo_widget"
metricSource := cs.PickAndFill(objectName, func(ms *MetricSource, expand func(string) string) {
	ms.PrometheusQuery = expand(ms.PrometheusQuery)
}).UnwrapOr(MetricSource{})

With this, configuration can be condensed like in the example below:

originalConfig := ConfigSet[ObjectType, MetricSource]{
	{ Key: "foo_widget", Value: MetricSource{PrometheusQuery: "count(foo_widgets)"} },
	{ Key: "bar_widget", Value: MetricSource{PrometheusQuery: "count(bar_widgets)"} },
	{ Key: "qux_widget", Value: MetricSource{PrometheusQuery: "count(qux_widgets)"} },
}
condensedConfig := ConfigSet[ObjectType, MetricSource]{
	{ Key: "(foo|bar|qux)_widget", Value: MetricSource{PrometheusQuery: "count(${1}_widgets)"} },
}

Expansion follows the same rules as for regexp.ExpandString() from the standard library.

type PlainRegexp

type PlainRegexp string

PlainRegexp is a regex string that implements the Marshaler and Unmarshaler interfaces for encoding/json and gopkg.in/yaml.v2, respectively. This type also works with gopkg.in/yaml.v3: Even though the Unmarshaler interface has changed in yaml.v3, v3 also still supports the v2 interface. The "PlainRegexp" name is in contrast to type BoundedRegexp, see documentation over there.

During unmarshaling, absent string values will behave the same as empty string values. In both cases, the Regexp will match every input.

func (PlainRegexp) FindStringSubmatch

func (r PlainRegexp) FindStringSubmatch(in string) []string

FindStringSubmatch is a shorthand for `r.Regexp()` followed by `rx.FindStringSubmatch()`. If regex parsing returns an error, this function returns nil.

func (PlainRegexp) MarshalJSON

func (r PlainRegexp) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (PlainRegexp) MarshalYAML

func (r PlainRegexp) MarshalYAML() (any, error)

MarshalYAML implements the yaml.Marshaler interface.

func (PlainRegexp) MatchString

func (r PlainRegexp) MatchString(in string) bool

MatchString is a shorthand for `r.Regexp()` followed by `rx.MatchString()`. If regex parsing returns an error, this function returns false.

func (PlainRegexp) Regexp

func (r PlainRegexp) Regexp() (*regexp.Regexp, error)

Regexp returns the parsed regexp.Regexp instance for this PlainRegexp. An error is returned if the regular expression string inside this PlainRegexp is invalid. If the PlainRegexp was unmarshaled from JSON or YAML, no error will ever be returned because invalid regex strings were already rejected during unmarshalling.

func (*PlainRegexp) UnmarshalJSON

func (r *PlainRegexp) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*PlainRegexp) UnmarshalYAML

func (r *PlainRegexp) UnmarshalYAML(u func(any) error) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

Jump to

Keyboard shortcuts

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