-
Notifications
You must be signed in to change notification settings - Fork 103
/
module.go
215 lines (183 loc) · 5.55 KB
/
module.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package schema
import (
"fmt"
"sync"
"github.com/mitchellh/hashstructure/v2"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
const (
ModuleName = "schema"
)
var (
once sync.Once
module starlark.StringDict
)
func LoadModule() (starlark.StringDict, error) {
once.Do(func() {
handlerType := starlarkstruct.FromStringDict(
starlark.String("HandlerType"),
map[string]starlark.Value{
"Schema": starlark.MakeInt(int(ReturnSchema)),
"Options": starlark.MakeInt(int(ReturnOptions)),
"String": starlark.MakeInt(int(ReturnString)),
"Field": starlark.MakeInt(int(ReturnField)),
},
)
module = starlark.StringDict{
ModuleName: &starlarkstruct.Module{
Name: ModuleName,
Members: starlark.StringDict{
"Schema": starlark.NewBuiltin("Schema", newSchema),
"Toggle": starlark.NewBuiltin("Toggle", newToggle),
"Option": starlark.NewBuiltin("Option", newOption),
"Dropdown": starlark.NewBuiltin("Dropdown", newDropdown),
"Location": starlark.NewBuiltin("Location", newLocation),
"Text": starlark.NewBuiltin("Text", newText),
"LocationBased": starlark.NewBuiltin("LocationBased", newLocationBased),
"DateTime": starlark.NewBuiltin("DateTime", newDateTime),
"OAuth2": starlark.NewBuiltin("OAuth2", newOAuth2),
"PhotoSelect": starlark.NewBuiltin("PhotoSelect", newPhotoSelect),
"Typeahead": starlark.NewBuiltin("Typeahead", newTypeahead),
"Handler": starlark.NewBuiltin("Handler", newHandler),
"HandlerType": handlerType,
"Generated": starlark.NewBuiltin("Generated", newGenerated),
"Color": starlark.NewBuiltin("Color", newColor),
"Notification": starlark.NewBuiltin("Notification", newNotification),
"Sound": starlark.NewBuiltin("Sound", newSound),
},
},
}
})
return module, nil
}
type Field interface {
AsSchemaField() SchemaField
}
type StarlarkSchema struct {
Schema
Handlers map[string]SchemaHandler
starlarkFields *starlark.List
starlarkHandlers *starlark.List
starlarkNotifications *starlark.List
}
func newSchema(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var (
version starlark.String
fields *starlark.List
handlers *starlark.List
notifications *starlark.List
)
if err := starlark.UnpackArgs(
"Schema",
args, kwargs,
"version", &version,
"fields?", &fields,
"handlers?", &handlers,
"notifications?", ¬ifications,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for Schema: %s", err)
}
if version.GoString() != "1" {
return nil, fmt.Errorf("only schema version 1 is supported, not: %s", version.GoString())
}
s := &StarlarkSchema{
Schema: Schema{
Version: version.GoString(),
},
Handlers: map[string]SchemaHandler{},
starlarkFields: fields,
starlarkHandlers: handlers,
starlarkNotifications: notifications,
}
if s.starlarkFields != nil {
fieldIter := s.starlarkFields.Iterate()
defer fieldIter.Done()
var fieldVal starlark.Value
for i := 0; fieldIter.Next(&fieldVal); {
if _, isNone := fieldVal.(starlark.NoneType); isNone {
continue
}
f, ok := fieldVal.(Field)
if !ok {
return nil, fmt.Errorf(
"expected fields to be a list of Field but found: %s (at index %d)",
fieldVal.Type(),
i,
)
} else if f.AsSchemaField().Type == "notification" {
return nil, fmt.Errorf(
"expected fields to be a list of Field but found: %s (at index %d)",
fieldVal.Type(),
i,
)
}
s.Schema.Fields = append(s.Schema.Fields, f.AsSchemaField())
}
}
if s.starlarkHandlers != nil {
handlerIter := s.starlarkHandlers.Iterate()
defer handlerIter.Done()
var handlerVal starlark.Value
for i := 0; handlerIter.Next(&handlerVal); {
handler, ok := handlerVal.(*Handler)
if !ok {
return nil, fmt.Errorf(
"expected handlers to hold Handler but found: %s (at index %d)",
handlerVal.Type(),
i,
)
}
s.Handlers[handler.Function.Name()] = handler.SchemaHandler
}
}
if s.starlarkNotifications != nil {
notificationIter := s.starlarkNotifications.Iterate()
defer notificationIter.Done()
var notificationVal starlark.Value
for i := 0; notificationIter.Next(¬ificationVal); {
if _, isNone := notificationVal.(starlark.NoneType); isNone {
continue
}
n, ok := notificationVal.(*Notification)
if !ok {
return nil, fmt.Errorf(
"expected notifications to be a list of Notification but found: %s (at index %d)",
notificationVal.Type(),
i,
)
}
s.Schema.Notifications = append(s.Schema.Notifications, n.AsSchemaField())
}
}
return s, nil
}
func (s StarlarkSchema) AttrNames() []string {
return []string{
"version",
"fields",
"handlers",
}
}
func (s StarlarkSchema) Attr(name string) (starlark.Value, error) {
switch name {
case "version":
return starlark.String(s.Version), nil
case "fields":
return s.starlarkFields, nil
case "handlers":
return s.starlarkHandlers, nil
case "notifications":
return s.starlarkNotifications, nil
default:
return nil, nil
}
}
func (s StarlarkSchema) String() string { return "Schema(...)" }
func (s StarlarkSchema) Type() string { return "Schema" }
func (s StarlarkSchema) Freeze() {}
func (s StarlarkSchema) Truth() starlark.Bool { return true }
func (s StarlarkSchema) Hash() (uint32, error) {
sum, err := hashstructure.Hash(s, hashstructure.FormatV2, nil)
return uint32(sum), err
}