github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/app/lib/types/method.go (about) 1 // Package types - Content managed by Project Forge, see [projectforge.md] for details. 2 package types 3 4 import ( 5 "fmt" 6 "strings" 7 8 "github.com/samber/lo" 9 ) 10 11 type Argument struct { 12 Key string `json:"key"` 13 Type *Wrapped `json:"type"` 14 } 15 16 func (a Argument) String() string { 17 return fmt.Sprintf("%s %s", a.Key, a.Type.String()) 18 } 19 20 type Arguments []Argument 21 22 const KeyMethod = "method" 23 24 type Method struct { 25 Args Arguments `json:"args,omitempty"` 26 Ret *Wrapped `json:"ret,omitempty"` 27 } 28 29 var _ Type = (*Method)(nil) 30 31 func (x *Method) Key() string { 32 return KeyMethod 33 } 34 35 func (x *Method) Sortable() bool { 36 for _, a := range x.Args { 37 if !a.Type.Sortable() { 38 return false 39 } 40 } 41 return x.Ret.Sortable() 42 } 43 44 func (x *Method) Scalar() bool { 45 return false 46 } 47 48 func (x *Method) String() string { 49 argStrings := lo.Map(x.Args, func(arg Argument, _ int) string { 50 return arg.String() 51 }) 52 return fmt.Sprintf("fn(%s) %s", strings.Join(argStrings, ", "), x.Ret.String()) 53 } 54 55 func (x *Method) From(v any) any { 56 return invalidInput(x.Key(), v) 57 } 58 59 func (x *Method) Default(key string) any { 60 return key + "()" 61 } 62 63 func NewMethod(ret *Wrapped) *Wrapped { 64 return Wrap(&Method{Ret: ret}) 65 }