From eab38629013e34b4490be4d665142a1357c97156 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Wed, 2 Apr 2025 18:09:46 +0200 Subject: demonstration of refinement types without self-referential types The big "yuck" about this can be seen in the LiteralValue invocations in the test code. Calling NewValue or LiteralValue is extremely convoluted and would likely need to be wrapped through additional boilerplate at the concrete type declaration site. --- refined/condition.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 refined/condition.go (limited to 'refined/condition.go') diff --git a/refined/condition.go b/refined/condition.go new file mode 100644 index 0000000..5b43554 --- /dev/null +++ b/refined/condition.go @@ -0,0 +1,20 @@ +/******************************************************************************* +* Copyright 2025 Stefan Majewsky +* SPDX-License-Identifier: Apache-2.0 +* Refer to the file "LICENSE" for details. +*******************************************************************************/ + +package refined + +import ( + "fmt" + "regexp" +) + +// Building block for writing MatchesValue() implementations. +func RegexpMatch(rx *regexp.Regexp, value string) error { + if !rx.MatchString(value) { + return fmt.Errorf("provided value %q does not match expected pattern %q", value, rx.String()) + } + return nil +} -- cgit v1.2.3