-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make cascade front matter order deterministic
Fixes #12594
- Loading branch information
Showing
10 changed files
with
277 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2024 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package maps | ||
|
||
import ( | ||
"github.com/gohugoio/hugo/common/hashing" | ||
) | ||
|
||
// Ordered is a map that can be iterated in the order of insertion. | ||
// Note that insertion order is not affected if a key is re-inserted into the map. | ||
// This is not thread safe. | ||
type Ordered[K comparable, T any] struct { | ||
// The keys in the order they were added. | ||
keys []K | ||
// The values. | ||
values map[K]T | ||
} | ||
|
||
// NewOrdered creates a new Ordered map. | ||
func NewOrdered[K comparable, T any]() *Ordered[K, T] { | ||
return &Ordered[K, T]{values: make(map[K]T)} | ||
} | ||
|
||
// Set sets the value for the given key. | ||
// Note that insertion order is not affected if a key is re-inserted into the map. | ||
func (m *Ordered[K, T]) Set(key K, value T) { | ||
// Check if key already exists. | ||
if _, found := m.values[key]; !found { | ||
m.keys = append(m.keys, key) | ||
} | ||
m.values[key] = value | ||
} | ||
|
||
// Get gets the value for the given key. | ||
func (m *Ordered[K, T]) Get(key K) (T, bool) { | ||
value, found := m.values[key] | ||
return value, found | ||
} | ||
|
||
// Delete deletes the value for the given key. | ||
func (m *Ordered[K, T]) Delete(key K) { | ||
delete(m.values, key) | ||
for i, k := range m.keys { | ||
if k == key { | ||
m.keys = append(m.keys[:i], m.keys[i+1:]...) | ||
break | ||
} | ||
} | ||
} | ||
|
||
// Clone creates a shallow copy of the map. | ||
// If m is nil, it returns nil. | ||
func (m *Ordered[K, T]) Clone() *Ordered[K, T] { | ||
if m == nil { | ||
return nil | ||
} | ||
clone := NewOrdered[K, T]() | ||
for _, k := range m.keys { | ||
clone.Set(k, m.values[k]) | ||
} | ||
return clone | ||
} | ||
|
||
// Keys returns the keys in the order they were added. | ||
func (m *Ordered[K, T]) Keys() []K { | ||
if m == nil { | ||
return nil | ||
} | ||
return m.keys | ||
} | ||
|
||
// Values returns the values in the order they were added. | ||
func (m *Ordered[K, T]) Values() []T { | ||
if m == nil { | ||
return nil | ||
} | ||
var values []T | ||
for _, k := range m.keys { | ||
values = append(values, m.values[k]) | ||
} | ||
return values | ||
} | ||
|
||
// Len returns the number of items in the map. | ||
func (m *Ordered[K, T]) Len() int { | ||
return len(m.keys) | ||
} | ||
|
||
// Range calls f sequentially for each key and value present in the map. | ||
// If f returns false, range stops the iteration. | ||
// TODO(bep) replace with iter.Seq2 when we bump go Go 1.24. | ||
func (m *Ordered[K, T]) Range(f func(key K, value T) bool) { | ||
if m == nil { | ||
return | ||
} | ||
for _, k := range m.keys { | ||
if !f(k, m.values[k]) { | ||
return | ||
} | ||
} | ||
} | ||
|
||
// Hash calculates a hash from the values. | ||
func (m *Ordered[K, T]) Hash() (uint64, error) { | ||
return hashing.Hash(m.values) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2024 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package maps | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestOrdered(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
m := NewOrdered[string, int]() | ||
m.Set("a", 1) | ||
m.Set("b", 2) | ||
m.Set("c", 3) | ||
|
||
c.Assert(m.Keys(), qt.DeepEquals, []string{"a", "b", "c"}) | ||
c.Assert(m.Values(), qt.DeepEquals, []int{1, 2, 3}) | ||
|
||
v, found := m.Get("b") | ||
c.Assert(found, qt.Equals, true) | ||
c.Assert(v, qt.Equals, 2) | ||
|
||
m.Set("b", 22) | ||
c.Assert(m.Keys(), qt.DeepEquals, []string{"a", "b", "c"}) | ||
c.Assert(m.Values(), qt.DeepEquals, []int{1, 22, 3}) | ||
|
||
m.Delete("b") | ||
|
||
c.Assert(m.Keys(), qt.DeepEquals, []string{"a", "c"}) | ||
c.Assert(m.Values(), qt.DeepEquals, []int{1, 3}) | ||
} | ||
|
||
func TestOrderedHash(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
m := NewOrdered[string, int]() | ||
m.Set("a", 1) | ||
m.Set("b", 2) | ||
m.Set("c", 3) | ||
|
||
h1, err := m.Hash() | ||
c.Assert(err, qt.IsNil) | ||
|
||
m.Set("d", 4) | ||
|
||
h2, err := m.Hash() | ||
c.Assert(err, qt.IsNil) | ||
|
||
c.Assert(h1, qt.Not(qt.Equals), h2) | ||
|
||
m = NewOrdered[string, int]() | ||
m.Set("b", 2) | ||
m.Set("a", 1) | ||
m.Set("c", 3) | ||
|
||
h3, err := m.Hash() | ||
c.Assert(err, qt.IsNil) | ||
// Order does not matter. | ||
c.Assert(h1, qt.Equals, h3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.