-
Notifications
You must be signed in to change notification settings - Fork 2
/
value_function.go
193 lines (157 loc) · 4.64 KB
/
value_function.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
package sdktypes
import (
"bytes"
"errors"
"fmt"
"google.golang.org/protobuf/proto"
"go.autokitteh.dev/autokitteh/internal/kittehs"
modulev1 "go.autokitteh.dev/autokitteh/proto/gen/go/autokitteh/module/v1"
valuev1 "go.autokitteh.dev/autokitteh/proto/gen/go/autokitteh/values/v1"
)
type FunctionValuePB = valuev1.Function
type functionValueTraits struct{}
func (functionValueTraits) Validate(m *FunctionValuePB) error {
return errors.Join(
executorIDField("executor_id", m.ExecutorId),
nameField("name", m.Name),
objectField[ModuleFunction]("function", m.Desc),
validateFunctionFlags(m.Flags),
)
}
func (functionValueTraits) StrictValidate(m *FunctionValuePB) error {
return errors.Join(
mandatory("name", m.Name),
)
}
var _ objectTraits[*FunctionValuePB] = functionValueTraits{}
type FunctionValue struct {
object[*FunctionValuePB, functionValueTraits]
}
func (FunctionValue) isConcreteValue() {}
func (f FunctionValue) ExecutorID() ExecutorID {
return kittehs.Must1(ParseExecutorID(f.read().ExecutorId))
}
func (f FunctionValue) Data() []byte { return f.read().Data }
func (f FunctionValue) ArgNames() []string {
fdesc := f.read().Desc
if fdesc == nil {
return nil
}
return kittehs.Transform(fdesc.Input, func(f *modulev1.FunctionField) string { return f.Name })
}
func (f FunctionValue) Name() Symbol { return kittehs.Must1(ParseSymbol(f.read().Name)) }
func (f FunctionValue) UniqueID() string { return fmt.Sprintf("%v.%s", f.ExecutorID(), f.Name()) }
func (f FunctionValue) HasFlag(flag FunctionFlag) bool {
return kittehs.ContainedIn(f.read().Flags...)(flag.String())
}
func (v Value) IsFunction() bool { return v.read().Function != nil }
func (v Value) GetFunction() FunctionValue { return forceFromProto[FunctionValue](v.read().Function) }
func init() {
registerValueGetter(func(v Value) concreteValue {
if v.IsFunction() {
return v.GetFunction()
}
return nil
})
}
type FunctionFlag string
const (
PrivilidgedFunctionFlag FunctionFlag = "privilidged" // pass workflow context.
PureFunctionFlag FunctionFlag = "pure" // do not run in an activity.
DisablePollingFunctionFlag FunctionFlag = "no-poll" // do not poll.
ConstFunctionFlag FunctionFlag = "const" // result is serialized in data.
)
func (ff FunctionFlag) String() string { return string(ff) }
func validateFunctionFlags(fs []string) error {
var errs []error
for _, f := range fs {
switch f {
case PrivilidgedFunctionFlag.String(), PureFunctionFlag.String(),
DisablePollingFunctionFlag.String(), ConstFunctionFlag.String():
return nil
default:
errs = append(errs, fmt.Errorf("invalid function flag %q", f))
}
}
return errors.Join(errs...)
}
func NewFunctionValue(xid ExecutorID, name string, data []byte, flags []FunctionFlag, desc ModuleFunction) (Value, error) {
return ValueFromProto(
&ValuePB{
Function: &FunctionValuePB{
ExecutorId: xid.String(),
Name: name,
Desc: desc.ToProto(),
Data: data,
Flags: kittehs.TransformToStrings(flags),
},
},
)
}
const (
dataHeader = 'D'
errorHeader = 'E'
)
func NewConstFunctionValue(name string, data Value) (Value, error) {
bs, err := proto.Marshal(data.ToProto())
if err != nil {
return InvalidValue, err
}
buf := bytes.NewBuffer([]byte{dataHeader})
if _, err := buf.Write(bs); err != nil {
return InvalidValue, err
}
return NewFunctionValue(
InvalidExecutorID,
name,
buf.Bytes(),
[]FunctionFlag{ConstFunctionFlag},
ModuleFunction{},
)
}
func NewConstFunctionError(name string, in error) (Value, error) {
perr := WrapError(in)
bs, err := proto.Marshal(perr.ToProto())
if err != nil {
return InvalidValue, err
}
buf := bytes.NewBuffer([]byte{errorHeader})
if _, err := buf.Write(bs); err != nil {
return InvalidValue, err
}
return NewFunctionValue(
InvalidExecutorID,
name,
buf.Bytes(),
[]FunctionFlag{ConstFunctionFlag},
ModuleFunction{},
)
}
func (f FunctionValue) ConstValue() (Value, error) {
if !f.HasFlag(ConstFunctionFlag) {
return InvalidValue, fmt.Errorf("function is not a const")
}
bs := f.m.Data
if len(bs) == 0 {
return InvalidValue, errors.New("empty data")
}
k, bs := bs[0], bs[1:]
if k == dataHeader {
var pb ValuePB
if err := proto.Unmarshal(bs, &pb); err != nil {
return InvalidValue, err
}
return ValueFromProto(&pb)
} else if k == errorHeader {
var pb ProgramErrorPB
if err := proto.Unmarshal(bs, &pb); err != nil {
return InvalidValue, err
}
perr, err := ProgramErrorFromProto(&pb)
if err != nil {
return InvalidValue, err
}
return InvalidValue, perr.ToError()
}
return InvalidValue, fmt.Errorf("invalid data type %q", k)
}