Source file test/fixedbugs/issue73309b.go

     1  // compile
     2  
     3  // Copyright 2025 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  type Unsigned interface {
    10  	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
    11  }
    12  
    13  // a Validator instance
    14  type Validator []Validable
    15  
    16  type Numeric interface {
    17  	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
    18  }
    19  
    20  func (v Validator) Valid() bool {
    21  	for _, field := range v {
    22  		if !field.Validate() {
    23  			return false
    24  		}
    25  	}
    26  	return true
    27  }
    28  
    29  type Validable interface {
    30  	Validate() bool
    31  }
    32  
    33  type FieldDef[T any] struct {
    34  	value T
    35  	rules []Rule[T]
    36  }
    37  
    38  func (f FieldDef[T]) Validate() bool {
    39  	for _, rule := range f.rules {
    40  		if !rule(f) {
    41  			return false
    42  		}
    43  	}
    44  	return true
    45  }
    46  
    47  type Rule[T any] = func(FieldDef[T]) bool
    48  
    49  func Field[T any](value T, rules ...Rule[T]) *FieldDef[T] {
    50  	return &FieldDef[T]{value: value, rules: rules}
    51  }
    52  
    53  type StringRule = Rule[string]
    54  
    55  type NumericRule[T Numeric] = Rule[T]
    56  
    57  type UnsignedRule[T Unsigned] = Rule[T]
    58  
    59  func MinS(n int) StringRule {
    60  	return func(fd FieldDef[string]) bool {
    61  		return len(fd.value) < n
    62  	}
    63  }
    64  
    65  func MinD[T Numeric](n T) NumericRule[T] {
    66  	return func(fd FieldDef[T]) bool {
    67  		return fd.value < n
    68  	}
    69  }
    70  
    71  func MinU[T Unsigned](n T) UnsignedRule[T] {
    72  	return func(fd FieldDef[T]) bool {
    73  		return fd.value < n
    74  	}
    75  }
    76  
    77  func main() {
    78  	v := Validator{
    79  		Field("test", MinS(5)),
    80  	}
    81  
    82  	if !v.Valid() {
    83  		println("invalid")
    84  		return
    85  	}
    86  
    87  	println("valid")
    88  }
    89  

View as plain text