From: Bjørn Erik Pedersen Date: Sun, 25 Jan 2026 10:49:08 +0000 (+0100) Subject: Make docs helper maxAge JSON output user friendly X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=5a64551a070d261923b93a1795fa7cdd38a439a0;p=brevno-suite%2Fhugo Make docs helper maxAge JSON output user friendly --- diff --git a/cache/filecache/filecache_config.go b/cache/filecache/filecache_config.go index 025b62b7f..96ea9611a 100644 --- a/cache/filecache/filecache_config.go +++ b/cache/filecache/filecache_config.go @@ -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] diff --git a/cache/filecache/filecache_config_test.go b/cache/filecache/filecache_config_test.go index 8da26fa18..ecd6f366b 100644 --- a/cache/filecache/filecache_config_test.go +++ b/cache/filecache/filecache_config_test.go @@ -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`) +}