mirror of
https://github.com/token2/snapd.git
synced 2026-03-13 11:15:47 -07:00
Without this change strutil.OrderedMap would panic on a duplicate key. This change instead makes it return an error. This means that, if you're `try`ing a snap, edit its `snap.yaml` to accidentally have a duplicate key, instead of snapd getting depramestrated the user will see an error. See [lp:1782990] for the panic you used to get; with this change instead you get ``` $ snap info xyzzy --verbose name: xyzzy summary: "" publisher: – license: unset description: | notes: private: false confinement: devmode: false jailmode: false trymode: true enabled: true broken: true (cannot use installed snap "xyzzy" at revision x1: cannot parse snap.yaml: found duplicate key "foo") ignore-validation: false refresh-date: today at 11:22 BST installed: (x1) 0B ``` [lp:1782990]: https://bugs.launchpad.net/snapd/+bug/1782990
122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
// -*- Mode: Go; indent-tabs-mode: t -*-
|
|
|
|
/*
|
|
* Copyright (C) 2017 Canonical Ltd
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 3 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
package strutil
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
// OrderedMap is a map of strings to strings that preserves the
|
|
// insert order when calling "Keys()".
|
|
//
|
|
// Heavily based on the spread.Environment code (thanks for that!)
|
|
type OrderedMap struct {
|
|
keys []string
|
|
vals map[string]string
|
|
}
|
|
|
|
// NewOrderedMap creates a new ordered map initialized with the
|
|
// given pairs of strings.
|
|
func NewOrderedMap(pairs ...string) *OrderedMap {
|
|
o := &OrderedMap{vals: make(map[string]string),
|
|
keys: make([]string, len(pairs)/2),
|
|
}
|
|
for i := 0; i+1 < len(pairs); i += 2 {
|
|
o.vals[pairs[i]] = pairs[i+1]
|
|
o.keys[i/2] = pairs[i]
|
|
}
|
|
return o
|
|
}
|
|
|
|
// Keys returns a list of keys in the map sorted by insertion order
|
|
func (o *OrderedMap) Keys() []string {
|
|
return append([]string(nil), o.keys...)
|
|
}
|
|
|
|
// Get returns the value for the given key
|
|
func (o *OrderedMap) Get(k string) string {
|
|
return o.vals[k]
|
|
}
|
|
|
|
// Del removes the given key from the data structure
|
|
func (o *OrderedMap) Del(key string) {
|
|
l := len(o.vals)
|
|
delete(o.vals, key)
|
|
if len(o.vals) != l {
|
|
for i, k := range o.keys {
|
|
if k == key {
|
|
copy(o.keys[i:], o.keys[i+1:])
|
|
o.keys = o.keys[:len(o.keys)-1]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Set adds the given key, value to the map. If the key already
|
|
// exists it is removed and the new value is put on the end.
|
|
func (o *OrderedMap) Set(k, v string) {
|
|
o.Del(k)
|
|
o.keys = append(o.keys, k)
|
|
o.vals[k] = v
|
|
}
|
|
|
|
// Copy makes a copy of the map
|
|
func (o *OrderedMap) Copy() *OrderedMap {
|
|
copy := &OrderedMap{}
|
|
copy.keys = append([]string(nil), o.keys...)
|
|
copy.vals = make(map[string]string)
|
|
for k, v := range o.vals {
|
|
copy.vals[k] = v
|
|
}
|
|
return copy
|
|
}
|
|
|
|
// UnmarshalYAML unmarshals a yaml string map and preserves the order
|
|
func (o *OrderedMap) UnmarshalYAML(u func(interface{}) error) error {
|
|
var vals map[string]string
|
|
if err := u(&vals); err != nil {
|
|
return err
|
|
}
|
|
|
|
var seen = make(map[string]bool)
|
|
var keys = make([]string, len(vals))
|
|
var order yaml.MapSlice
|
|
if err := u(&order); err != nil {
|
|
return err
|
|
}
|
|
for i, item := range order {
|
|
k, ok := item.Key.(string)
|
|
_, good := vals[k]
|
|
if !ok || !good {
|
|
return fmt.Errorf("cannot read %q", item.Key)
|
|
}
|
|
if seen[k] {
|
|
return fmt.Errorf("found duplicate key %q", k)
|
|
}
|
|
seen[k] = true
|
|
keys[i] = k
|
|
}
|
|
o.keys = keys
|
|
o.vals = vals
|
|
return nil
|
|
}
|