]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Make docs helper maxAge JSON output user friendly
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 25 Jan 2026 10:49:08 +0000 (11:49 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 25 Jan 2026 12:58:28 +0000 (13:58 +0100)
cache/filecache/filecache_config.go
cache/filecache/filecache_config_test.go

index 025b62b7f3951cba91c4d803e96022b5ddc35b22..96ea9611af9f0850c667801012f82a097b24ae79 100644 (file)
@@ -15,6 +15,7 @@
 package filecache
 
 import (
+       "encoding/json"
        "errors"
        "fmt"
        "path"
@@ -112,6 +113,23 @@ type FileCacheConfig struct {
        IsResourceDir bool `json:"-"`
 }
 
+// MarshalJSON marshals FileCacheConfig to JSON with MaxAge as a human-readable string.
+func (c FileCacheConfig) MarshalJSON() ([]byte, error) {
+       var maxAge any
+       if c.MaxAge == -1 {
+               maxAge = -1
+       } else {
+               maxAge = strings.TrimSuffix(c.MaxAge.String(), "0m0s")
+       }
+       return json.Marshal(&struct {
+               MaxAge any    `json:"maxAge"`
+               Dir    string `json:"dir"`
+       }{
+               MaxAge: maxAge,
+               Dir:    c.Dir,
+       })
+}
+
 // GetJSONCache gets the file cache for getJSON.
 func (f Caches) GetJSONCache() *Cache {
        return f[CacheKeyGetJSON]
index 8da26fa18a4103a7875027d4b3fa8006b234ca82..ecd6f366baff916cd414436109b99e5e883c028d 100644 (file)
@@ -14,6 +14,7 @@
 package filecache_test
 
 import (
+       "encoding/json"
        "path/filepath"
        "runtime"
        "testing"
@@ -144,3 +145,29 @@ func TestDecodeConfigDefault(t *testing.T) {
        c.Assert(imgConfig.IsResourceDir, qt.Equals, true)
        c.Assert(jsonConfig.IsResourceDir, qt.Equals, false)
 }
+
+func TestFileCacheConfigMarshalJSON(t *testing.T) {
+       c := qt.New(t)
+
+       cfg := config.New()
+       cfg.Set("cacheDir", "/cache")
+       cfg.Set("workingDir", "/my/project")
+
+       fs := afero.NewMemMapFs()
+       decoded := testconfig.GetTestConfigs(fs, cfg).Base.Caches
+
+       moduleQueriesConfig := decoded[filecache.CacheKeyModuleQueries]
+       c.Assert(moduleQueriesConfig.MaxAge, qt.Equals, 24*time.Hour)
+
+       b, err := json.Marshal(moduleQueriesConfig)
+       c.Assert(err, qt.IsNil)
+
+       c.Assert(string(b), qt.Contains, `"maxAge":"24h"`)
+       c.Assert(string(b), qt.Not(qt.Contains), "86400000000000")
+       c.Assert(string(b), qt.Not(qt.Contains), "8.64e")
+
+       moduleQueriesConfig.MaxAge = -1
+       b, err = json.Marshal(moduleQueriesConfig)
+       c.Assert(err, qt.IsNil)
+       c.Assert(string(b), qt.Contains, `"maxAge":-1`)
+}