chore(wallet) add stretchr/testify/mock to dependencies
This commit is contained in:
parent
afe0fcdcf4
commit
39fbaca5a9
1
go.mod
1
go.mod
|
@ -250,6 +250,7 @@ require (
|
||||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||||
github.com/status-im/go-multiaddr-ethv4 v1.2.5 // indirect
|
github.com/status-im/go-multiaddr-ethv4 v1.2.5 // indirect
|
||||||
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 // indirect
|
github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.0 // indirect
|
||||||
github.com/tklauser/go-sysconf v0.3.6 // indirect
|
github.com/tklauser/go-sysconf v0.3.6 // indirect
|
||||||
github.com/tklauser/numcpus v0.2.2 // indirect
|
github.com/tklauser/numcpus v0.2.2 // indirect
|
||||||
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
|
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
|
||||||
|
|
1
go.sum
1
go.sum
|
@ -2016,6 +2016,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
|
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
engines:
|
||||||
|
gofmt:
|
||||||
|
enabled: true
|
||||||
|
golint:
|
||||||
|
enabled: true
|
||||||
|
govet:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
exclude_patterns:
|
||||||
|
- ".github/"
|
||||||
|
- "vendor/"
|
||||||
|
- "codegen/"
|
||||||
|
- "*.yml"
|
||||||
|
- ".*.yml"
|
||||||
|
- "*.md"
|
||||||
|
- "Gopkg.*"
|
||||||
|
- "doc.go"
|
||||||
|
- "type_specific_codegen_test.go"
|
||||||
|
- "type_specific_codegen.go"
|
||||||
|
- ".gitignore"
|
||||||
|
- "LICENSE"
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, build with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
|
@ -0,0 +1,22 @@
|
||||||
|
The MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2014 Stretchr, Inc.
|
||||||
|
Copyright (c) 2017-2018 objx contributors
|
||||||
|
|
||||||
|
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.
|
|
@ -0,0 +1,80 @@
|
||||||
|
# Objx
|
||||||
|
[![Build Status](https://travis-ci.org/stretchr/objx.svg?branch=master)](https://travis-ci.org/stretchr/objx)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/github.com/stretchr/objx)](https://goreportcard.com/report/github.com/stretchr/objx)
|
||||||
|
[![Maintainability](https://api.codeclimate.com/v1/badges/1d64bc6c8474c2074f2b/maintainability)](https://codeclimate.com/github/stretchr/objx/maintainability)
|
||||||
|
[![Test Coverage](https://api.codeclimate.com/v1/badges/1d64bc6c8474c2074f2b/test_coverage)](https://codeclimate.com/github/stretchr/objx/test_coverage)
|
||||||
|
[![Sourcegraph](https://sourcegraph.com/github.com/stretchr/objx/-/badge.svg)](https://sourcegraph.com/github.com/stretchr/objx)
|
||||||
|
[![GoDoc](https://godoc.org/github.com/stretchr/objx?status.svg)](https://godoc.org/github.com/stretchr/objx)
|
||||||
|
|
||||||
|
Objx - Go package for dealing with maps, slices, JSON and other data.
|
||||||
|
|
||||||
|
Get started:
|
||||||
|
|
||||||
|
- Install Objx with [one line of code](#installation), or [update it with another](#staying-up-to-date)
|
||||||
|
- Check out the API Documentation http://godoc.org/github.com/stretchr/objx
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes a powerful `Get` method (among others) that allows you to easily and quickly get access to data within the map, without having to worry too much about type assertions, missing data, default values etc.
|
||||||
|
|
||||||
|
### Pattern
|
||||||
|
Objx uses a preditable pattern to make access data from within `map[string]interface{}` easy. Call one of the `objx.` functions to create your `objx.Map` to get going:
|
||||||
|
|
||||||
|
m, err := objx.FromJSON(json)
|
||||||
|
|
||||||
|
NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong, the rest will be optimistic and try to figure things out without panicking.
|
||||||
|
|
||||||
|
Use `Get` to access the value you're interested in. You can use dot and array
|
||||||
|
notation too:
|
||||||
|
|
||||||
|
m.Get("places[0].latlng")
|
||||||
|
|
||||||
|
Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type.
|
||||||
|
|
||||||
|
if m.Get("code").IsStr() { // Your code... }
|
||||||
|
|
||||||
|
Or you can just assume the type, and use one of the strong type methods to extract the real value:
|
||||||
|
|
||||||
|
m.Get("code").Int()
|
||||||
|
|
||||||
|
If there's no value there (or if it's the wrong type) then a default value will be returned, or you can be explicit about the default value.
|
||||||
|
|
||||||
|
Get("code").Int(-1)
|
||||||
|
|
||||||
|
If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating, manipulating and selecting that data. You can find out more by exploring the index below.
|
||||||
|
|
||||||
|
### Reading data
|
||||||
|
A simple example of how to use Objx:
|
||||||
|
|
||||||
|
// Use MustFromJSON to make an objx.Map from some JSON
|
||||||
|
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
|
||||||
|
|
||||||
|
// Get the details
|
||||||
|
name := m.Get("name").Str()
|
||||||
|
age := m.Get("age").Int()
|
||||||
|
|
||||||
|
// Get their nickname (or use their name if they don't have one)
|
||||||
|
nickname := m.Get("nickname").Str(name)
|
||||||
|
|
||||||
|
### Ranging
|
||||||
|
Since `objx.Map` is a `map[string]interface{}` you can treat it as such. For example, to `range` the data, do what you would expect:
|
||||||
|
|
||||||
|
m := objx.MustFromJSON(json)
|
||||||
|
for key, value := range m {
|
||||||
|
// Your code...
|
||||||
|
}
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
To install Objx, use go get:
|
||||||
|
|
||||||
|
go get github.com/stretchr/objx
|
||||||
|
|
||||||
|
### Staying up to date
|
||||||
|
To update Objx to the latest version, run:
|
||||||
|
|
||||||
|
go get -u github.com/stretchr/objx
|
||||||
|
|
||||||
|
### Supported go versions
|
||||||
|
We support the lastest three major Go versions, which are 1.10, 1.11 and 1.12 at the moment.
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
Please feel free to submit issues, fork the repository and send pull requests!
|
|
@ -0,0 +1,30 @@
|
||||||
|
version: '2'
|
||||||
|
|
||||||
|
env:
|
||||||
|
GOFLAGS: -mod=vendor
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
default:
|
||||||
|
deps: [test]
|
||||||
|
|
||||||
|
lint:
|
||||||
|
desc: Checks code style
|
||||||
|
cmds:
|
||||||
|
- gofmt -d -s *.go
|
||||||
|
- go vet ./...
|
||||||
|
silent: true
|
||||||
|
|
||||||
|
lint-fix:
|
||||||
|
desc: Fixes code style
|
||||||
|
cmds:
|
||||||
|
- gofmt -w -s *.go
|
||||||
|
|
||||||
|
test:
|
||||||
|
desc: Runs go tests
|
||||||
|
cmds:
|
||||||
|
- go test -race ./...
|
||||||
|
|
||||||
|
test-coverage:
|
||||||
|
desc: Runs go tests and calculates test coverage
|
||||||
|
cmds:
|
||||||
|
- go test -race -coverprofile=c.out ./...
|
|
@ -0,0 +1,197 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PathSeparator is the character used to separate the elements
|
||||||
|
// of the keypath.
|
||||||
|
//
|
||||||
|
// For example, `location.address.city`
|
||||||
|
PathSeparator string = "."
|
||||||
|
|
||||||
|
// arrayAccesRegexString is the regex used to extract the array number
|
||||||
|
// from the access path
|
||||||
|
arrayAccesRegexString = `^(.+)\[([0-9]+)\]$`
|
||||||
|
|
||||||
|
// mapAccessRegexString is the regex used to extract the map key
|
||||||
|
// from the access path
|
||||||
|
mapAccessRegexString = `^([^\[]*)\[([^\]]+)\](.*)$`
|
||||||
|
)
|
||||||
|
|
||||||
|
// arrayAccesRegex is the compiled arrayAccesRegexString
|
||||||
|
var arrayAccesRegex = regexp.MustCompile(arrayAccesRegexString)
|
||||||
|
|
||||||
|
// mapAccessRegex is the compiled mapAccessRegexString
|
||||||
|
var mapAccessRegex = regexp.MustCompile(mapAccessRegexString)
|
||||||
|
|
||||||
|
// Get gets the value using the specified selector and
|
||||||
|
// returns it inside a new Obj object.
|
||||||
|
//
|
||||||
|
// If it cannot find the value, Get will return a nil
|
||||||
|
// value inside an instance of Obj.
|
||||||
|
//
|
||||||
|
// Get can only operate directly on map[string]interface{} and []interface.
|
||||||
|
//
|
||||||
|
// Example
|
||||||
|
//
|
||||||
|
// To access the title of the third chapter of the second book, do:
|
||||||
|
//
|
||||||
|
// o.Get("books[1].chapters[2].title")
|
||||||
|
func (m Map) Get(selector string) *Value {
|
||||||
|
rawObj := access(m, selector, nil, false)
|
||||||
|
return &Value{data: rawObj}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the value using the specified selector and
|
||||||
|
// returns the object on which Set was called.
|
||||||
|
//
|
||||||
|
// Set can only operate directly on map[string]interface{} and []interface
|
||||||
|
//
|
||||||
|
// Example
|
||||||
|
//
|
||||||
|
// To set the title of the third chapter of the second book, do:
|
||||||
|
//
|
||||||
|
// o.Set("books[1].chapters[2].title","Time to Go")
|
||||||
|
func (m Map) Set(selector string, value interface{}) Map {
|
||||||
|
access(m, selector, value, true)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// getIndex returns the index, which is hold in s by two braches.
|
||||||
|
// It also returns s withour the index part, e.g. name[1] will return (1, name).
|
||||||
|
// If no index is found, -1 is returned
|
||||||
|
func getIndex(s string) (int, string) {
|
||||||
|
arrayMatches := arrayAccesRegex.FindStringSubmatch(s)
|
||||||
|
if len(arrayMatches) > 0 {
|
||||||
|
// Get the key into the map
|
||||||
|
selector := arrayMatches[1]
|
||||||
|
// Get the index into the array at the key
|
||||||
|
// We know this cannt fail because arrayMatches[2] is an int for sure
|
||||||
|
index, _ := strconv.Atoi(arrayMatches[2])
|
||||||
|
return index, selector
|
||||||
|
}
|
||||||
|
return -1, s
|
||||||
|
}
|
||||||
|
|
||||||
|
// getKey returns the key which is held in s by two brackets.
|
||||||
|
// It also returns the next selector.
|
||||||
|
func getKey(s string) (string, string) {
|
||||||
|
selSegs := strings.SplitN(s, PathSeparator, 2)
|
||||||
|
thisSel := selSegs[0]
|
||||||
|
nextSel := ""
|
||||||
|
|
||||||
|
if len(selSegs) > 1 {
|
||||||
|
nextSel = selSegs[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
mapMatches := mapAccessRegex.FindStringSubmatch(s)
|
||||||
|
if len(mapMatches) > 0 {
|
||||||
|
if _, err := strconv.Atoi(mapMatches[2]); err != nil {
|
||||||
|
thisSel = mapMatches[1]
|
||||||
|
nextSel = "[" + mapMatches[2] + "]" + mapMatches[3]
|
||||||
|
|
||||||
|
if thisSel == "" {
|
||||||
|
thisSel = mapMatches[2]
|
||||||
|
nextSel = mapMatches[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
if nextSel == "" {
|
||||||
|
selSegs = []string{"", ""}
|
||||||
|
} else if nextSel[0] == '.' {
|
||||||
|
nextSel = nextSel[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return thisSel, nextSel
|
||||||
|
}
|
||||||
|
|
||||||
|
// access accesses the object using the selector and performs the
|
||||||
|
// appropriate action.
|
||||||
|
func access(current interface{}, selector string, value interface{}, isSet bool) interface{} {
|
||||||
|
thisSel, nextSel := getKey(selector)
|
||||||
|
|
||||||
|
indexes := []int{}
|
||||||
|
for strings.Contains(thisSel, "[") {
|
||||||
|
prevSel := thisSel
|
||||||
|
index := -1
|
||||||
|
index, thisSel = getIndex(thisSel)
|
||||||
|
indexes = append(indexes, index)
|
||||||
|
if prevSel == thisSel {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if curMap, ok := current.(Map); ok {
|
||||||
|
current = map[string]interface{}(curMap)
|
||||||
|
}
|
||||||
|
// get the object in question
|
||||||
|
switch current.(type) {
|
||||||
|
case map[string]interface{}:
|
||||||
|
curMSI := current.(map[string]interface{})
|
||||||
|
if nextSel == "" && isSet {
|
||||||
|
curMSI[thisSel] = value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok := curMSI[thisSel].(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
_, ok = curMSI[thisSel].(Map)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (curMSI[thisSel] == nil || !ok) && len(indexes) == 0 && isSet {
|
||||||
|
curMSI[thisSel] = map[string]interface{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
current = curMSI[thisSel]
|
||||||
|
default:
|
||||||
|
current = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// do we need to access the item of an array?
|
||||||
|
if len(indexes) > 0 {
|
||||||
|
num := len(indexes)
|
||||||
|
for num > 0 {
|
||||||
|
num--
|
||||||
|
index := indexes[num]
|
||||||
|
indexes = indexes[:num]
|
||||||
|
if array, ok := interSlice(current); ok {
|
||||||
|
if index < len(array) {
|
||||||
|
current = array[index]
|
||||||
|
} else {
|
||||||
|
current = nil
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if nextSel != "" {
|
||||||
|
current = access(current, nextSel, value, isSet)
|
||||||
|
}
|
||||||
|
return current
|
||||||
|
}
|
||||||
|
|
||||||
|
func interSlice(slice interface{}) ([]interface{}, bool) {
|
||||||
|
if array, ok := slice.([]interface{}); ok {
|
||||||
|
return array, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
s := reflect.ValueOf(slice)
|
||||||
|
if s.Kind() != reflect.Slice {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := make([]interface{}, s.Len())
|
||||||
|
|
||||||
|
for i := 0; i < s.Len(); i++ {
|
||||||
|
ret[i] = s.Index(i).Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, true
|
||||||
|
}
|
|
@ -0,0 +1,280 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SignatureSeparator is the character that is used to
|
||||||
|
// separate the Base64 string from the security signature.
|
||||||
|
const SignatureSeparator = "_"
|
||||||
|
|
||||||
|
// URLValuesSliceKeySuffix is the character that is used to
|
||||||
|
// specify a suffic for slices parsed by URLValues.
|
||||||
|
// If the suffix is set to "[i]", then the index of the slice
|
||||||
|
// is used in place of i
|
||||||
|
// Ex: Suffix "[]" would have the form a[]=b&a[]=c
|
||||||
|
// OR Suffix "[i]" would have the form a[0]=b&a[1]=c
|
||||||
|
// OR Suffix "" would have the form a=b&a=c
|
||||||
|
var urlValuesSliceKeySuffix = "[]"
|
||||||
|
|
||||||
|
const (
|
||||||
|
URLValuesSliceKeySuffixEmpty = ""
|
||||||
|
URLValuesSliceKeySuffixArray = "[]"
|
||||||
|
URLValuesSliceKeySuffixIndex = "[i]"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetURLValuesSliceKeySuffix sets the character that is used to
|
||||||
|
// specify a suffic for slices parsed by URLValues.
|
||||||
|
// If the suffix is set to "[i]", then the index of the slice
|
||||||
|
// is used in place of i
|
||||||
|
// Ex: Suffix "[]" would have the form a[]=b&a[]=c
|
||||||
|
// OR Suffix "[i]" would have the form a[0]=b&a[1]=c
|
||||||
|
// OR Suffix "" would have the form a=b&a=c
|
||||||
|
func SetURLValuesSliceKeySuffix(s string) error {
|
||||||
|
if s == URLValuesSliceKeySuffixEmpty || s == URLValuesSliceKeySuffixArray || s == URLValuesSliceKeySuffixIndex {
|
||||||
|
urlValuesSliceKeySuffix = s
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("objx: Invalid URLValuesSliceKeySuffix provided.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON converts the contained object to a JSON string
|
||||||
|
// representation
|
||||||
|
func (m Map) JSON() (string, error) {
|
||||||
|
for k, v := range m {
|
||||||
|
m[k] = cleanUp(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := json.Marshal(m)
|
||||||
|
if err != nil {
|
||||||
|
err = errors.New("objx: JSON encode failed with: " + err.Error())
|
||||||
|
}
|
||||||
|
return string(result), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanUpInterfaceArray(in []interface{}) []interface{} {
|
||||||
|
result := make([]interface{}, len(in))
|
||||||
|
for i, v := range in {
|
||||||
|
result[i] = cleanUp(v)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanUpInterfaceMap(in map[interface{}]interface{}) Map {
|
||||||
|
result := Map{}
|
||||||
|
for k, v := range in {
|
||||||
|
result[fmt.Sprintf("%v", k)] = cleanUp(v)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanUpStringMap(in map[string]interface{}) Map {
|
||||||
|
result := Map{}
|
||||||
|
for k, v := range in {
|
||||||
|
result[k] = cleanUp(v)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanUpMSIArray(in []map[string]interface{}) []Map {
|
||||||
|
result := make([]Map, len(in))
|
||||||
|
for i, v := range in {
|
||||||
|
result[i] = cleanUpStringMap(v)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanUpMapArray(in []Map) []Map {
|
||||||
|
result := make([]Map, len(in))
|
||||||
|
for i, v := range in {
|
||||||
|
result[i] = cleanUpStringMap(v)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanUp(v interface{}) interface{} {
|
||||||
|
switch v := v.(type) {
|
||||||
|
case []interface{}:
|
||||||
|
return cleanUpInterfaceArray(v)
|
||||||
|
case []map[string]interface{}:
|
||||||
|
return cleanUpMSIArray(v)
|
||||||
|
case map[interface{}]interface{}:
|
||||||
|
return cleanUpInterfaceMap(v)
|
||||||
|
case Map:
|
||||||
|
return cleanUpStringMap(v)
|
||||||
|
case []Map:
|
||||||
|
return cleanUpMapArray(v)
|
||||||
|
default:
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustJSON converts the contained object to a JSON string
|
||||||
|
// representation and panics if there is an error
|
||||||
|
func (m Map) MustJSON() string {
|
||||||
|
result, err := m.JSON()
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Base64 converts the contained object to a Base64 string
|
||||||
|
// representation of the JSON string representation
|
||||||
|
func (m Map) Base64() (string, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
jsonData, err := m.JSON()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
|
||||||
|
_, _ = encoder.Write([]byte(jsonData))
|
||||||
|
_ = encoder.Close()
|
||||||
|
|
||||||
|
return buf.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustBase64 converts the contained object to a Base64 string
|
||||||
|
// representation of the JSON string representation and panics
|
||||||
|
// if there is an error
|
||||||
|
func (m Map) MustBase64() string {
|
||||||
|
result, err := m.Base64()
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignedBase64 converts the contained object to a Base64 string
|
||||||
|
// representation of the JSON string representation and signs it
|
||||||
|
// using the provided key.
|
||||||
|
func (m Map) SignedBase64(key string) (string, error) {
|
||||||
|
base64, err := m.Base64()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
sig := HashWithKey(base64, key)
|
||||||
|
return base64 + SignatureSeparator + sig, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustSignedBase64 converts the contained object to a Base64 string
|
||||||
|
// representation of the JSON string representation and signs it
|
||||||
|
// using the provided key and panics if there is an error
|
||||||
|
func (m Map) MustSignedBase64(key string) string {
|
||||||
|
result, err := m.SignedBase64(key)
|
||||||
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
URL Query
|
||||||
|
------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
// URLValues creates a url.Values object from an Obj. This
|
||||||
|
// function requires that the wrapped object be a map[string]interface{}
|
||||||
|
func (m Map) URLValues() url.Values {
|
||||||
|
vals := make(url.Values)
|
||||||
|
|
||||||
|
m.parseURLValues(m, vals, "")
|
||||||
|
|
||||||
|
return vals
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Map) parseURLValues(queryMap Map, vals url.Values, key string) {
|
||||||
|
useSliceIndex := false
|
||||||
|
if urlValuesSliceKeySuffix == "[i]" {
|
||||||
|
useSliceIndex = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range queryMap {
|
||||||
|
val := &Value{data: v}
|
||||||
|
switch {
|
||||||
|
case val.IsObjxMap():
|
||||||
|
if key == "" {
|
||||||
|
m.parseURLValues(val.ObjxMap(), vals, k)
|
||||||
|
} else {
|
||||||
|
m.parseURLValues(val.ObjxMap(), vals, key+"["+k+"]")
|
||||||
|
}
|
||||||
|
case val.IsObjxMapSlice():
|
||||||
|
sliceKey := k
|
||||||
|
if key != "" {
|
||||||
|
sliceKey = key + "[" + k + "]"
|
||||||
|
}
|
||||||
|
|
||||||
|
if useSliceIndex {
|
||||||
|
for i, sv := range val.MustObjxMapSlice() {
|
||||||
|
sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]"
|
||||||
|
m.parseURLValues(sv, vals, sk)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sliceKey = sliceKey + urlValuesSliceKeySuffix
|
||||||
|
for _, sv := range val.MustObjxMapSlice() {
|
||||||
|
m.parseURLValues(sv, vals, sliceKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case val.IsMSISlice():
|
||||||
|
sliceKey := k
|
||||||
|
if key != "" {
|
||||||
|
sliceKey = key + "[" + k + "]"
|
||||||
|
}
|
||||||
|
|
||||||
|
if useSliceIndex {
|
||||||
|
for i, sv := range val.MustMSISlice() {
|
||||||
|
sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]"
|
||||||
|
m.parseURLValues(New(sv), vals, sk)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sliceKey = sliceKey + urlValuesSliceKeySuffix
|
||||||
|
for _, sv := range val.MustMSISlice() {
|
||||||
|
m.parseURLValues(New(sv), vals, sliceKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case val.IsStrSlice(), val.IsBoolSlice(),
|
||||||
|
val.IsFloat32Slice(), val.IsFloat64Slice(),
|
||||||
|
val.IsIntSlice(), val.IsInt8Slice(), val.IsInt16Slice(), val.IsInt32Slice(), val.IsInt64Slice(),
|
||||||
|
val.IsUintSlice(), val.IsUint8Slice(), val.IsUint16Slice(), val.IsUint32Slice(), val.IsUint64Slice():
|
||||||
|
|
||||||
|
sliceKey := k
|
||||||
|
if key != "" {
|
||||||
|
sliceKey = key + "[" + k + "]"
|
||||||
|
}
|
||||||
|
|
||||||
|
if useSliceIndex {
|
||||||
|
for i, sv := range val.StringSlice() {
|
||||||
|
sk := sliceKey + "[" + strconv.FormatInt(int64(i), 10) + "]"
|
||||||
|
vals.Set(sk, sv)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sliceKey = sliceKey + urlValuesSliceKeySuffix
|
||||||
|
vals[sliceKey] = val.StringSlice()
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
if key == "" {
|
||||||
|
vals.Set(k, val.String())
|
||||||
|
} else {
|
||||||
|
vals.Set(key+"["+k+"]", val.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// URLQuery gets an encoded URL query representing the given
|
||||||
|
// Obj. This function requires that the wrapped object be a
|
||||||
|
// map[string]interface{}
|
||||||
|
func (m Map) URLQuery() (string, error) {
|
||||||
|
return m.URLValues().Encode(), nil
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
/*
|
||||||
|
Objx - Go package for dealing with maps, slices, JSON and other data.
|
||||||
|
|
||||||
|
Overview
|
||||||
|
|
||||||
|
Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes
|
||||||
|
a powerful `Get` method (among others) that allows you to easily and quickly get
|
||||||
|
access to data within the map, without having to worry too much about type assertions,
|
||||||
|
missing data, default values etc.
|
||||||
|
|
||||||
|
Pattern
|
||||||
|
|
||||||
|
Objx uses a preditable pattern to make access data from within `map[string]interface{}` easy.
|
||||||
|
Call one of the `objx.` functions to create your `objx.Map` to get going:
|
||||||
|
|
||||||
|
m, err := objx.FromJSON(json)
|
||||||
|
|
||||||
|
NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong,
|
||||||
|
the rest will be optimistic and try to figure things out without panicking.
|
||||||
|
|
||||||
|
Use `Get` to access the value you're interested in. You can use dot and array
|
||||||
|
notation too:
|
||||||
|
|
||||||
|
m.Get("places[0].latlng")
|
||||||
|
|
||||||
|
Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type.
|
||||||
|
|
||||||
|
if m.Get("code").IsStr() { // Your code... }
|
||||||
|
|
||||||
|
Or you can just assume the type, and use one of the strong type methods to extract the real value:
|
||||||
|
|
||||||
|
m.Get("code").Int()
|
||||||
|
|
||||||
|
If there's no value there (or if it's the wrong type) then a default value will be returned,
|
||||||
|
or you can be explicit about the default value.
|
||||||
|
|
||||||
|
Get("code").Int(-1)
|
||||||
|
|
||||||
|
If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating,
|
||||||
|
manipulating and selecting that data. You can find out more by exploring the index below.
|
||||||
|
|
||||||
|
Reading data
|
||||||
|
|
||||||
|
A simple example of how to use Objx:
|
||||||
|
|
||||||
|
// Use MustFromJSON to make an objx.Map from some JSON
|
||||||
|
m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
|
||||||
|
|
||||||
|
// Get the details
|
||||||
|
name := m.Get("name").Str()
|
||||||
|
age := m.Get("age").Int()
|
||||||
|
|
||||||
|
// Get their nickname (or use their name if they don't have one)
|
||||||
|
nickname := m.Get("nickname").Str(name)
|
||||||
|
|
||||||
|
Ranging
|
||||||
|
|
||||||
|
Since `objx.Map` is a `map[string]interface{}` you can treat it as such.
|
||||||
|
For example, to `range` the data, do what you would expect:
|
||||||
|
|
||||||
|
m := objx.MustFromJSON(json)
|
||||||
|
for key, value := range m {
|
||||||
|
// Your code...
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
package objx
|
|
@ -0,0 +1,215 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MSIConvertable is an interface that defines methods for converting your
|
||||||
|
// custom types to a map[string]interface{} representation.
|
||||||
|
type MSIConvertable interface {
|
||||||
|
// MSI gets a map[string]interface{} (msi) representing the
|
||||||
|
// object.
|
||||||
|
MSI() map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map provides extended functionality for working with
|
||||||
|
// untyped data, in particular map[string]interface (msi).
|
||||||
|
type Map map[string]interface{}
|
||||||
|
|
||||||
|
// Value returns the internal value instance
|
||||||
|
func (m Map) Value() *Value {
|
||||||
|
return &Value{data: m}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nil represents a nil Map.
|
||||||
|
var Nil = New(nil)
|
||||||
|
|
||||||
|
// New creates a new Map containing the map[string]interface{} in the data argument.
|
||||||
|
// If the data argument is not a map[string]interface, New attempts to call the
|
||||||
|
// MSI() method on the MSIConvertable interface to create one.
|
||||||
|
func New(data interface{}) Map {
|
||||||
|
if _, ok := data.(map[string]interface{}); !ok {
|
||||||
|
if converter, ok := data.(MSIConvertable); ok {
|
||||||
|
data = converter.MSI()
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Map(data.(map[string]interface{}))
|
||||||
|
}
|
||||||
|
|
||||||
|
// MSI creates a map[string]interface{} and puts it inside a new Map.
|
||||||
|
//
|
||||||
|
// The arguments follow a key, value pattern.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Returns nil if any key argument is non-string or if there are an odd number of arguments.
|
||||||
|
//
|
||||||
|
// Example
|
||||||
|
//
|
||||||
|
// To easily create Maps:
|
||||||
|
//
|
||||||
|
// m := objx.MSI("name", "Mat", "age", 29, "subobj", objx.MSI("active", true))
|
||||||
|
//
|
||||||
|
// // creates an Map equivalent to
|
||||||
|
// m := objx.Map{"name": "Mat", "age": 29, "subobj": objx.Map{"active": true}}
|
||||||
|
func MSI(keyAndValuePairs ...interface{}) Map {
|
||||||
|
newMap := Map{}
|
||||||
|
keyAndValuePairsLen := len(keyAndValuePairs)
|
||||||
|
if keyAndValuePairsLen%2 != 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for i := 0; i < keyAndValuePairsLen; i = i + 2 {
|
||||||
|
key := keyAndValuePairs[i]
|
||||||
|
value := keyAndValuePairs[i+1]
|
||||||
|
|
||||||
|
// make sure the key is a string
|
||||||
|
keyString, keyStringOK := key.(string)
|
||||||
|
if !keyStringOK {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
newMap[keyString] = value
|
||||||
|
}
|
||||||
|
return newMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// ****** Conversion Constructors
|
||||||
|
|
||||||
|
// MustFromJSON creates a new Map containing the data specified in the
|
||||||
|
// jsonString.
|
||||||
|
//
|
||||||
|
// Panics if the JSON is invalid.
|
||||||
|
func MustFromJSON(jsonString string) Map {
|
||||||
|
o, err := FromJSON(jsonString)
|
||||||
|
if err != nil {
|
||||||
|
panic("objx: MustFromJSON failed with error: " + err.Error())
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustFromJSONSlice creates a new slice of Map containing the data specified in the
|
||||||
|
// jsonString. Works with jsons with a top level array
|
||||||
|
//
|
||||||
|
// Panics if the JSON is invalid.
|
||||||
|
func MustFromJSONSlice(jsonString string) []Map {
|
||||||
|
slice, err := FromJSONSlice(jsonString)
|
||||||
|
if err != nil {
|
||||||
|
panic("objx: MustFromJSONSlice failed with error: " + err.Error())
|
||||||
|
}
|
||||||
|
return slice
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromJSON creates a new Map containing the data specified in the
|
||||||
|
// jsonString.
|
||||||
|
//
|
||||||
|
// Returns an error if the JSON is invalid.
|
||||||
|
func FromJSON(jsonString string) (Map, error) {
|
||||||
|
var m Map
|
||||||
|
err := json.Unmarshal([]byte(jsonString), &m)
|
||||||
|
if err != nil {
|
||||||
|
return Nil, err
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromJSONSlice creates a new slice of Map containing the data specified in the
|
||||||
|
// jsonString. Works with jsons with a top level array
|
||||||
|
//
|
||||||
|
// Returns an error if the JSON is invalid.
|
||||||
|
func FromJSONSlice(jsonString string) ([]Map, error) {
|
||||||
|
var slice []Map
|
||||||
|
err := json.Unmarshal([]byte(jsonString), &slice)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return slice, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromBase64 creates a new Obj containing the data specified
|
||||||
|
// in the Base64 string.
|
||||||
|
//
|
||||||
|
// The string is an encoded JSON string returned by Base64
|
||||||
|
func FromBase64(base64String string) (Map, error) {
|
||||||
|
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64String))
|
||||||
|
decoded, err := ioutil.ReadAll(decoder)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return FromJSON(string(decoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustFromBase64 creates a new Obj containing the data specified
|
||||||
|
// in the Base64 string and panics if there is an error.
|
||||||
|
//
|
||||||
|
// The string is an encoded JSON string returned by Base64
|
||||||
|
func MustFromBase64(base64String string) Map {
|
||||||
|
result, err := FromBase64(base64String)
|
||||||
|
if err != nil {
|
||||||
|
panic("objx: MustFromBase64 failed with error: " + err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromSignedBase64 creates a new Obj containing the data specified
|
||||||
|
// in the Base64 string.
|
||||||
|
//
|
||||||
|
// The string is an encoded JSON string returned by SignedBase64
|
||||||
|
func FromSignedBase64(base64String, key string) (Map, error) {
|
||||||
|
parts := strings.Split(base64String, SignatureSeparator)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, errors.New("objx: Signed base64 string is malformed")
|
||||||
|
}
|
||||||
|
|
||||||
|
sig := HashWithKey(parts[0], key)
|
||||||
|
if parts[1] != sig {
|
||||||
|
return nil, errors.New("objx: Signature for base64 data does not match")
|
||||||
|
}
|
||||||
|
return FromBase64(parts[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustFromSignedBase64 creates a new Obj containing the data specified
|
||||||
|
// in the Base64 string and panics if there is an error.
|
||||||
|
//
|
||||||
|
// The string is an encoded JSON string returned by Base64
|
||||||
|
func MustFromSignedBase64(base64String, key string) Map {
|
||||||
|
result, err := FromSignedBase64(base64String, key)
|
||||||
|
if err != nil {
|
||||||
|
panic("objx: MustFromSignedBase64 failed with error: " + err.Error())
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromURLQuery generates a new Obj by parsing the specified
|
||||||
|
// query.
|
||||||
|
//
|
||||||
|
// For queries with multiple values, the first value is selected.
|
||||||
|
func FromURLQuery(query string) (Map, error) {
|
||||||
|
vals, err := url.ParseQuery(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
m := Map{}
|
||||||
|
for k, vals := range vals {
|
||||||
|
m[k] = vals[0]
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustFromURLQuery generates a new Obj by parsing the specified
|
||||||
|
// query.
|
||||||
|
//
|
||||||
|
// For queries with multiple values, the first value is selected.
|
||||||
|
//
|
||||||
|
// Panics if it encounters an error
|
||||||
|
func MustFromURLQuery(query string) Map {
|
||||||
|
o, err := FromURLQuery(query)
|
||||||
|
if err != nil {
|
||||||
|
panic("objx: MustFromURLQuery failed with error: " + err.Error())
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
// Exclude returns a new Map with the keys in the specified []string
|
||||||
|
// excluded.
|
||||||
|
func (m Map) Exclude(exclude []string) Map {
|
||||||
|
excluded := make(Map)
|
||||||
|
for k, v := range m {
|
||||||
|
if !contains(exclude, k) {
|
||||||
|
excluded[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return excluded
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy creates a shallow copy of the Obj.
|
||||||
|
func (m Map) Copy() Map {
|
||||||
|
copied := Map{}
|
||||||
|
for k, v := range m {
|
||||||
|
copied[k] = v
|
||||||
|
}
|
||||||
|
return copied
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge blends the specified map with a copy of this map and returns the result.
|
||||||
|
//
|
||||||
|
// Keys that appear in both will be selected from the specified map.
|
||||||
|
// This method requires that the wrapped object be a map[string]interface{}
|
||||||
|
func (m Map) Merge(merge Map) Map {
|
||||||
|
return m.Copy().MergeHere(merge)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MergeHere blends the specified map with this map and returns the current map.
|
||||||
|
//
|
||||||
|
// Keys that appear in both will be selected from the specified map. The original map
|
||||||
|
// will be modified. This method requires that
|
||||||
|
// the wrapped object be a map[string]interface{}
|
||||||
|
func (m Map) MergeHere(merge Map) Map {
|
||||||
|
for k, v := range merge {
|
||||||
|
m[k] = v
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform builds a new Obj giving the transformer a chance
|
||||||
|
// to change the keys and values as it goes. This method requires that
|
||||||
|
// the wrapped object be a map[string]interface{}
|
||||||
|
func (m Map) Transform(transformer func(key string, value interface{}) (string, interface{})) Map {
|
||||||
|
newMap := Map{}
|
||||||
|
for k, v := range m {
|
||||||
|
modifiedKey, modifiedVal := transformer(k, v)
|
||||||
|
newMap[modifiedKey] = modifiedVal
|
||||||
|
}
|
||||||
|
return newMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// TransformKeys builds a new map using the specified key mapping.
|
||||||
|
//
|
||||||
|
// Unspecified keys will be unaltered.
|
||||||
|
// This method requires that the wrapped object be a map[string]interface{}
|
||||||
|
func (m Map) TransformKeys(mapping map[string]string) Map {
|
||||||
|
return m.Transform(func(key string, value interface{}) (string, interface{}) {
|
||||||
|
if newKey, ok := mapping[key]; ok {
|
||||||
|
return newKey, value
|
||||||
|
}
|
||||||
|
return key, value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if a string slice contains a string
|
||||||
|
func contains(s []string, e string) bool {
|
||||||
|
for _, a := range s {
|
||||||
|
if a == e {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HashWithKey hashes the specified string using the security key
|
||||||
|
func HashWithKey(data, key string) string {
|
||||||
|
d := sha1.Sum([]byte(data + ":" + key))
|
||||||
|
return hex.EncodeToString(d[:])
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
// Has gets whether there is something at the specified selector
|
||||||
|
// or not.
|
||||||
|
//
|
||||||
|
// If m is nil, Has will always return false.
|
||||||
|
func (m Map) Has(selector string) bool {
|
||||||
|
if m == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return !m.Get(selector).IsNil()
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsNil gets whether the data is nil or not.
|
||||||
|
func (v *Value) IsNil() bool {
|
||||||
|
return v == nil || v.data == nil
|
||||||
|
}
|
|
@ -0,0 +1,346 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
/*
|
||||||
|
MSI (map[string]interface{} and []map[string]interface{})
|
||||||
|
*/
|
||||||
|
|
||||||
|
// MSI gets the value as a map[string]interface{}, returns the optionalDefault
|
||||||
|
// value or a system default object if the value is the wrong type.
|
||||||
|
func (v *Value) MSI(optionalDefault ...map[string]interface{}) map[string]interface{} {
|
||||||
|
if s, ok := v.data.(map[string]interface{}); ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
if s, ok := v.data.(Map); ok {
|
||||||
|
return map[string]interface{}(s)
|
||||||
|
}
|
||||||
|
if len(optionalDefault) == 1 {
|
||||||
|
return optionalDefault[0]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustMSI gets the value as a map[string]interface{}.
|
||||||
|
//
|
||||||
|
// Panics if the object is not a map[string]interface{}.
|
||||||
|
func (v *Value) MustMSI() map[string]interface{} {
|
||||||
|
if s, ok := v.data.(Map); ok {
|
||||||
|
return map[string]interface{}(s)
|
||||||
|
}
|
||||||
|
return v.data.(map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// MSISlice gets the value as a []map[string]interface{}, returns the optionalDefault
|
||||||
|
// value or nil if the value is not a []map[string]interface{}.
|
||||||
|
func (v *Value) MSISlice(optionalDefault ...[]map[string]interface{}) []map[string]interface{} {
|
||||||
|
if s, ok := v.data.([]map[string]interface{}); ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
s := v.ObjxMapSlice()
|
||||||
|
if s == nil {
|
||||||
|
if len(optionalDefault) == 1 {
|
||||||
|
return optionalDefault[0]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]map[string]interface{}, len(s))
|
||||||
|
for i := range s {
|
||||||
|
result[i] = s[i].Value().MSI()
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustMSISlice gets the value as a []map[string]interface{}.
|
||||||
|
//
|
||||||
|
// Panics if the object is not a []map[string]interface{}.
|
||||||
|
func (v *Value) MustMSISlice() []map[string]interface{} {
|
||||||
|
if s := v.MSISlice(); s != nil {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.data.([]map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMSI gets whether the object contained is a map[string]interface{} or not.
|
||||||
|
func (v *Value) IsMSI() bool {
|
||||||
|
_, ok := v.data.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
_, ok = v.data.(Map)
|
||||||
|
}
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsMSISlice gets whether the object contained is a []map[string]interface{} or not.
|
||||||
|
func (v *Value) IsMSISlice() bool {
|
||||||
|
_, ok := v.data.([]map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
_, ok = v.data.([]Map)
|
||||||
|
if !ok {
|
||||||
|
s, ok := v.data.([]interface{})
|
||||||
|
if ok {
|
||||||
|
for i := range s {
|
||||||
|
switch s[i].(type) {
|
||||||
|
case Map:
|
||||||
|
case map[string]interface{}:
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// EachMSI calls the specified callback for each object
|
||||||
|
// in the []map[string]interface{}.
|
||||||
|
//
|
||||||
|
// Panics if the object is the wrong type.
|
||||||
|
func (v *Value) EachMSI(callback func(int, map[string]interface{}) bool) *Value {
|
||||||
|
for index, val := range v.MustMSISlice() {
|
||||||
|
carryon := callback(index, val)
|
||||||
|
if !carryon {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// WhereMSI uses the specified decider function to select items
|
||||||
|
// from the []map[string]interface{}. The object contained in the result will contain
|
||||||
|
// only the selected items.
|
||||||
|
func (v *Value) WhereMSI(decider func(int, map[string]interface{}) bool) *Value {
|
||||||
|
var selected []map[string]interface{}
|
||||||
|
v.EachMSI(func(index int, val map[string]interface{}) bool {
|
||||||
|
shouldSelect := decider(index, val)
|
||||||
|
if !shouldSelect {
|
||||||
|
selected = append(selected, val)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: selected}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GroupMSI uses the specified grouper function to group the items
|
||||||
|
// keyed by the return of the grouper. The object contained in the
|
||||||
|
// result will contain a map[string][]map[string]interface{}.
|
||||||
|
func (v *Value) GroupMSI(grouper func(int, map[string]interface{}) string) *Value {
|
||||||
|
groups := make(map[string][]map[string]interface{})
|
||||||
|
v.EachMSI(func(index int, val map[string]interface{}) bool {
|
||||||
|
group := grouper(index, val)
|
||||||
|
if _, ok := groups[group]; !ok {
|
||||||
|
groups[group] = make([]map[string]interface{}, 0)
|
||||||
|
}
|
||||||
|
groups[group] = append(groups[group], val)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: groups}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceMSI uses the specified function to replace each map[string]interface{}s
|
||||||
|
// by iterating each item. The data in the returned result will be a
|
||||||
|
// []map[string]interface{} containing the replaced items.
|
||||||
|
func (v *Value) ReplaceMSI(replacer func(int, map[string]interface{}) map[string]interface{}) *Value {
|
||||||
|
arr := v.MustMSISlice()
|
||||||
|
replaced := make([]map[string]interface{}, len(arr))
|
||||||
|
v.EachMSI(func(index int, val map[string]interface{}) bool {
|
||||||
|
replaced[index] = replacer(index, val)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: replaced}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CollectMSI uses the specified collector function to collect a value
|
||||||
|
// for each of the map[string]interface{}s in the slice. The data returned will be a
|
||||||
|
// []interface{}.
|
||||||
|
func (v *Value) CollectMSI(collector func(int, map[string]interface{}) interface{}) *Value {
|
||||||
|
arr := v.MustMSISlice()
|
||||||
|
collected := make([]interface{}, len(arr))
|
||||||
|
v.EachMSI(func(index int, val map[string]interface{}) bool {
|
||||||
|
collected[index] = collector(index, val)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: collected}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
ObjxMap ((Map) and [](Map))
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ObjxMap gets the value as a (Map), returns the optionalDefault
|
||||||
|
// value or a system default object if the value is the wrong type.
|
||||||
|
func (v *Value) ObjxMap(optionalDefault ...(Map)) Map {
|
||||||
|
if s, ok := v.data.((Map)); ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
if s, ok := v.data.(map[string]interface{}); ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
if len(optionalDefault) == 1 {
|
||||||
|
return optionalDefault[0]
|
||||||
|
}
|
||||||
|
return New(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustObjxMap gets the value as a (Map).
|
||||||
|
//
|
||||||
|
// Panics if the object is not a (Map).
|
||||||
|
func (v *Value) MustObjxMap() Map {
|
||||||
|
if s, ok := v.data.(map[string]interface{}); ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return v.data.((Map))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjxMapSlice gets the value as a [](Map), returns the optionalDefault
|
||||||
|
// value or nil if the value is not a [](Map).
|
||||||
|
func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
|
||||||
|
if s, ok := v.data.([]Map); ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
if s, ok := v.data.([]map[string]interface{}); ok {
|
||||||
|
result := make([]Map, len(s))
|
||||||
|
for i := range s {
|
||||||
|
result[i] = s[i]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
s, ok := v.data.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
if len(optionalDefault) == 1 {
|
||||||
|
return optionalDefault[0]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]Map, len(s))
|
||||||
|
for i := range s {
|
||||||
|
switch s[i].(type) {
|
||||||
|
case Map:
|
||||||
|
result[i] = s[i].(Map)
|
||||||
|
case map[string]interface{}:
|
||||||
|
result[i] = New(s[i])
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustObjxMapSlice gets the value as a [](Map).
|
||||||
|
//
|
||||||
|
// Panics if the object is not a [](Map).
|
||||||
|
func (v *Value) MustObjxMapSlice() [](Map) {
|
||||||
|
if s := v.ObjxMapSlice(); s != nil {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
return v.data.([](Map))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsObjxMap gets whether the object contained is a (Map) or not.
|
||||||
|
func (v *Value) IsObjxMap() bool {
|
||||||
|
_, ok := v.data.((Map))
|
||||||
|
if !ok {
|
||||||
|
_, ok = v.data.(map[string]interface{})
|
||||||
|
}
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsObjxMapSlice gets whether the object contained is a [](Map) or not.
|
||||||
|
func (v *Value) IsObjxMapSlice() bool {
|
||||||
|
_, ok := v.data.([](Map))
|
||||||
|
if !ok {
|
||||||
|
_, ok = v.data.([]map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
s, ok := v.data.([]interface{})
|
||||||
|
if ok {
|
||||||
|
for i := range s {
|
||||||
|
switch s[i].(type) {
|
||||||
|
case Map:
|
||||||
|
case map[string]interface{}:
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// EachObjxMap calls the specified callback for each object
|
||||||
|
// in the [](Map).
|
||||||
|
//
|
||||||
|
// Panics if the object is the wrong type.
|
||||||
|
func (v *Value) EachObjxMap(callback func(int, Map) bool) *Value {
|
||||||
|
for index, val := range v.MustObjxMapSlice() {
|
||||||
|
carryon := callback(index, val)
|
||||||
|
if !carryon {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// WhereObjxMap uses the specified decider function to select items
|
||||||
|
// from the [](Map). The object contained in the result will contain
|
||||||
|
// only the selected items.
|
||||||
|
func (v *Value) WhereObjxMap(decider func(int, Map) bool) *Value {
|
||||||
|
var selected [](Map)
|
||||||
|
v.EachObjxMap(func(index int, val Map) bool {
|
||||||
|
shouldSelect := decider(index, val)
|
||||||
|
if !shouldSelect {
|
||||||
|
selected = append(selected, val)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: selected}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GroupObjxMap uses the specified grouper function to group the items
|
||||||
|
// keyed by the return of the grouper. The object contained in the
|
||||||
|
// result will contain a map[string][](Map).
|
||||||
|
func (v *Value) GroupObjxMap(grouper func(int, Map) string) *Value {
|
||||||
|
groups := make(map[string][](Map))
|
||||||
|
v.EachObjxMap(func(index int, val Map) bool {
|
||||||
|
group := grouper(index, val)
|
||||||
|
if _, ok := groups[group]; !ok {
|
||||||
|
groups[group] = make([](Map), 0)
|
||||||
|
}
|
||||||
|
groups[group] = append(groups[group], val)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: groups}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplaceObjxMap uses the specified function to replace each (Map)s
|
||||||
|
// by iterating each item. The data in the returned result will be a
|
||||||
|
// [](Map) containing the replaced items.
|
||||||
|
func (v *Value) ReplaceObjxMap(replacer func(int, Map) Map) *Value {
|
||||||
|
arr := v.MustObjxMapSlice()
|
||||||
|
replaced := make([](Map), len(arr))
|
||||||
|
v.EachObjxMap(func(index int, val Map) bool {
|
||||||
|
replaced[index] = replacer(index, val)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: replaced}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CollectObjxMap uses the specified collector function to collect a value
|
||||||
|
// for each of the (Map)s in the slice. The data returned will be a
|
||||||
|
// []interface{}.
|
||||||
|
func (v *Value) CollectObjxMap(collector func(int, Map) interface{}) *Value {
|
||||||
|
arr := v.MustObjxMapSlice()
|
||||||
|
collected := make([]interface{}, len(arr))
|
||||||
|
v.EachObjxMap(func(index int, val Map) bool {
|
||||||
|
collected[index] = collector(index, val)
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return &Value{data: collected}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,159 @@
|
||||||
|
package objx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Value provides methods for extracting interface{} data in various
|
||||||
|
// types.
|
||||||
|
type Value struct {
|
||||||
|
// data contains the raw data being managed by this Value
|
||||||
|
data interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data returns the raw data contained by this Value
|
||||||
|
func (v *Value) Data() interface{} {
|
||||||
|
return v.data
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the value always as a string
|
||||||
|
func (v *Value) String() string {
|
||||||
|
switch {
|
||||||
|
case v.IsNil():
|
||||||
|
return ""
|
||||||
|
case v.IsStr():
|
||||||
|
return v.Str()
|
||||||
|
case v.IsBool():
|
||||||
|
return strconv.FormatBool(v.Bool())
|
||||||
|
case v.IsFloat32():
|
||||||
|
return strconv.FormatFloat(float64(v.Float32()), 'f', -1, 32)
|
||||||
|
case v.IsFloat64():
|
||||||
|
return strconv.FormatFloat(v.Float64(), 'f', -1, 64)
|
||||||
|
case v.IsInt():
|
||||||
|
return strconv.FormatInt(int64(v.Int()), 10)
|
||||||
|
case v.IsInt8():
|
||||||
|
return strconv.FormatInt(int64(v.Int8()), 10)
|
||||||
|
case v.IsInt16():
|
||||||
|
return strconv.FormatInt(int64(v.Int16()), 10)
|
||||||
|
case v.IsInt32():
|
||||||
|
return strconv.FormatInt(int64(v.Int32()), 10)
|
||||||
|
case v.IsInt64():
|
||||||
|
return strconv.FormatInt(v.Int64(), 10)
|
||||||
|
case v.IsUint():
|
||||||
|
return strconv.FormatUint(uint64(v.Uint()), 10)
|
||||||
|
case v.IsUint8():
|
||||||
|
return strconv.FormatUint(uint64(v.Uint8()), 10)
|
||||||
|
case v.IsUint16():
|
||||||
|
return strconv.FormatUint(uint64(v.Uint16()), 10)
|
||||||
|
case v.IsUint32():
|
||||||
|
return strconv.FormatUint(uint64(v.Uint32()), 10)
|
||||||
|
case v.IsUint64():
|
||||||
|
return strconv.FormatUint(v.Uint64(), 10)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%#v", v.Data())
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringSlice returns the value always as a []string
|
||||||
|
func (v *Value) StringSlice(optionalDefault ...[]string) []string {
|
||||||
|
switch {
|
||||||
|
case v.IsStrSlice():
|
||||||
|
return v.MustStrSlice()
|
||||||
|
case v.IsBoolSlice():
|
||||||
|
slice := v.MustBoolSlice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatBool(iv)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsFloat32Slice():
|
||||||
|
slice := v.MustFloat32Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatFloat(float64(iv), 'f', -1, 32)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsFloat64Slice():
|
||||||
|
slice := v.MustFloat64Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatFloat(iv, 'f', -1, 64)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsIntSlice():
|
||||||
|
slice := v.MustIntSlice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatInt(int64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsInt8Slice():
|
||||||
|
slice := v.MustInt8Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatInt(int64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsInt16Slice():
|
||||||
|
slice := v.MustInt16Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatInt(int64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsInt32Slice():
|
||||||
|
slice := v.MustInt32Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatInt(int64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsInt64Slice():
|
||||||
|
slice := v.MustInt64Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatInt(iv, 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsUintSlice():
|
||||||
|
slice := v.MustUintSlice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatUint(uint64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsUint8Slice():
|
||||||
|
slice := v.MustUint8Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatUint(uint64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsUint16Slice():
|
||||||
|
slice := v.MustUint16Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatUint(uint64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsUint32Slice():
|
||||||
|
slice := v.MustUint32Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatUint(uint64(iv), 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
case v.IsUint64Slice():
|
||||||
|
slice := v.MustUint64Slice()
|
||||||
|
vals := make([]string, len(slice))
|
||||||
|
for i, iv := range slice {
|
||||||
|
vals[i] = strconv.FormatUint(iv, 10)
|
||||||
|
}
|
||||||
|
return vals
|
||||||
|
}
|
||||||
|
if len(optionalDefault) == 1 {
|
||||||
|
return optionalDefault[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return []string{}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
// Package mock provides a system by which it is possible to mock your objects
|
||||||
|
// and verify calls are happening as expected.
|
||||||
|
//
|
||||||
|
// # Example Usage
|
||||||
|
//
|
||||||
|
// The mock package provides an object, Mock, that tracks activity on another object. It is usually
|
||||||
|
// embedded into a test object as shown below:
|
||||||
|
//
|
||||||
|
// type MyTestObject struct {
|
||||||
|
// // add a Mock object instance
|
||||||
|
// mock.Mock
|
||||||
|
//
|
||||||
|
// // other fields go here as normal
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// When implementing the methods of an interface, you wire your functions up
|
||||||
|
// to call the Mock.Called(args...) method, and return the appropriate values.
|
||||||
|
//
|
||||||
|
// For example, to mock a method that saves the name and age of a person and returns
|
||||||
|
// the year of their birth or an error, you might write this:
|
||||||
|
//
|
||||||
|
// func (o *MyTestObject) SavePersonDetails(firstname, lastname string, age int) (int, error) {
|
||||||
|
// args := o.Called(firstname, lastname, age)
|
||||||
|
// return args.Int(0), args.Error(1)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// The Int, Error and Bool methods are examples of strongly typed getters that take the argument
|
||||||
|
// index position. Given this argument list:
|
||||||
|
//
|
||||||
|
// (12, true, "Something")
|
||||||
|
//
|
||||||
|
// You could read them out strongly typed like this:
|
||||||
|
//
|
||||||
|
// args.Int(0)
|
||||||
|
// args.Bool(1)
|
||||||
|
// args.String(2)
|
||||||
|
//
|
||||||
|
// For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:
|
||||||
|
//
|
||||||
|
// return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)
|
||||||
|
//
|
||||||
|
// This may cause a panic if the object you are getting is nil (the type assertion will fail), in those
|
||||||
|
// cases you should check for nil first.
|
||||||
|
package mock
|
File diff suppressed because it is too large
Load Diff
|
@ -945,9 +945,13 @@ github.com/status-im/zxcvbn-go/match
|
||||||
github.com/status-im/zxcvbn-go/matching
|
github.com/status-im/zxcvbn-go/matching
|
||||||
github.com/status-im/zxcvbn-go/scoring
|
github.com/status-im/zxcvbn-go/scoring
|
||||||
github.com/status-im/zxcvbn-go/utils/math
|
github.com/status-im/zxcvbn-go/utils/math
|
||||||
|
# github.com/stretchr/objx v0.5.0
|
||||||
|
## explicit; go 1.12
|
||||||
|
github.com/stretchr/objx
|
||||||
# github.com/stretchr/testify v1.8.4
|
# github.com/stretchr/testify v1.8.4
|
||||||
## explicit; go 1.20
|
## explicit; go 1.20
|
||||||
github.com/stretchr/testify/assert
|
github.com/stretchr/testify/assert
|
||||||
|
github.com/stretchr/testify/mock
|
||||||
github.com/stretchr/testify/require
|
github.com/stretchr/testify/require
|
||||||
github.com/stretchr/testify/suite
|
github.com/stretchr/testify/suite
|
||||||
# github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
|
# github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
|
||||||
|
|
Loading…
Reference in New Issue