MSP-8172 Add vendor
This commit is contained in:
1 parent
5175d87e08
commit
eb066cb055
2619 files changed
+757077
-2
No files matched your search
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Charles Phillips
|
||||
|
||||
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.
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
# bellows
|
||||
Flatten and expand golang maps and structs
|
||||
|
||||
## Features
|
||||
There are some existing golang flatten/expand implementations, but they are targeted to specific use-cases, and none of them flatten Structs.
|
||||
|
||||
This `Flatten` will skip parsing a Map that does not have a String as the index.
|
||||
|
||||
This `Flatten` recursively passes values as their original Interface, so it is simpler than implementations that rely on passing reflect.Value.
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
// Expand a dot-separated flat map into a nested maps and slices
|
||||
func Expand(flat map[string]interface{}) interface{}
|
||||
|
||||
// Flatten a nested map into a dot-separated flat map
|
||||
func Flatten(value interface{}) map[string]interface{} {}
|
||||
|
||||
// Flatten a nested map into a dot-separated flat map, with a prefix
|
||||
func FlattenPrefixed(value interface{}, prefix string) map[string]interface{} {}
|
||||
|
||||
// Flatten a nested map into an existing dot-separated flat map, with a prefix
|
||||
func FlattenPrefixedToResult(value interface{}, prefix string, m map[string]interface{}) {}
|
||||
```
|
||||
|
||||
## Other golang flatten/expand implementations
|
||||
|
||||
* [hashicorp/terraform/flatmap](https://github.com/hashicorp/terraform/tree/master/flatmap)
|
||||
* [turtlemonvh/mapmap](https://github.com/turtlemonvh/mapmap)
|
||||
* [peking2/func-go](https://github.com/peking2/func-go)
|
||||
* [wolfeidau/unflatten](https://github.com/wolfeidau/unflatten)
|
||||
* [jeremywohl/flatten](https://github.com/jeremywohl/flatten)
|
||||
|
||||
(C) Copyright 2015 doublerebel. MIT Licensed.
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package bellows
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
arrayIndexRegexp = regexp.MustCompile("\\[\\d*]")
|
||||
)
|
||||
|
||||
func Expand(flatMap map[string]interface{}, opts ...option) interface{} {
|
||||
options := &bellowsOptions{sep: "."}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
var dst interface{}
|
||||
for path, value := range flatMap {
|
||||
parts := strings.Split(path, options.sep)
|
||||
dst = put(dst, parts, value)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func put(dst interface{}, path []string, value interface{}) interface{} {
|
||||
if len(path) == 0 {
|
||||
return value
|
||||
}
|
||||
|
||||
p := path[0]
|
||||
index, isArray := getArrayIndex(p)
|
||||
if isArray {
|
||||
if dst == nil {
|
||||
dst = make([]interface{}, 0, 3)
|
||||
}
|
||||
if arr, ok := dst.([]interface{}); ok {
|
||||
i := len(arr)
|
||||
if i == index {
|
||||
arr = append(arr, put(nil, path[1:], value))
|
||||
} else if i < index {
|
||||
toInsert := make([]interface{}, index-i)
|
||||
newItem := put(nil, path[1:], value)
|
||||
switch newItem.(type) {
|
||||
case []interface{}:
|
||||
for i := range toInsert {
|
||||
toInsert[i] = make([]interface{}, 0)
|
||||
}
|
||||
case map[string]interface{}:
|
||||
for i := range toInsert {
|
||||
toInsert[i] = make(map[string]interface{}, 0)
|
||||
}
|
||||
}
|
||||
arr = append(arr[:i], append(toInsert, newItem)...)
|
||||
} else {
|
||||
arr[index] = put(arr[index], path[1:], value)
|
||||
}
|
||||
|
||||
dst = arr
|
||||
}
|
||||
} else {
|
||||
if dst == nil {
|
||||
dst = make(map[string]interface{}, 3)
|
||||
}
|
||||
if m, ok := dst.(map[string]interface{}); ok {
|
||||
if val, ok := m[p]; ok {
|
||||
m[p] = put(val, path[1:], value)
|
||||
} else {
|
||||
m[p] = put(nil, path[1:], value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dst
|
||||
}
|
||||
|
||||
func getArrayIndex(part string) (int, bool) {
|
||||
index := arrayIndexRegexp.FindString(part)
|
||||
if index == "" {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
i, err := strconv.Atoi(index[1 : len(index)-1])
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
return i, true
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package bellows
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func Flatten(value interface{}, opts ...option) map[string]interface{} {
|
||||
options := &bellowsOptions{
|
||||
prefix: "",
|
||||
sep: ".",
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(options)
|
||||
}
|
||||
m := make(map[string]interface{}, 5)
|
||||
FlattenPrefixedToResult(value, options, m)
|
||||
return m
|
||||
}
|
||||
|
||||
func FlattenPrefixedToResult(value interface{}, opts *bellowsOptions, m map[string]interface{}) {
|
||||
original := reflect.ValueOf(value)
|
||||
kind := original.Kind()
|
||||
if kind == reflect.Ptr || kind == reflect.Interface {
|
||||
original = reflect.Indirect(original)
|
||||
kind = original.Kind()
|
||||
}
|
||||
|
||||
if !original.IsValid() {
|
||||
if opts.prefix != "" {
|
||||
m[opts.prefix] = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
t := original.Type()
|
||||
|
||||
switch kind {
|
||||
case reflect.Map:
|
||||
if t.Key().Kind() != reflect.String {
|
||||
break
|
||||
}
|
||||
keys := original.MapKeys()
|
||||
base := ""
|
||||
if opts.prefix != "" {
|
||||
base = opts.prefix + opts.sep
|
||||
}
|
||||
for _, childKey := range keys {
|
||||
childValue := original.MapIndex(childKey)
|
||||
FlattenPrefixedToResult(childValue.Interface(), &bellowsOptions{
|
||||
prefix: base + childKey.String(),
|
||||
sep: opts.sep,
|
||||
}, m)
|
||||
}
|
||||
case reflect.Struct:
|
||||
numField := original.NumField()
|
||||
base := ""
|
||||
for i := 0; i < numField; i++ {
|
||||
childValue := original.Field(i)
|
||||
f := t.Field(i)
|
||||
childKey := f.Name
|
||||
if f.Anonymous {
|
||||
childKey = ""
|
||||
base = opts.prefix
|
||||
} else if opts.prefix != "" {
|
||||
base = opts.prefix + opts.sep
|
||||
}
|
||||
FlattenPrefixedToResult(childValue.Interface(), &bellowsOptions{
|
||||
prefix: base + childKey,
|
||||
sep: opts.sep,
|
||||
}, m)
|
||||
}
|
||||
case reflect.Array, reflect.Slice:
|
||||
l := original.Len()
|
||||
base := opts.prefix
|
||||
for i := 0; i < l; i++ {
|
||||
childValue := original.Index(i)
|
||||
FlattenPrefixedToResult(childValue.Interface(), &bellowsOptions{
|
||||
prefix: fmt.Sprintf("%s%s[%d]", base, opts.sep, i),
|
||||
sep: opts.sep,
|
||||
}, m)
|
||||
}
|
||||
default:
|
||||
if opts.prefix != "" {
|
||||
m[opts.prefix] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package bellows
|
||||
|
||||
type bellowsOptions struct {
|
||||
prefix string
|
||||
sep string
|
||||
}
|
||||
|
||||
type option func(o *bellowsOptions)
|
||||
|
||||
func WithPrefix(prefix string) option {
|
||||
return func(o *bellowsOptions) {
|
||||
o.prefix = prefix
|
||||
}
|
||||
}
|
||||
|
||||
func WithSep(sep string) option {
|
||||
return func(o *bellowsOptions) {
|
||||
o.sep = sep
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user