diff --git a/tags/iteration_tags_test.go b/tags/iteration_tags_test.go index 78aa555..b91dc6c 100644 --- a/tags/iteration_tags_test.go +++ b/tags/iteration_tags_test.go @@ -85,6 +85,7 @@ var iterationTests = []struct{ in, expected string }{ // range {`{% for i in (3 .. 5) %}{{i}}.{% endfor %}`, "3.4.5."}, {`{% for i in (3..5) %}{{i}}.{% endfor %}`, "3.4.5."}, + {`{% assign l = (3..5) %}{% for i in l %}{{i}}.{% endfor %}`, "3.4.5."}, // tablerow {`{% tablerow product in products %}{{ product }}{% endtablerow %}`, diff --git a/tags/standard_tags_test.go b/tags/standard_tags_test.go index a0cbf6c..510577e 100644 --- a/tags/standard_tags_test.go +++ b/tags/standard_tags_test.go @@ -24,6 +24,7 @@ var tagTests = []struct{ in, expected string }{ // variable tags {`{% assign av = 1 %}{{ av }}`, "1"}, {`{% assign av = obj.a %}{{ av }}`, "1"}, + {`{% assign av = (1..5) %}{{ av }}`, "{1 5}"}, {`{% capture x %}captured{% endcapture %}{{ x }}`, "captured"}, // TODO research whether Liquid requires matching interior tags diff --git a/values/convert.go b/values/convert.go index fef6953..a688de8 100644 --- a/values/convert.go +++ b/values/convert.go @@ -202,6 +202,8 @@ func Convert(value interface{}, typ reflect.Type) (interface{}, error) { // noli result = reflect.Append(result, ev.Convert(et)) } return result.Interface(), nil + } else if r, ok := value.(Range); ok { + return r.AsArray(), nil } switch rv.Kind() { case reflect.Array, reflect.Slice: diff --git a/values/convert_test.go b/values/convert_test.go index 30b0f09..fa58dc4 100644 --- a/values/convert_test.go +++ b/values/convert_test.go @@ -84,6 +84,8 @@ var convertTests = []struct { {yaml.MapSlice{{Key: "a", Value: 1}}, map[string]string{"a": "1"}}, {yaml.MapSlice{{Key: "a", Value: nil}}, map[string]interface{}{"a": nil}}, {yaml.MapSlice{{Key: nil, Value: 1}}, map[interface{}]string{nil: "1"}}, + {Range{1, 5}, []interface{}{1, 2, 3, 4, 5}}, + {Range{0, 0}, []interface{}{0}}, // {"March 14, 2016", time.Now(), timeMustParse("2016-03-14T00:00:00Z")}, {redConvertible{}, "red"}, } diff --git a/values/range.go b/values/range.go index bb8fe2c..1fa5a7e 100644 --- a/values/range.go +++ b/values/range.go @@ -15,3 +15,12 @@ func (r Range) Len() int { return r.e + 1 - r.b } // Index is in the iteration interface func (r Range) Index(i int) interface{} { return r.b + i } + +// AsArray converts the range into an array. +func (r Range) AsArray() []interface{} { + a := make([]interface{}, 0, r.Len()) + for i := r.b; i <= r.e; i++ { + a = append(a, i) + } + return a +}