Skip to content

Commit 22d0c17

Browse files
committed
docshelper: Fix some YAML serialization issues with sites matrix configuration
Fixes #14132
1 parent 25c7c18 commit 22d0c17

File tree

7 files changed

+92
-9
lines changed

7 files changed

+92
-9
lines changed

‎commands/gen.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ url: %s
249249
return err
250250
}
251251
defer f.Close()
252-
yamlEnc := yaml.NewEncoder(f, yaml.AutoInt())
252+
yamlEnc := yaml.NewEncoder(f, yaml.UseSingleQuote(true), yaml.AutoInt())
253253
if err := yamlEnc.Encode(m); err != nil {
254254
return err
255255
}

‎hugolib/roles/roles.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func DecodeConfig(defaultContentRole string, m map[string]any) (*config.ConfigNa
206206
if defaultContentRole, err = roles.init(defaultContentRole); err != nil {
207207
return roles, nil, err
208208
}
209-
return roles, nil, nil
209+
return roles, roles.roleConfigs, nil
210210
})
211211

212212
return v, defaultContentRole, err

‎hugolib/sitesmatrix/sitematrix_integration_test.go‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package sitesmatrix_test
1515

1616
import (
17+
"encoding/json"
1718
"fmt"
1819
"strings"
1920
"testing"
@@ -1100,6 +1101,87 @@ title: "P%d"
11001101
return b
11011102
}
11021103

1104+
// See #14132. We recently reworked the config structs for languages, versions, and roles,
1105+
// which made them incomplete when generating the docshelper YAML file.
1106+
// Add a test here to ensure we don't regress.
1107+
func TestUnmarshalSitesMatrixConfig(t *testing.T) {
1108+
t.Parallel()
1109+
1110+
files := `
1111+
-- hugo.toml --
1112+
defaultContentLanguage = "en"
1113+
defaultContentLanguageInSubDir = true
1114+
defaultCOntentVersionInSubDir = true
1115+
defaultContentVersion = "v1.0.0"
1116+
defaultContentRole = "guest"
1117+
defaultContentRoleInSubDir = true
1118+
1119+
[moule.mounts]
1120+
source = 'content'
1121+
target = 'content'
1122+
1123+
1124+
[languages]
1125+
[languages.en]
1126+
1127+
[versions]
1128+
[versions."v1.0.0"]
1129+
1130+
[roles]
1131+
[roles.guest]
1132+
1133+
`
1134+
1135+
b := hugolib.Test(t, files)
1136+
1137+
toJSONAndMap := func(v any) map[string]any {
1138+
bb, err := json.Marshal(v)
1139+
b.Assert(err, qt.IsNil)
1140+
var m map[string]any
1141+
err = json.Unmarshal(bb, &m)
1142+
b.Assert(err, qt.IsNil)
1143+
return m
1144+
}
1145+
1146+
conf := b.H.Configs.Base
1147+
1148+
b.Assert(toJSONAndMap(conf.Languages), qt.DeepEquals,
1149+
map[string]any{
1150+
"en": map[string]any{
1151+
"Disabled": bool(false),
1152+
"LanguageCode": "",
1153+
"LanguageDirection": "",
1154+
"LanguageName": "",
1155+
"Title": "",
1156+
"Weight": float64(0),
1157+
},
1158+
})
1159+
1160+
b.Assert(toJSONAndMap(conf.Versions), qt.DeepEquals, map[string]any{
1161+
"v1.0.0": map[string]any{
1162+
"Weight": float64(0),
1163+
},
1164+
})
1165+
1166+
b.Assert(toJSONAndMap(conf.Roles), qt.DeepEquals, map[string]any{
1167+
"guest": map[string]any{
1168+
"Weight": float64(0),
1169+
},
1170+
})
1171+
1172+
firstMount := conf.Module.Mounts[0]
1173+
b.Assert(toJSONAndMap(firstMount.Sites.Matrix), qt.DeepEquals, map[string]any{
1174+
"languages": nil,
1175+
"versions": nil,
1176+
"roles": nil,
1177+
})
1178+
b.Assert(toJSONAndMap(firstMount.Sites.Complements), qt.DeepEquals, map[string]any{
1179+
"languages": nil,
1180+
"versions": nil,
1181+
"roles": nil,
1182+
})
1183+
}
1184+
11031185
func TestSitesMatrixContentBenchmark(t *testing.T) {
11041186
const numPages = 3
11051187
b := newSitesMatrixContentBenchmarkBuilder(t, numPages, false, true)

‎hugolib/sitesmatrix/vectorstores.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,9 @@ type IntSetsConfig struct {
813813
// Sites holds configuration about which sites a file/content/page/resource belongs to.
814814
type Sites struct {
815815
// Matrix defines what sites to build this content for.
816-
Matrix StringSlices `mapstructure:"matrix" json:"matrix,omitzero"`
816+
Matrix StringSlices `mapstructure:"matrix" json:"matrix"`
817817
// Complements defines what sites to complement with this content.
818-
Complements StringSlices `mapstructure:"complements" json:"complements,omitzero"`
818+
Complements StringSlices `mapstructure:"complements" json:"complements"`
819819
}
820820

821821
func (s *Sites) Equal(other Sites) bool {

‎hugolib/versions/versions.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,6 @@ func DecodeConfig(defaultContentVersion string, m map[string]any) (*config.Confi
206206
if err := versions.init(defaultContentVersion); err != nil {
207207
return versions, nil, err
208208
}
209-
return versions, nil, nil
209+
return versions, versions.versionConfigs, nil
210210
})
211211
}

‎langs/config.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func DecodeConfig(defaultContentLanguage string, disabledLanguages []string, m m
216216
if defaultContentLanguage, err = languages.init(defaultContentLanguage, disabledLanguages); err != nil {
217217
return languages, nil, err
218218
}
219-
return languages, nil, nil
219+
return languages, languages.LanguageConfigs, nil
220220
})
221221

222222
return v, defaultContentLanguage, err

‎modules/config.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ type Mount struct {
418418
Target string
419419

420420
// Any file in this mount will be associated with this language.
421-
Lang string
421+
// Deprecated, use Sites instead.
422+
Lang string `json:"-"`
422423

423424
// Sites defines which sites this mount applies to.
424425
Sites sitesmatrix.Sites
@@ -429,11 +430,11 @@ type Mount struct {
429430

430431
// Include only files matching the given Glob patterns (string or slice).
431432
// Deprecated, use Files instead.
432-
IncludeFiles any
433+
IncludeFiles any `json:"-"`
433434

434435
// Exclude all files matching the given Glob patterns (string or slice).
435436
// Deprecated, use Files instead.
436-
ExcludeFiles any
437+
ExcludeFiles any `json:"-"`
437438

438439
// Disable watching in watch mode for this mount.
439440
DisableWatch bool

0 commit comments

Comments
 (0)