diff options
| -rw-r--r-- | refined/builder.go | 14 | ||||
| -rw-r--r-- | refined/refined_test.go | 9 | ||||
| -rw-r--r-- | refined/value.go | 45 |
3 files changed, 34 insertions, 34 deletions
diff --git a/refined/builder.go b/refined/builder.go index 7744320..95b3ab9 100644 --- a/refined/builder.go +++ b/refined/builder.go @@ -11,21 +11,11 @@ import ( "regexp" ) -type Builder[T any, V any] interface { +type ValueType[Self any, T any] interface { MatchesValue(T) error - Build(T, Verification) V + Build(Prevalue[Self, T]) Self } -type Verification interface { - isVerification(verificationSeal) -} - -type verification struct{} - -type verificationSeal struct{} - -func (verification) isVerification(_ verificationSeal) {} - // Building block for writing MatchesValue() implementations. func RegexpMatch(rx *regexp.Regexp, value string) error { if !rx.MatchString(value) { diff --git a/refined/refined_test.go b/refined/refined_test.go index 44f8a7c..118eef6 100644 --- a/refined/refined_test.go +++ b/refined/refined_test.go @@ -20,7 +20,7 @@ var accountNameRx = regexp.MustCompile(`^[a-z_][a-z0-9_]*$`) // Full demonstration of a refinement type for the test. type AccountName struct { - refined.Value[AccountName, AccountName, string] + refined.Value[AccountName, string] } // Demonstration of a struct containing a refinement type. @@ -34,10 +34,13 @@ func (AccountName) MatchesValue(value string) error { } // Build implements the refined.Builder interface. -func (AccountName) Build(value string, v refined.Verification) AccountName { - return AccountName{refined.Build[AccountName](value, v)} +func (AccountName) Build(v refined.Prevalue[AccountName, string]) AccountName { + return AccountName{refined.Build(v)} } +// Example for how to use Literal() to declare a pseudo-const value. +var reservedAccountName = refined.Literal[AccountName]("reserved") + // Example for how to access the contained value in computations. func (n AccountName) ContainerName() string { return fmt.Sprintf("container-for-%s", n.Raw()) diff --git a/refined/value.go b/refined/value.go index 15e9bc1..9c7a8b3 100644 --- a/refined/value.go +++ b/refined/value.go @@ -13,55 +13,62 @@ import ( ) // NOTE: The zero value is illegal and will panic on use. -type Value[B Builder[T, V], V any, T any] struct { +type Value[V ValueType[V, T], T any] struct { value Option[T] } -func Build[B Builder[T, V], V any, T any](value T, _ Verification) Value[B, V, T] { - return Value[B, V, T]{Some(value)} +type Prevalue[V any, T any] struct { + value Option[T] +} + +func Build[V ValueType[V, T], T any](v Prevalue[V, T]) Value[V, T] { + if v.value.IsNone() { + panic("illegal use of zero-valued instance of refined.Prevalue") + } + return Value[V, T]{v.value} } -func New[B Builder[T, V], V any, T any](value T) (V, error) { - var b B - err := b.MatchesValue(value) +func New[V ValueType[V, T], T any](value T) (V, error) { + var builder V + err := builder.MatchesValue(value) if err != nil { var empty V return empty, err } - return b.Build(value, verification{}), nil + return builder.Build(Prevalue[V, T]{Some(value)}), nil } -func Literal[B Builder[T, V], V any, T any](value T) V { - result, err := New[B](value) +func Literal[V ValueType[V, T], T any](value T) V { + result, err := New[V](value) if err != nil { panic(err.Error()) } return result } -func newValue[B Builder[T, V], V any, T any](value T) (Value[B, V, T], error) { - var b B - err := b.MatchesValue(value) +func newValue[V ValueType[V, T], T any](value T) (Value[V, T], error) { + var builder V + err := builder.MatchesValue(value) if err != nil { - return Value[B, V, T]{None[T]()}, err + return Value[V, T]{None[T]()}, err } - return Value[B, V, T]{Some(value)}, nil + return Value[V, T]{Some(value)}, nil } -func (v Value[B, V, T]) Raw() T { - return v.value.UnwrapOrPanic("illegal use of zero-valued instance of Refined type") +func (v Value[V, T]) Raw() T { + return v.value.UnwrapOrPanic("illegal use of zero-valued instance of refined.Value") } -func (v *Value[B, V, T]) UnmarshalJSON(buf []byte) error { +func (v *Value[V, T]) UnmarshalJSON(buf []byte) error { var value T err := json.Unmarshal(buf, &value) if err != nil { return err } - *v, err = newValue[B, V, T](value) + *v, err = newValue[V, T](value) return err } -func (v Value[B, V, T]) MarshalJSON() ([]byte, error) { +func (v Value[V, T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.Raw()) } |
