Skip to content

Commit e08278d

Browse files
committed
Expand the numeric conversions to template funcs/methods
So the example mentioned in #14079 also works for TOML and JSON front matter. See #14079
1 parent df4f80d commit e08278d

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

‎common/hreflect/helpers.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ func IsContextType(tp reflect.Type) bool {
315315
// This is currently only implemented for int kinds,
316316
// added to handle the move to a new YAML library which produces uint64 for unsigned integers.
317317
// We can expand on this later if needed.
318+
// This conversion is lossless.
318319
// See Issue 14079.
319320
func ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) {
320321
if IsInt(typ.Kind()) {
@@ -333,6 +334,21 @@ func ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool
333334
}
334335
return val.Convert(typ), true
335336
}
337+
if IsFloat(val.Kind()) {
338+
f := val.Float()
339+
if f < float64(math.MinInt64) || f > float64(math.MaxInt64) {
340+
return reflect.Value{}, false
341+
}
342+
i := int64(f)
343+
if typ.OverflowInt(i) {
344+
return reflect.Value{}, false
345+
}
346+
// Check for lossless conversion.
347+
if float64(i) != f {
348+
return reflect.Value{}, false
349+
}
350+
return val.Convert(typ), true
351+
}
336352
}
337353
return reflect.Value{}, false
338354
}

‎common/hreflect/helpers_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,33 @@ func TestCastIfPossible(t *testing.T) {
231231
ok: true,
232232
expected: int(math.MaxInt16),
233233
},
234+
// From float to int.
235+
{
236+
name: "float64(1.5) to int",
237+
value: float64(1.5),
238+
typ: int(0),
239+
ok: false, // loss of precision
240+
},
241+
{
242+
name: "float64(1.0) to int",
243+
value: float64(1.0),
244+
typ: int(0),
245+
ok: true,
246+
expected: int(1),
247+
},
248+
{
249+
name: "float64(math.MaxFloat64) to int16",
250+
value: float64(math.MaxFloat64),
251+
typ: int16(0),
252+
ok: false, // overflow
253+
},
254+
{
255+
name: "float64(32767) to int16",
256+
value: float64(32767),
257+
typ: int16(0),
258+
ok: true,
259+
expected: int16(32767),
260+
},
234261
} {
235262

236263
v, ok := ConvertIfPossible(reflect.ValueOf(test.value), reflect.TypeOf(test.typ))

‎tpl/templates/templates_integration_test.go‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,53 @@ myinteger: 2
327327
-- layouts/all.html --
328328
{{ $date := "2023-10-15T13:18:50-07:00" | time }}
329329
{{ $mydata := resources.Get "mydata.yaml" | transform.Unmarshal }}
330-
date: {{ $date | time.Format "2006-01-06" }}|
331-
date+2y: {{ $date.AddDate $mydata.myinteger 0 0 | time.Format "2006-01-06" }}|
330+
date: {{ $date | time.Format "2006-01-02" }}|
331+
date+2y: {{ $date.AddDate $mydata.myinteger 0 0 | time.Format "2006-01-02" }}|
332332
`
333333

334334
b := hugolib.Test(t, files)
335335

336-
b.AssertFileContent("public/index.html", "date: 2023-10-23|", "date+2y: 2025-10-25|")
336+
b.AssertFileContent("public/index.html", "date: 2023-10-15", "date+2y: 2025-10-15")
337+
}
338+
339+
func TestTOMLAddDateIssue14079(t *testing.T) {
340+
t.Parallel()
341+
342+
files := `
343+
-- hugo.toml --
344+
disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "RSS"]
345+
-- assets/mydata.toml --
346+
myinteger = 2
347+
-- layouts/all.html --
348+
{{ $date := "2023-10-15T13:18:50-07:00" | time }}
349+
{{ $mydata := resources.Get "mydata.toml" | transform.Unmarshal }}
350+
date: {{ $date | time.Format "2006-01-02" }}|
351+
date+2y: {{ $date.AddDate $mydata.myinteger 0 0 | time.Format "2006-01-02" }}|
352+
`
353+
354+
b := hugolib.Test(t, files)
355+
356+
b.AssertFileContent("public/index.html", "date: 2023-10-15", "date+2y: 2025-10-15")
357+
}
358+
359+
func TestJSONAddDateIssue14079(t *testing.T) {
360+
t.Parallel()
361+
362+
files := `
363+
-- hugo.toml --
364+
disableKinds = ["page", "section", "taxonomy", "term", "sitemap", "RSS"]
365+
-- assets/mydata.json --
366+
{
367+
"myinteger": 2
368+
}
369+
-- layouts/all.html --
370+
{{ $date := "2023-10-15T13:18:50-07:00" | time }}
371+
{{ $mydata := resources.Get "mydata.json" | transform.Unmarshal }}
372+
date: {{ $date | time.Format "2006-01-02" }}|
373+
date+2y: {{ $date.AddDate $mydata.myinteger 0 0 | time.Format "2006-01-02" }}|
374+
`
375+
376+
b := hugolib.Test(t, files)
377+
378+
b.AssertFileContent("public/index.html", "date: 2023-10-15", "date+2y: 2025-10-15")
337379
}

0 commit comments

Comments
 (0)