-
Notifications
You must be signed in to change notification settings - Fork 103
/
typeahead.go
93 lines (75 loc) · 1.81 KB
/
typeahead.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
82
83
84
85
86
87
88
89
90
91
92
93
package schema
import (
"fmt"
"github.com/mitchellh/hashstructure/v2"
"go.starlark.net/starlark"
)
type Typeahead struct {
SchemaField
}
func newTypeahead(
thread *starlark.Thread,
_ *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
id starlark.String
name starlark.String
desc starlark.String
icon starlark.String
handler *starlark.Function
)
if err := starlark.UnpackArgs(
"Typeahead",
args, kwargs,
"id", &id,
"name", &name,
"desc", &desc,
"icon", &icon,
"handler", &handler,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for Typeahead: %s", err)
}
s := &Typeahead{}
s.SchemaField.Type = "typeahead"
s.ID = id.GoString()
s.Name = name.GoString()
s.Description = desc.GoString()
s.Icon = icon.GoString()
s.Handler = handler.Name()
s.StarlarkHandler = handler
return s, nil
}
func (s *Typeahead) AsSchemaField() SchemaField {
return s.SchemaField
}
func (s *Typeahead) AttrNames() []string {
return []string{
"id", "name", "desc", "icon", "handler",
}
}
func (s *Typeahead) Attr(name string) (starlark.Value, error) {
switch name {
case "id":
return starlark.String(s.ID), nil
case "name":
return starlark.String(s.Name), nil
case "desc":
return starlark.String(s.Description), nil
case "icon":
return starlark.String(s.Icon), nil
case "handler":
return s.StarlarkHandler, nil
default:
return nil, nil
}
}
func (s *Typeahead) String() string { return "Typeahead(...)" }
func (s *Typeahead) Type() string { return "Typeahead" }
func (s *Typeahead) Freeze() {}
func (s *Typeahead) Truth() starlark.Bool { return true }
func (s *Typeahead) Hash() (uint32, error) {
sum, err := hashstructure.Hash(s, hashstructure.FormatV2, nil)
return uint32(sum), err
}