Skip to content

Commit

Permalink
Merge pull request #40 from imiskolee/master
Browse files Browse the repository at this point in the history
1. support json.Number for parse into number types
  • Loading branch information
osteele authored Nov 7, 2020
2 parents 26998e1 + dd12222 commit 3b040cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package liquid

import (
"bytes"
"encoding/json"
"fmt"
"io"
"testing"
Expand Down Expand Up @@ -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 }
Expand Down
14 changes: 14 additions & 0 deletions values/convert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package values

import (
"encoding/json"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 3b040cc

Please sign in to comment.