Skip to content

Commit 7d74cd0

Browse files
committed
commands: Handle floats without decimals in hugo config
Updates gohugoio#11345
1 parent d139f30 commit 7d74cd0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

commands/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/bep/simplecobra"
26+
"github.com/gohugoio/hugo/common/maps"
2627
"github.com/gohugoio/hugo/config/allconfig"
2728
"github.com/gohugoio/hugo/modules"
2829
"github.com/gohugoio/hugo/parser"
@@ -92,6 +93,7 @@ func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
9293
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
9394
return err
9495
}
96+
maps.ConvertFloat64WithNoDecimalsToInt(m)
9597
switch format {
9698
case "yaml":
9799
return parser.InterfaceToConfig(m, metadecoders.YAML, os.Stdout)

common/maps/maps.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,28 @@ func (r KeyRenamer) renamePath(parentKeyPath string, m map[string]any) {
210210
}
211211
}
212212
}
213+
214+
// ConvertFloat64WithNoDecimalsToInt converts float64 values with no decimals to int recursively.
215+
func ConvertFloat64WithNoDecimalsToInt(m map[string]any) {
216+
for k, v := range m {
217+
switch vv := v.(type) {
218+
case float64:
219+
if v == float64(int64(vv)) {
220+
m[k] = int64(vv)
221+
}
222+
case map[string]any:
223+
ConvertFloat64WithNoDecimalsToInt(vv)
224+
case []any:
225+
for i, vvv := range vv {
226+
switch vvvv := vvv.(type) {
227+
case float64:
228+
if vvv == float64(int64(vvvv)) {
229+
vv[i] = int64(vvvv)
230+
}
231+
case map[string]any:
232+
ConvertFloat64WithNoDecimalsToInt(vvvv)
233+
}
234+
}
235+
}
236+
}
237+
}

0 commit comments

Comments
 (0)