Fix corrupted frontPageContent keys during migration (#4870)

* Add NestedMap.Delete
* Migrate corrupt frontPageContent keys
This commit is contained in:
WithoutPants
2024-05-23 13:59:39 +10:00
committed by GitHub
parent 98d210f7f9
commit eb67f7f4d6
3 changed files with 110 additions and 0 deletions

View File

@@ -47,6 +47,23 @@ func (m NestedMap) Set(key string, value interface{}) {
current[fields[len(fields)-1]] = value
}
func (m NestedMap) Delete(key string) {
fields := strings.Split(key, ".")
current := m
for _, f := range fields[:len(fields)-1] {
v, ok := current[f].(map[string]interface{})
if !ok {
return
}
current = v
}
delete(current, fields[len(fields)-1])
}
// MergeMaps merges src into dest. If a key exists in both maps, the value from src is used.
func MergeMaps(dest map[string]interface{}, src map[string]interface{}) {
for k, v := range src {

View File

@@ -122,6 +122,69 @@ func TestNestedMapSet(t *testing.T) {
}
}
func TestNestedMapDelete(t *testing.T) {
tests := []struct {
name string
key string
existing NestedMap
want NestedMap
}{
{
name: "Delete non existing value",
key: "foo.bar.baa",
existing: NestedMap{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"baz": "qux",
},
},
},
want: NestedMap{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"baz": "qux",
},
},
},
},
{
name: "Delete existing value",
key: "foo.bar",
existing: NestedMap{
"foo": map[string]interface{}{
"bar": "old",
},
},
want: NestedMap{
"foo": map[string]interface{}{},
},
},
{
name: "Delete existing map",
key: "foo.bar",
existing: NestedMap{
"foo": map[string]interface{}{
"bar": map[string]interface{}{
"baz": "qux",
},
},
},
want: NestedMap{
"foo": map[string]interface{}{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.existing.Delete(tt.key)
if !reflect.DeepEqual(tt.existing, tt.want) {
t.Errorf("NestedMap.Set() got = %v, want %v", tt.existing, tt.want)
}
})
}
}
func TestMergeMaps(t *testing.T) {
tests := []struct {
name string