]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
commands: Handle floats without decimals in hugo config
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 7 Aug 2023 17:56:02 +0000 (19:56 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 8 Aug 2023 07:13:17 +0000 (09:13 +0200)
Updates #11345

commands/config.go
common/maps/maps.go

index 9bf1c0961b5f97978f70ea8d2c6dfe4e17099b48..63ee4f7c8d5ce9468ac82097183ef39dbf6c9c88 100644 (file)
@@ -23,6 +23,7 @@ import (
        "time"
 
        "github.com/bep/simplecobra"
+       "github.com/gohugoio/hugo/common/maps"
        "github.com/gohugoio/hugo/config/allconfig"
        "github.com/gohugoio/hugo/modules"
        "github.com/gohugoio/hugo/parser"
@@ -92,6 +93,7 @@ func (c *configCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
                if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
                        return err
                }
+               maps.ConvertFloat64WithNoDecimalsToInt(m)
                switch format {
                case "yaml":
                        return parser.InterfaceToConfig(m, metadecoders.YAML, os.Stdout)
index 6aefde927fb0dbd983599755a23016a7c0977dd1..f0fd3d5ce9cb8488166df68ed816b1aa2c405ca4 100644 (file)
@@ -210,3 +210,28 @@ func (r KeyRenamer) renamePath(parentKeyPath string, m map[string]any) {
                }
        }
 }
+
+// ConvertFloat64WithNoDecimalsToInt converts float64 values with no decimals to int recursively.
+func ConvertFloat64WithNoDecimalsToInt(m map[string]any) {
+       for k, v := range m {
+               switch vv := v.(type) {
+               case float64:
+                       if v == float64(int64(vv)) {
+                               m[k] = int64(vv)
+                       }
+               case map[string]any:
+                       ConvertFloat64WithNoDecimalsToInt(vv)
+               case []any:
+                       for i, vvv := range vv {
+                               switch vvvv := vvv.(type) {
+                               case float64:
+                                       if vvv == float64(int64(vvvv)) {
+                                               vv[i] = int64(vvvv)
+                                       }
+                               case map[string]any:
+                                       ConvertFloat64WithNoDecimalsToInt(vvvv)
+                               }
+                       }
+               }
+       }
+}