Debian Codegraph @

hugolib/versions/versions.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package versions
15
16 import (
17 "errors"
18 "fmt"
19 "iter"
20 "sort"
21
22 "github.com/gohugoio/hugo/common/paths"
23 "github.com/gohugoio/hugo/common/predicate"
24 "github.com/gohugoio/hugo/common/version"
25 "github.com/gohugoio/hugo/config"
26 "github.com/gohugoio/hugo/hugolib/sitesmatrix"
27 "github.com/mitchellh/mapstructure"
28 )
29
30 var _ sitesmatrix.DimensionInfo = (*versionWrapper)(nil)
31
32 type VersionConfig struct {
33 // The weight of the version.
34 // Used to determine the order of the versions.
35 // If zero, we use the version name to sort.
36 Weight int
37 }
38
39 type Version interface {
40 Name() string
41 }
42
43 type Versions []Version
44
45 type versionWrapper struct {
46 v VersionInternal
47 }
48
49 func (v versionWrapper) Name() string {
50 return v.v.Name
51 }
52
53 func (v versionWrapper) IsDefault() bool {
54 return v.v.Default
55 }
56
57 func NewVersion(v VersionInternal) Version {
58 return versionWrapper{v: v}
59 }
60
61 var _ Version = (*versionWrapper)(nil)
62
63 type VersionInternal struct {
64 // Name of the version.
65 // This is the key from the config.
66 Name string
67
68 // Parsed version of Name.
69 parsedVersion version.Version
70
71 // Whether this version is the default version.
72 // This will be by default rendered in the root.
73 // There can only be one default version.
74 Default bool
75
76 VersionConfig
77 }
78
79 type VersionsInternal struct {
80 versionConfigs map[string]VersionConfig
81
82 Sorted []VersionInternal
83 }
84
85 func (r VersionsInternal) Len() int {
86 return len(r.Sorted)
87 }
88
89 func (r VersionsInternal) IndexDefault() int {
90 for i, version := range r.Sorted {
91 if version.Default {
92 return i
93 }
94 }
95 panic("no default version found")
96 }
97
98 func (r VersionsInternal) ResolveName(i int) string {
99 if i < 0 || i >= len(r.Sorted) {
100 panic(fmt.Sprintf("index %d out of range for versions", i))
101 }
102 return r.Sorted[i].Name
103 }
104
105 func (r VersionsInternal) ResolveIndex(name string) int {
106 for i, version := range r.Sorted {
107 if version.Name == name {
108 return i
109 }
110 }
111 // Compare against parsed versions as well.
112 v, err := version.ParseVersion(name)
113 if err == nil {
114 for i, version := range r.Sorted {
115 if version.parsedVersion.Compare(v) == 0 {
116 return i
117 }
118 }
119 }
120 return -1
121 }
122
123 // IndexMatch returns an iterator for the versions that match the filter.
124 func (r VersionsInternal) IndexMatch(match predicate.P[predicate.IndexString]) (iter.Seq[int], error) {
125 return func(yield func(i int) bool) {
126 for i, version := range r.Sorted {
127 if match(predicate.IndexString{Index: i, String: version.Name}) {
128 if !yield(i) {
129 return
130 }
131 }
132 }
133 }, nil
134 }
135
136 // ForEachIndex returns an iterator for the indices of the versions.
137 func (r VersionsInternal) ForEachIndex() iter.Seq[int] {
138 return func(yield func(i int) bool) {
139 for i := range r.Sorted {
140 if !yield(i) {
141 return
142 }
143 }
144 }
145 }
146
147 const defaultContentVersionFallback = "v1.0.0"
148
149 func (r *VersionsInternal) init(defaultContentVersion string) error {
150 if r.versionConfigs == nil {
151 r.versionConfigs = make(map[string]VersionConfig)
152 }
153 defaultContentVersionProvided := defaultContentVersion != ""
154 if len(r.versionConfigs) == 0 {
155 if defaultContentVersion == "" {
156 defaultContentVersion = defaultContentVersionFallback
157 }
158 // Add a default version.
159 r.versionConfigs[defaultContentVersion] = VersionConfig{}
160 }
161
162 var defaultSeen bool
163 for k, v := range r.versionConfigs {
164 if k == "" {
165 return errors.New("version name cannot be empty")
166 }
167 if err := paths.ValidateIdentifier(k); err != nil {
168 return fmt.Errorf("version name %q is invalid: %s", k, err)
169 }
170
171 var isDefault bool
172 if k == defaultContentVersion {
173 isDefault = true
174 defaultSeen = true
175 }
176
177 // Ignore error, this may not be a semver version.
178 parsedVersion, _ := version.ParseVersion(k)
179
180 r.Sorted = append(r.Sorted, VersionInternal{Name: k, parsedVersion: parsedVersion, Default: isDefault, VersionConfig: v})
181 }
182
183 // Sort by weight if set, then semver descending.
184 sort.SliceStable(r.Sorted, func(i, j int) bool {
185 ri, rj := r.Sorted[i], r.Sorted[j]
186 if ri.Weight == rj.Weight {
187 return ri.parsedVersion.Compare(rj.parsedVersion) < 0
188 }
189 if rj.Weight == 0 {
190 return true
191 }
192 if ri.Weight == 0 {
193 return false
194 }
195 return ri.Weight < rj.Weight
196 })
197
198 if !defaultSeen {
199 if defaultContentVersionProvided {
200 return fmt.Errorf("the configured defaultContentVersion %q does not exist", defaultContentVersion)
201 }
202 // If no default version is set, we set the first one.
203 first := r.Sorted[0]
204 first.Default = true
205 r.versionConfigs[first.Name] = first.VersionConfig
206 r.Sorted[0] = first
207 }
208
209 return nil
210 }
211
212 func (r VersionsInternal) Has(version string) bool {
213 _, found := r.versionConfigs[version]
214 return found
215 }
216
217 func DecodeConfig(defaultContentVersion string, m map[string]any) (*config.ConfigNamespace[map[string]VersionConfig, VersionsInternal], error) {
218 return config.DecodeNamespace[map[string]VersionConfig](m, func(in any) (VersionsInternal, any, error) {
219 var versions VersionsInternal
220 var conf map[string]VersionConfig
221 if err := mapstructure.Decode(m, &conf); err != nil {
222 return versions, nil, err
223 }
224 versions.versionConfigs = conf
225 if err := versions.init(defaultContentVersion); err != nil {
226 return versions, nil, err
227 }
228 return versions, versions.versionConfigs, nil
229 })
230 }