From dd12222409513087aa37671fc0099c9362993eba Mon Sep 17 00:00:00 2001 From: misko Date: Fri, 20 Dec 2019 19:51:52 +0800 Subject: [PATCH] 1. fix --- engine_test.go | 6 ++++-- values/convert.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/engine_test.go b/engine_test.go index 4a56258..c7cc367 100644 --- a/engine_test.go +++ b/engine_test.go @@ -2,6 +2,7 @@ package liquid import ( "bytes" + "encoding/json" "fmt" "io" "testing" @@ -42,13 +43,14 @@ func TestEngine_ParseAndRenderString_ptr_to_hash(t *testing.T) { params := map[string]interface{}{ "message": &map[string]interface{}{ "Text": "hello", + "jsonNumber" : json.Number("123"), }, } engine := NewEngine() - template := "{{ message.Text }}" + template := "{{ message.Text }} {{message.jsonNumber}}" str, err := engine.ParseAndRenderString(template, params) require.NoError(t, err) - require.Equal(t, "hello", str) + require.Equal(t, "hello 123", str) } type testStruct struct{ Text string } diff --git a/values/convert.go b/values/convert.go index 4962c22..fef6953 100644 --- a/values/convert.go +++ b/values/convert.go @@ -1,6 +1,7 @@ package values import ( + "encoding/json" "fmt" "reflect" "strconv" @@ -44,6 +45,13 @@ func convertValueToInt(value interface{}, typ reflect.Type) (int64, error) { return 0, conversionError("", value, typ) } return v, nil + case json.Number: + v, err := strconv.ParseInt(value.String(), 10, 64) + if err != nil { + return 0, conversionError("", value, typ) + } + return v, nil + } return 0, conversionError("", value, typ) } @@ -57,6 +65,12 @@ func convertValueToFloat(value interface{}, typ reflect.Type) (float64, error) { return 0, conversionError("", value, typ) } return v, nil + case json.Number: + v, err := strconv.ParseFloat(value.String(), 64) + if err != nil { + return 0, conversionError("", value, typ) + } + return v, nil } return 0, conversionError("", value, typ) }