mirror of
https://github.com/rclone/rclone.git
synced 2025-12-21 18:53:34 +00:00
vendor: add qingstor-sdk-go for QingStor
This commit is contained in:
50
vendor/github.com/pengsrc/go-shared/json/json.go
generated
vendored
Normal file
50
vendor/github.com/pengsrc/go-shared/json/json.go
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// Encode encode given interface to json byte slice.
|
||||
func Encode(source interface{}, unescape bool) ([]byte, error) {
|
||||
bytesResult, err := json.Marshal(source)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
if unescape {
|
||||
bytesResult = bytes.Replace(bytesResult, []byte("\\u003c"), []byte("<"), -1)
|
||||
bytesResult = bytes.Replace(bytesResult, []byte("\\u003e"), []byte(">"), -1)
|
||||
bytesResult = bytes.Replace(bytesResult, []byte("\\u0026"), []byte("&"), -1)
|
||||
}
|
||||
|
||||
return bytesResult, nil
|
||||
}
|
||||
|
||||
// Decode decode given json byte slice to corresponding struct.
|
||||
func Decode(content []byte, destinations ...interface{}) (interface{}, error) {
|
||||
var destination interface{}
|
||||
var err error
|
||||
if len(destinations) == 1 {
|
||||
destination = destinations[0]
|
||||
err = json.Unmarshal(content, destination)
|
||||
} else {
|
||||
err = json.Unmarshal(content, &destination)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return destination, err
|
||||
}
|
||||
|
||||
// FormatToReadable formats given json byte slice prettily.
|
||||
func FormatToReadable(source []byte) ([]byte, error) {
|
||||
var out bytes.Buffer
|
||||
err := json.Indent(&out, source, "", " ") // Using 2 space indent
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
79
vendor/github.com/pengsrc/go-shared/json/json_test.go
generated
vendored
Normal file
79
vendor/github.com/pengsrc/go-shared/json/json_test.go
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestJSONDecodeUnknown(t *testing.T) {
|
||||
jsonString := `{
|
||||
"key1" : "This is a string.",
|
||||
"key2" : 10.50,
|
||||
"key3": [null, {"nestedKey1": "Another string"}]
|
||||
}`
|
||||
|
||||
anyData, err := Decode([]byte(jsonString))
|
||||
assert.NoError(t, err)
|
||||
data := anyData.(map[string]interface{})
|
||||
assert.Equal(t, 10.50, data["key2"])
|
||||
|
||||
var anotherData interface{}
|
||||
_, err = Decode([]byte(jsonString), &anotherData)
|
||||
assert.NoError(t, err)
|
||||
data = anyData.(map[string]interface{})
|
||||
assert.Equal(t, 10.50, data["key2"])
|
||||
|
||||
_, err = Decode([]byte(`- - -`), &JSONMustError{})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestJSONDecodeKnown(t *testing.T) {
|
||||
type SampleJSON struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
sampleJSONString := `{"name": "NAME"}`
|
||||
|
||||
sample := SampleJSON{Name: "NaMe", Description: "DeScRiPtIoN"}
|
||||
anyDataPointer, err := Decode([]byte(sampleJSONString), &sample)
|
||||
assert.NoError(t, err)
|
||||
data := anyDataPointer.(*SampleJSON)
|
||||
assert.Equal(t, "NAME", sample.Name)
|
||||
assert.Equal(t, "DeScRiPtIoN", sample.Description)
|
||||
assert.Equal(t, "NAME", (*data).Name)
|
||||
assert.Equal(t, "DeScRiPtIoN", (*data).Description)
|
||||
}
|
||||
|
||||
func TestJSONEncode(t *testing.T) {
|
||||
type SampleJSON struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
sample := SampleJSON{Name: "NaMe", Description: "DeScRiPtIoN"}
|
||||
|
||||
jsonBytes, err := Encode(sample, true)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, `{"name":"NaMe","description":"DeScRiPtIoN"}`, string(jsonBytes))
|
||||
|
||||
_, err = Encode(&JSONMustError{}, true)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestJSONFormatToReadable(t *testing.T) {
|
||||
sampleJSONString := `{"name": "NAME"}`
|
||||
|
||||
jsonBytes, err := FormatToReadable([]byte(sampleJSONString))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "{\n \"name\": \"NAME\"\n}", string(jsonBytes))
|
||||
|
||||
_, err = FormatToReadable([]byte(`XXXXX`))
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
type JSONMustError struct{}
|
||||
|
||||
func (*JSONMustError) MarshalJSON() ([]byte, error) {
|
||||
return []byte{}, errors.New("marshal error")
|
||||
}
|
||||
Reference in New Issue
Block a user