-
Notifications
You must be signed in to change notification settings - Fork 103
/
handler.go
81 lines (65 loc) · 1.6 KB
/
handler.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package schema
import (
"fmt"
"github.com/mitchellh/hashstructure/v2"
"go.starlark.net/starlark"
)
type Handler struct {
SchemaHandler
}
func newHandler(
thread *starlark.Thread,
_ *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
handler *starlark.Function
type_ starlark.Int
)
if err := starlark.UnpackArgs(
"Handler",
args, kwargs,
"handler", &handler,
"type", &type_,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for Handler: %s", err)
}
handlerType := HandlerReturnType(type_.BigInt().Int64())
if handlerType != ReturnSchema &&
handlerType != ReturnOptions &&
handlerType != ReturnString &&
handlerType != ReturnField {
return nil, fmt.Errorf("invalid handler type %d", int(handlerType))
}
s := &Handler{}
s.Function = handler
s.ReturnType = handlerType
return s, nil
}
func (s *Handler) AsSchemaHandler() SchemaHandler {
return s.SchemaHandler
}
func (s *Handler) AttrNames() []string {
return []string{
"handler", "type",
}
}
func (s *Handler) Attr(name string) (starlark.Value, error) {
switch name {
case "handler":
return s.Function, nil
case "type":
return starlark.MakeInt(int(s.ReturnType)), nil
default:
return nil, nil
}
}
func (s *Handler) String() string { return "Handler(...)" }
func (s *Handler) Type() string { return "Handler" }
func (s *Handler) Freeze() {}
func (s *Handler) Truth() starlark.Bool { return true }
func (s *Handler) Hash() (uint32, error) {
sum, err := hashstructure.Hash(s, hashstructure.FormatV2, nil)
return uint32(sum), err
}