Skip to content

Commit 515f7b3

Browse files
authored
Fix and optimize the handling logic of Inputs and Outputs (#35)
Signed-off-by: laminar <fangtian@kubesphere.io>
1 parent bd3cb1e commit 515f7b3

3 files changed

Lines changed: 43 additions & 16 deletions

File tree

context/context.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"net/http"
99
"os"
10-
"reflect"
1110
"strconv"
1211
"strings"
1312
"sync"
@@ -333,7 +332,7 @@ func NewResponseWriterWrapper(w http.ResponseWriter, statusCode int) *ResponseWr
333332
}
334333

335334
func (ctx *FunctionContext) Send(outputName string, data []byte) ([]byte, error) {
336-
if ctx.HasOutputs() {
335+
if !ctx.HasOutputs() {
337336
return nil, errors.New("no output")
338337
}
339338

@@ -384,19 +383,17 @@ func (ctx *FunctionContext) Send(outputName string, data []byte) ([]byte, error)
384383
}
385384

386385
func (ctx *FunctionContext) HasInputs() bool {
387-
nilInputs := map[string]*Input{}
388-
if reflect.DeepEqual(ctx.Inputs, nilInputs) {
389-
return false
386+
if len(ctx.GetInputs()) > 0 {
387+
return true
390388
}
391-
return true
389+
return false
392390
}
393391

394392
func (ctx *FunctionContext) HasOutputs() bool {
395-
nilOutputs := map[string]*Output{}
396-
if reflect.DeepEqual(ctx.Outputs, nilOutputs) {
397-
return false
393+
if len(ctx.GetOutputs()) > 0 {
394+
return true
398395
}
399-
return true
396+
return false
400397
}
401398

402399
func (ctx *FunctionContext) ReturnOnSuccess() Out {
@@ -694,17 +691,17 @@ func parseContext() (*FunctionContext, error) {
694691
ctx.Event = &EventRequest{}
695692
ctx.SyncRequest = &SyncRequest{}
696693

697-
if !ctx.HasInputs() {
698-
for name, in := range ctx.Inputs {
694+
if ctx.HasInputs() {
695+
for name, in := range ctx.GetInputs() {
699696
if _, err := getBuildingBlockType(in.ComponentType); err != nil {
700697
klog.Errorf("failed to get building block type for input %s: %v", name, err)
701698
return nil, err
702699
}
703700
}
704701
}
705702

706-
if !ctx.HasOutputs() {
707-
for name, out := range ctx.Outputs {
703+
if ctx.HasOutputs() {
704+
for name, out := range ctx.GetOutputs() {
708705
if _, err := getBuildingBlockType(out.ComponentType); err != nil {
709706
klog.Errorf("failed to get building block type for output %s: %v", name, err)
710707
return nil, err

context/context_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,37 @@ func TestParseFunctionContext(t *testing.T) {
208208
t.Fatal("Error set function context env")
209209
}
210210

211+
// test `inputs`, `outputs` fields
212+
if err := os.Setenv(FunctionContextEnvName, funcCtx); err == nil {
213+
if ctx, err := GetRuntimeContext(); err != nil {
214+
t.Fatalf("Error parse function context: %s", err.Error())
215+
} else {
216+
// test `inputs`
217+
if !ctx.HasInputs() || len(ctx.GetInputs()) != 2 {
218+
t.Fatal("Error parse function context: failed to parse inputs")
219+
}
220+
if cron, exist := ctx.GetInputs()["cron"]; exist {
221+
if cron.Uri == "cron_input" && cron.ComponentType == "bindings.cron" {
222+
223+
} else {
224+
t.Fatal("Error parse function context: failed to parse input cron")
225+
}
226+
}
227+
228+
// test `outputs`
229+
if !ctx.HasOutputs() || len(ctx.GetOutputs()) != 3 {
230+
t.Fatal("Error parse function context: failed to parse outputs")
231+
}
232+
if echo, exist := ctx.GetOutputs()["echo"]; exist {
233+
if path, exist := echo.Metadata["path"]; exist && echo.Uri == "echo" && path == "echo" {
234+
235+
} else {
236+
t.Fatal("Error parse function context: failed to parse output echo")
237+
}
238+
}
239+
}
240+
}
241+
211242
// test `prePlugins`, `postPlugins`, `pluginsTracing` fields
212243
if err := os.Setenv(FunctionContextEnvName, funcCtxWithPlugins); err == nil {
213244
if ctx, err := GetRuntimeContext(); err != nil {

context/innerevent.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package context
33
import (
44
"encoding/json"
55
"fmt"
6-
"reflect"
76
"sync"
87
"time"
98

@@ -167,7 +166,7 @@ func (inner *innerEvent) Clone(event *cloudevents.Event) {
167166
}
168167

169168
func (inner *innerEvent) save() {
170-
if inner.cloudevent == nil || (inner.data != nil && reflect.DeepEqual(inner.data.Metadata, map[string]string{}) && inner.data.UserData == nil) {
169+
if inner.cloudevent == nil || (inner.data != nil && len(inner.data.Metadata) > 0 && inner.data.UserData == nil) {
171170
fmt.Println(inner.data.UserData)
172171
return
173172
}

0 commit comments

Comments
 (0)