-
Notifications
You must be signed in to change notification settings - Fork 4
/
method.go
65 lines (50 loc) · 1.16 KB
/
method.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
57
58
59
60
61
62
63
64
65
// Package types - Content managed by Project Forge, see [projectforge.md] for details.
package types
import (
"fmt"
"strings"
"github.com/samber/lo"
)
type Argument struct {
Key string `json:"key"`
Type *Wrapped `json:"type"`
}
func (a Argument) String() string {
return fmt.Sprintf("%s %s", a.Key, a.Type.String())
}
type Arguments []Argument
const KeyMethod = "method"
type Method struct {
Args Arguments `json:"args,omitempty"`
Ret *Wrapped `json:"ret,omitempty"`
}
var _ Type = (*Method)(nil)
func (x *Method) Key() string {
return KeyMethod
}
func (x *Method) Sortable() bool {
for _, a := range x.Args {
if !a.Type.Sortable() {
return false
}
}
return x.Ret.Sortable()
}
func (x *Method) Scalar() bool {
return false
}
func (x *Method) String() string {
argStrings := lo.Map(x.Args, func(arg Argument, _ int) string {
return arg.String()
})
return fmt.Sprintf("fn(%s) %s", strings.Join(argStrings, ", "), x.Ret.String())
}
func (x *Method) From(v any) any {
return invalidInput(x.Key(), v)
}
func (x *Method) Default(key string) any {
return key + "()"
}
func NewMethod(ret *Wrapped) *Wrapped {
return Wrap(&Method{Ret: ret})
}