Skip to content

Commit

Permalink
Merge pull request #160 from Bitspark/fix-http-bug
Browse files Browse the repository at this point in the history
Add 'valid' port to JSONRead
  • Loading branch information
td5r authored Aug 10, 2018
2 parents b3a0689 + 154a6fb commit 6a8e5c2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/api/run_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func ConstructHttpEndpoint(env *Environ, port int, operator string, gens core.Ge
}
httpDef.InstanceDefs = append(httpDef.InstanceDefs, unpackerIns)
httpDef.Connections["httpServer.handler)body"] = []string{"(unpacker"}
httpDef.Connections["unpacker)"] = []string{"(operator"}
httpDef.Connections["unpacker)item"] = []string{"(operator"}
}

if outDef.Equals(elem.HTTP_RESPONSE_DEF) {
Expand Down
26 changes: 22 additions & 4 deletions pkg/elem/encoding_json_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ var encodingJSONReadCfg = &builtinConfig{
Type: "binary",
},
Out: core.TypeDef{
Type: "generic",
Generic: "itemType",
Type: "map",
Map: map[string]*core.TypeDef{
"valid": {
Type: "boolean",
},
"item": {
Type: "generic",
Generic: "itemType",
},
},
},
},
},
Expand All @@ -24,6 +32,8 @@ var encodingJSONReadCfg = &builtinConfig{
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
def, _ := op.Define()
itemDef := def.ServiceDefs[core.MAIN_SERVICE].Out.Map["item"]
for !op.CheckStop() {
i := in.Pull()
if core.IsMarker(i) {
Expand All @@ -33,11 +43,19 @@ var encodingJSONReadCfg = &builtinConfig{
var obj interface{}
err := json.Unmarshal([]byte(i.(utils.Binary)), &obj)
if err != nil {
out.Push(nil)
out.Map("item").Push(nil)
out.Map("valid").Push(false)
continue
}
obj = utils.CleanValue(obj)
out.Push(obj) // TODO: Make this safer
err = itemDef.VerifyData(obj)
if err == nil {
out.Map("item").Push(obj)
out.Map("valid").Push(true)
} else {
out.Map("item").Push(nil)
out.Map("valid").Push(false)
}
}
},
}
37 changes: 35 additions & 2 deletions pkg/elem/encoding_json_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,39 @@ func Test_JsonRead__String(t *testing.T) {
o.Main().Out().Bufferize()
o.Start()
o.Main().In().Push(utils.Binary("\"test\""))
a.PortPushes("test", o.Main().Out())
a.PortPushes("test", o.Main().Out().Map("item"))
a.PortPushes(true, o.Main().Out().Map("valid"))
}

func Test_JsonRead__Invalid(t *testing.T) {
a := assertions.New(t)

o, err := buildOperator(
core.InstanceDef{
Operator: "slang.encoding.JSONRead",
Generics: map[string]*core.TypeDef{
"itemType": {
Type: "map",
Map: map[string]*core.TypeDef{
"a": {
Type: "number",
},
"b": {
Type: "boolean",
},
},
},
},
},
)
require.NoError(t, err)

o.Main().Out().Bufferize()
o.Start()
o.Main().In().Push(utils.Binary("\"test\""))
a.PortPushes(nil, o.Main().Out().Map("item").Map("a"))
a.PortPushes(nil, o.Main().Out().Map("item").Map("b"))
a.PortPushes(false, o.Main().Out().Map("valid"))
}

func Test_JsonRead__Complex(t *testing.T) {
Expand Down Expand Up @@ -65,5 +97,6 @@ func Test_JsonRead__Complex(t *testing.T) {
o.Main().Out().Bufferize()
o.Start()
o.Main().In().Push(utils.Binary("{\"a\":[1,2,3],\"b\":true}"))
a.PortPushes(map[string]interface{}{"a": []interface{}{1.0, 2.0, 3.0}, "b": true}, o.Main().Out())
a.PortPushes(map[string]interface{}{"a": []interface{}{1.0, 2.0, 3.0}, "b": true}, o.Main().Out().Map("item"))
a.PortPushes(true, o.Main().Out().Map("valid"))
}

0 comments on commit 6a8e5c2

Please sign in to comment.