1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
// -*- 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(any) 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
}
|