-
Notifications
You must be signed in to change notification settings - Fork 288
/
set_state.go
62 lines (52 loc) · 1.71 KB
/
set_state.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
package starkit
import (
"fmt"
"reflect"
"go.starlark.net/starlark"
)
// SetState works like SetState in React. It can take a value or a function.
//
// That function should transform old state into new state.
//
// For example, an extension that accumulated strings might use
// SetState() like this:
//
// err := starkit.SetState(t, func(strings []string) {
// return append([]string{newString}, strings...)
// })
//
// This would be so much easier with generics :grimace:
//
// SetState will return an error if it can't match the type
// of anything in the state store.
func SetState(t *starlark.Thread, valOrFn interface{}) error {
typ := reflect.TypeOf(valOrFn)
model, ok := t.Local(modelKey).(Model)
if !ok {
return fmt.Errorf("Internal error: Starlark not initialized correctly: starkit.Model not found")
}
isFn := typ.Kind() == reflect.Func
if !isFn {
// If there's already a value with this type in the state store, overwrite it.
_, ok := model.state[typ]
if !ok {
return fmt.Errorf("Internal error: Type not found in state store: %T", valOrFn)
}
model.state[typ] = valOrFn
return nil
}
// We have a function! Validate its signature.
if typ.NumIn() != 1 || typ.NumOut() != 1 || typ.In(0) != typ.Out(0) {
return fmt.Errorf("Internal error: invalid SetState call: signature must be func(T): T")
}
inTyp := typ.In(0)
// Overwrite the value in the state store.
existing, ok := model.state[inTyp]
if !ok {
return fmt.Errorf("Internal error: Type not found in state store: %T", valOrFn)
}
outs := reflect.ValueOf(valOrFn).Call([]reflect.Value{reflect.ValueOf(existing)})
// We know this is valid because of the type validation check above.
model.state[inTyp] = outs[0].Interface()
return nil
}