-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
56 lines (44 loc) · 1.01 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Package types - Content managed by Project Forge, see [projectforge.md] for details.
package types
import (
"fmt"
)
const KeyString = "string"
type String struct {
MinLength int `json:"minLength,omitempty"`
MaxLength int `json:"maxLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
}
var _ Type = (*String)(nil)
func (x *String) Key() string {
return KeyString
}
func (x *String) Sortable() bool {
return true
}
func (x *String) Scalar() bool {
return true
}
func (x *String) String() string {
if x.MaxLength > 0 {
return fmt.Sprintf("%s(%d)", x.Key(), x.MaxLength)
}
return x.Key()
}
func (x *String) From(v any) any {
switch t := v.(type) {
case string:
return t
default:
return invalidInput(x.Key(), t)
}
}
func (x *String) Default(string) any {
return ""
}
func NewString() *Wrapped {
return Wrap(&String{})
}
func NewStringArgs(minLength int, maxLength int, pattern string) *Wrapped {
return Wrap(&String{MinLength: minLength, MaxLength: maxLength, Pattern: pattern})
}