diff options
| author | Cuong Manh Le <cuong.manhle.vn@gmail.com> | 2025-06-04 00:04:26 +0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-06-04 07:40:51 -0700 |
| commit | 1f2a4d192d71ae5eeaacaa72a8bb2df4e6c08edd (patch) | |
| tree | 29a510ad3340b6319603c9592ef233f5ff039d68 /test | |
| parent | 5b748eed9c479383fc3ea3d018bbd03de1baa6e6 (diff) | |
| download | go-1f2a4d192d71ae5eeaacaa72a8bb2df4e6c08edd.tar.xz | |
test: add another regression test for issue 73309
Fixed #73309
Change-Id: Id715b9c71c95c92143a7fdb5a66b24305346dd3b
Reviewed-on: https://go-review.googlesource.com/c/go/+/678415
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'test')
| -rw-r--r-- | test/fixedbugs/issue73309b.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/test/fixedbugs/issue73309b.go b/test/fixedbugs/issue73309b.go new file mode 100644 index 0000000000..1e29781ba9 --- /dev/null +++ b/test/fixedbugs/issue73309b.go @@ -0,0 +1,88 @@ +// compile + +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +type Unsigned interface { + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr +} + +// a Validator instance +type Validator []Validable + +type Numeric interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64 +} + +func (v Validator) Valid() bool { + for _, field := range v { + if !field.Validate() { + return false + } + } + return true +} + +type Validable interface { + Validate() bool +} + +type FieldDef[T any] struct { + value T + rules []Rule[T] +} + +func (f FieldDef[T]) Validate() bool { + for _, rule := range f.rules { + if !rule(f) { + return false + } + } + return true +} + +type Rule[T any] = func(FieldDef[T]) bool + +func Field[T any](value T, rules ...Rule[T]) *FieldDef[T] { + return &FieldDef[T]{value: value, rules: rules} +} + +type StringRule = Rule[string] + +type NumericRule[T Numeric] = Rule[T] + +type UnsignedRule[T Unsigned] = Rule[T] + +func MinS(n int) StringRule { + return func(fd FieldDef[string]) bool { + return len(fd.value) < n + } +} + +func MinD[T Numeric](n T) NumericRule[T] { + return func(fd FieldDef[T]) bool { + return fd.value < n + } +} + +func MinU[T Unsigned](n T) UnsignedRule[T] { + return func(fd FieldDef[T]) bool { + return fd.value < n + } +} + +func main() { + v := Validator{ + Field("test", MinS(5)), + } + + if !v.Valid() { + println("invalid") + return + } + + println("valid") +} |
