-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux.go
105 lines (94 loc) · 2.72 KB
/
mux.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
package gravita
import (
"context"
"encoding/json"
"fmt"
)
type Mux struct {
NotMatchHandler LambdaUDFHandler
entries []*Entry
}
func NewMux() *Mux {
return &Mux{}
}
type lambdaUDFOutputData struct {
Success bool `json:"success"`
ErrorMsg string `json:"error_msg,omitempty"`
NumRecords int `json:"num_records,omitempty"`
Results []interface{} `json:"results,omitempty"`
}
func (mux *Mux) HandleLambdaEvent(ctx context.Context, event *LambdaUDFEvent) (jsonStr string, funcErr error) {
defer func() {
if panicValue := recover(); panicValue != nil {
if err, ok := panicValue.(error); ok {
funcErr = err
} else {
panic(panicValue)
}
}
}()
var handler LambdaUDFHandler
for _, e := range mux.entries {
if e.Match(event) {
handler = e.GetHandler()
if handler != nil {
break
}
}
}
if handler == nil {
if mux.NotMatchHandler != nil {
handler = mux.NotMatchHandler
} else {
handler = LambdaUDFHandlerFunc(func(_ context.Context, _ [][]interface{}) ([]interface{}, error) {
return nil, fmt.Errorf("external function `%s` not match", event.ExternalFunction)
})
}
}
var output lambdaUDFOutputData
ctxWithMetadata := withMetadata(ctx, &event.LambdaUDFEventMetadata)
results, err := handler.ExecuteUDF(ctxWithMetadata, event.Arguments)
if err != nil {
output.Success = false
output.ErrorMsg = err.Error()
} else {
n := len(results)
if n == event.NumRecords {
output.NumRecords = n
output.Results = results
} else if n < event.NumRecords {
output.NumRecords = event.NumRecords
output.Results = make([]interface{}, event.NumRecords)
copy(output.Results[:n], results[:n])
} else {
output.NumRecords = event.NumRecords
output.Results = results[:event.NumRecords]
}
output.Success = true
}
var bs []byte
bs, err = json.Marshal(output)
jsonStr = string(bs)
return
}
func (mux *Mux) NewEntry() *Entry {
entry := &Entry{}
mux.entries = append(mux.entries, entry)
return entry
}
func (mux *Mux) Handle(exFunc string, handler LambdaUDFHandler) *Entry {
return mux.NewEntry().ExternalFunction(exFunc).Handler(handler)
}
func (mux *Mux) HandleFunc(exFunc string, f func(context.Context, [][]interface{}) ([]interface{}, error)) *Entry {
return mux.NewEntry().ExternalFunction(exFunc).HandlerFunc(f)
}
func (mux *Mux) HandleRow(exFunc string, handler LambdaUDFRowHandler) *Entry {
return mux.NewEntry().ExternalFunction(exFunc).Handler(ParallelRowProcessHandler{
RowHandler: handler,
})
}
func (mux *Mux) HandleRowFunc(exFunc string, f func(context.Context, []interface{}) (interface{}, error)) *Entry {
return mux.NewEntry().ExternalFunction(exFunc).Handler(ParallelRowProcessHandler{
RowHandler: LambdaUDFRowHandlerFunc(f),
})
}