Skip to content

Commit

Permalink
auto-convert []byte to string when concatenating
Browse files Browse the repository at this point in the history
i.e. allow <string> + <[]byte>
  • Loading branch information
sauerbraten committed Jul 20, 2020
1 parent 4b52e03 commit bdff0f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,6 @@ func (st *Runtime) evalAdditiveExpression(node *AdditiveExprNode) reflect.Value
right := st.evalPrimaryExpressionGroup(node.Right)
kind := right.Kind()
// todo: optimize
// todo:
if isInt(kind) {
if isAdditive {
return reflect.ValueOf(+right.Int())
Expand All @@ -1009,7 +1008,7 @@ func (st *Runtime) evalAdditiveExpression(node *AdditiveExprNode) reflect.Value
return reflect.ValueOf(-right.Float())
}
}
node.Left.errorf("additive expression: right side %s (%s) is not a numeric value (left is nil)", node.Right, getTypeString(right))
node.Left.errorf("additive expression: right side %s (%s) is not a numeric value (no left side)", node.Right, getTypeString(right))
}

left, right := st.evalPrimaryExpressionGroup(node.Left), st.evalPrimaryExpressionGroup(node.Right)
Expand Down Expand Up @@ -1053,11 +1052,14 @@ func (st *Runtime) evalAdditiveExpression(node *AdditiveExprNode) reflect.Value
left = reflect.ValueOf(left.Uint() - toUint(right))
}
} else if kind == reflect.String {
if isAdditive {
left = reflect.ValueOf(left.String() + fmt.Sprint(right))
} else {
if !isAdditive {
node.Right.errorf("minus signal is not allowed with strings")
}
// converts []byte (and alias types of []byte) to string
if right.Kind() == reflect.Slice && right.Type().Elem().Kind() == reflect.Uint8 {
right = right.Convert(left.Type())
}
left = reflect.ValueOf(left.String() + fmt.Sprint(right))
} else {
node.Left.errorf("additive expression: left side %s (%s) is not a numeric value", node.Left, getTypeString(left))
}
Expand Down
12 changes: 12 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func TestEvalActionNode(t *testing.T) {

RunJetTest(t, data, nil, "actionNode_AddIntString", `{{ 2+"1" }}`, "3")
RunJetTest(t, data, nil, "actionNode_AddStringInt", `{{ "1"+2 }}`, "12")
RunJetTest(t, data, nil, "actionNode_AddStringByteSlice", `{{ "{foo:"+json("asd")+"}" }}`, `{foo:"asd"}`)

RunJetTest(t, data, nil, "actionNode_NumberNegative", `{{ -5 }}`, "-5")
RunJetTest(t, data, nil, "actionNode_NumberNegative_1", `{{ 1 + -5 }}`, fmt.Sprint(1+-5))
Expand Down Expand Up @@ -314,6 +315,17 @@ func TestEvalDefaultFuncs(t *testing.T) {
)}}`,
"My_Name_Is_II",
)

var data = make(VarMap)

data.Set("m", map[string]interface{}{
"arr": []string{"foo", "bar"},
"str": "some string",
"byte_arr": []byte("a string as byte slice"),
"num": 123.45,
})
RunJetTest(t, data, nil, "DefaultFuncs_json_1", `{{json(m)}}`, `{"arr":["foo","bar"],"byte_arr":"YSBzdHJpbmcgYXMgYnl0ZSBzbGljZQ==","num":123.45,"str":"some string"}`)
RunJetTest(t, data, nil, "DefaultFuncs_json_2", `{{json(m.arr)}}`, `["foo","bar"]`)
}

func TestEvalIssetAndTernaryExpression(t *testing.T) {
Expand Down

0 comments on commit bdff0f8

Please sign in to comment.