代码拉取完成,页面将自动刷新
同步操作将从 tupelo-shen/mysnapd 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// -*- 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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。