diff options
author | Karim Khaleel <code.karim@gmail.com> | 2024-04-24 23:23:40 +0300 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-05-19 14:08:05 +0200 |
commit | b98ae1c773f3bd264a484a007d4b0cbe1f422ddd (patch) | |
tree | a5fe65beaff249704da38783d40aa617c5def69b /vendor/github.com | |
parent | 7d787afb2cd8f711b508fb63a7d097bffe9c9cb3 (diff) |
Add default lazygit config generator for Config.md from JSON schema
Diffstat (limited to 'vendor/github.com')
-rw-r--r-- | vendor/github.com/iancoleman/orderedmap/LICENSE | 21 | ||||
-rw-r--r-- | vendor/github.com/iancoleman/orderedmap/orderedmap.go | 266 | ||||
-rw-r--r-- | vendor/github.com/iancoleman/orderedmap/readme.md | 81 |
3 files changed, 368 insertions, 0 deletions
diff --git a/vendor/github.com/iancoleman/orderedmap/LICENSE b/vendor/github.com/iancoleman/orderedmap/LICENSE new file mode 100644 index 000000000..2732e3795 --- /dev/null +++ b/vendor/github.com/iancoleman/orderedmap/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Ian Coleman + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, Subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or Substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/iancoleman/orderedmap/orderedmap.go b/vendor/github.com/iancoleman/orderedmap/orderedmap.go new file mode 100644 index 000000000..61587d9c7 --- /dev/null +++ b/vendor/github.com/iancoleman/orderedmap/orderedmap.go @@ -0,0 +1,266 @@ +package orderedmap + +import ( + "bytes" + "encoding/json" + "sort" +) + +type Pair struct { + key string + value interface{} +} + +func (kv *Pair) Key() string { + return kv.key +} + +func (kv *Pair) Value() interface{} { + return kv.value +} + +type ByPair struct { + Pairs []*Pair + LessFunc func(a *Pair, j *Pair) bool +} + +func (a ByPair) Len() int { return len(a.Pairs) } +func (a ByPair) Swap(i, j int) { a.Pairs[i], a.Pairs[j] = a.Pairs[j], a.Pairs[i] } +func (a ByPair) Less(i, j int) bool { return a.LessFunc(a.Pairs[i], a.Pairs[j]) } + +type OrderedMap struct { + keys []string + values map[string]interface{} + escapeHTML bool +} + +func New() *OrderedMap { + o := OrderedMap{} + o.keys = []string{} + o.values = map[string]interface{}{} + o.escapeHTML = true + return &o +} + +func (o *OrderedMap) SetEscapeHTML(on bool) { + o.escapeHTML = on +} + +func (o *OrderedMap) Get(key string) (interface{}, bool) { + val, exists := o.values[key] + return val, exists +} + +func (o *OrderedMap) Set(key string, value interface{}) { + _, exists := o.values[key] + if !exists { + o.keys = append(o.keys, key) + } + o.values[key] = value +} + +func (o *OrderedMap) Delete(key string) { + // check key is in use + _, ok := o.values[key] + if !ok { + return + } + // remove from keys + for i, k := range o.keys { + if k == key { + o.keys = append(o.keys[:i], o.keys[i+1:]...) + break + } + } + // remove from values + delete(o.values, key) +} + +func (o *OrderedMap) Keys() []string { + return o.keys +} + +func (o *OrderedMap) Values() map[string]interface{} { + return o.values +} + +// SortKeys Sort the map keys using your sort func +func (o *OrderedMap) SortKeys(sortFunc func(keys []string)) { + sortFunc(o.keys) +} + +// Sort Sort the map using your sort func +func (o *OrderedMap) Sort(lessFunc func(a *Pair, b *Pair) bool) { + pairs := make([]*Pair, len(o.keys)) + for i, key := range o.keys { + pairs[i] = &Pair{key, o.values[key]} + } + + sort.Sort(ByPair{pairs, lessFunc}) + + for i, pair := range pairs { + o.keys[i] = pair.key + } +} + +func (o *OrderedMap) UnmarshalJSON(b []byte) error { + if o.values == nil { + o.values = map[string]interface{}{} + } + err := json.Unmarshal(b, &o.values) + if err != nil { + return err + } + dec := json.NewDecoder(bytes.NewReader(b)) + if _, err = dec.Token(); err != nil { // skip '{' + return err + } + o.keys = make([]string, 0, len(o.values)) + return decodeOrderedMap(dec, o) +} + +func decodeOrderedMap(dec *json.Decoder, o *OrderedMap) error { + hasKey := make(map[string]bool, len(o.values)) + for { + token, err := dec.Token() + if err != nil { + return err + } + if delim, ok := token.(json.Delim); ok && delim == '}' { + return nil + } + key := token.(string) + if hasKey[key] { + // duplicate key + for j, k := range o.keys { + if k == key { + copy(o.keys[j:], o.keys[j+1:]) + break + } + } + o.keys[len(o.keys)-1] = key + } else { + hasKey[key] = true + o.keys = append(o.keys, key) + } + + token, err = dec.Token() + if err != nil { + return err + } + if delim, ok := token.(json.Delim); ok { + switch delim { + case '{': + if values, ok := o.values[key].(map[string]interface{}); ok { + newMap := OrderedMap{ + keys: make([]string, 0, len(values)), + values: values, + escapeHTML: o.escapeHTML, + } + if err = decodeOrderedMap(dec, &newMap); err != nil { + return err + } + o.values[key] = newMap + } else if oldMap, ok := o.values[key].(OrderedMap); ok { + newMap := OrderedMap{ + keys: make([]string, 0, len(oldMap.values)), + values: oldMap.values, + escapeHTML: o.escapeHTML, + } + if err = decodeOrderedMap(dec, &newMap); err != nil { + return err + } + o.values[key] = newMap + } else if err = decodeOrderedMap(dec, &OrderedMap{}); err != nil { + return err + } + case '[': + if values, ok := o.values[key].([]interface{}); ok { + if err = decodeSlice(dec, values, o.escapeHTML); err != nil { + return err + } + } else if err = decodeSlice(dec, []interface{}{}, o.escapeHTML); err != nil { + return err + } + } + } + } +} + +func decodeSlice(dec *json.Decoder, s []interface{}, escapeHTML bool) error { + for index := 0; ; index++ { + token, err := dec.Token() + if err != nil { + return err + } + if delim, ok := token.(json.Delim); ok { + switch delim { + case '{': + if index < len(s) { + if values, ok := s[index].(map[string]interface{}); ok { + newMap := OrderedMap{ + keys: make([]string, 0, len(values)), + values: values, + escapeHTML: escapeHTML, + } + if err = decodeOrderedMap(dec, &newMap); err != nil { + return err + } + s[index] = newMap + } else if oldMap, ok := s[index].(OrderedMap); ok { + newMap := OrderedMap{ + keys: make([]string, 0, len(oldMap.values)), + values: oldMap.values, + escapeHTML: escapeHTML, + } + if err = decodeOrderedMap(dec, &newMap); err != nil { + return err + } + s[index] = newMap + } else if err = decodeOrderedMap(dec, &OrderedMap{}); err != nil { + return err + } + } else if err = decodeOrderedMap(dec, &OrderedMap{}); err != nil { + return err + } + case '[': + if index < len(s) { + if values, ok := s[index].([]interface{}); ok { + if err = decodeSlice(dec, values, escapeHTML); err != nil { + return err + } + } else if err = decodeSlice(dec, []interface{}{}, escapeHTML); err != nil { + return err + } + } else if err = decodeSlice(dec, []interface{}{}, escapeHTML); err != nil { + return err + } + case ']': + return nil + } + } + } +} + +func (o OrderedMap) MarshalJSON() ([]byte, error) { + var buf bytes.Buffer + buf.WriteByte('{') + encoder := json.NewEncoder(&buf) + encoder.SetEscapeHTML(o.escapeHTML) + for i, k := range o.keys { + if i > 0 { + buf.WriteByte(',') + } + // add key + if err := encoder.Encode(k); err != nil { + return nil, err + } + buf.WriteByte(':') + // add value + if err := encoder.Encode(o.values[k]); err != nil { + return nil, err + } + } + buf.WriteByte('}') + return buf.Bytes(), nil +} diff --git a/vendor/github.com/iancoleman/orderedmap/readme.md b/vendor/github.com/iancoleman/orderedmap/readme.md new file mode 100644 index 000000000..cfe09c2fd --- /dev/null +++ b/vendor/github.com/iancoleman/orderedmap/readme.md @@ -0,0 +1,81 @@ +# orderedmap + +[![Build Status](https://travis-ci.com/iancoleman/orderedmap.svg)](https://travis-ci.com/iancoleman/orderedmap) + +A golang data type equivalent to python's collections.OrderedDict + +Retains order of keys in maps + +Can be JSON serialized / deserialized + +# Usage + +```go +package main + +import ( + "encoding/json" + "github.com/iancoleman/orderedmap" +) + +func main() { + + // use New() instead of o := map[string]interface{}{} + o := orderedmap.New() + + // use SetEscapeHTML() to whether escape problematic HTML characters or not, defaults is true + o.SetEscapeHTML(false) + + // use Set instead of o["a"] = 1 + o.Set("a", 1) + + // add some value with special characters + o.Set("b", "\\.<>[]{}_-") + + // use Get instead of i, ok := o["a"] + val, ok := o.Get("a") + + // use Keys instead of for k, v := range o + keys := o.Keys() + for _, k := range keys { + v, _ := o.Get(k) + } + + // use o.Delete instead of delete(o, key) + o.Delete("a") + + // serialize to a json string using encoding/json + bytes, err := json.Marshal(o) + prettyBytes, err := json.MarshalIndent(o, "", " ") + + // deserialize a json string using encoding/json + // all maps (including nested maps) will be parsed as orderedmaps + s := `{"a": 1}` + err := json.Unmarshal([]byte(s), &o) + + // sort the keys + o.SortKeys(sort.Strings) + + // sort by Pair + o.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool { + return a.Value().(float64) < b.Value().(float64) + }) +} +``` + +# Caveats + +* OrderedMap only takes strings for the key, as per [the JSON spec](http://json.org/). + +# Tests + +``` +go test +``` + +# Alternatives + +None of the alternatives offer JSON serialization. + +* [cevaris/ordered_map](https://github.com/cevaris/ordered_map) +* [mantyr/iterator](https://github.com/mantyr/iterator) |