initial commit [ci skip]
This commit is contained in:
56
engine/compiler/encoder/encoder.go
Normal file
56
engine/compiler/encoder/encoder.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Polyform License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package encoder
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/buildkite/yaml"
|
||||
json "github.com/ghodss/yaml"
|
||||
)
|
||||
|
||||
// Encode encodes an interface value as a string. This function
|
||||
// assumes all types were unmarshaled by the yaml.v2 library.
|
||||
// The yaml.v2 package only supports a subset of primative types.
|
||||
func Encode(v interface{}) string {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return v
|
||||
case bool:
|
||||
return strconv.FormatBool(v)
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'g', -1, 64)
|
||||
case []byte:
|
||||
return base64.StdEncoding.EncodeToString(v)
|
||||
case []interface{}:
|
||||
return encodeSlice(v)
|
||||
default:
|
||||
return encodeMap(v)
|
||||
}
|
||||
}
|
||||
|
||||
// helper function encodes a parameter in map format.
|
||||
func encodeMap(v interface{}) string {
|
||||
yml, _ := yaml.Marshal(v)
|
||||
out, _ := json.YAMLToJSON(yml)
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// helper function encodes a parameter in slice format.
|
||||
func encodeSlice(v interface{}) string {
|
||||
out, _ := yaml.Marshal(v)
|
||||
|
||||
in := []string{}
|
||||
err := yaml.Unmarshal(out, &in)
|
||||
if err == nil {
|
||||
return strings.Join(in, ",")
|
||||
}
|
||||
out, _ = json.YAMLToJSON(out)
|
||||
return string(out)
|
||||
}
|
||||
63
engine/compiler/encoder/encoder_test.go
Normal file
63
engine/compiler/encoder/encoder_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Polyform License
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package encoder
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
testdatum := []struct {
|
||||
data interface{}
|
||||
text string
|
||||
}{
|
||||
{
|
||||
data: "foo",
|
||||
text: "foo",
|
||||
},
|
||||
{
|
||||
data: true,
|
||||
text: "true",
|
||||
},
|
||||
{
|
||||
data: 42,
|
||||
text: "42",
|
||||
},
|
||||
{
|
||||
data: float64(42.424242),
|
||||
text: "42.424242",
|
||||
},
|
||||
{
|
||||
data: []interface{}{"foo", "bar", "baz"},
|
||||
text: "foo,bar,baz",
|
||||
},
|
||||
{
|
||||
data: []interface{}{1, 1, 2, 3, 5, 8},
|
||||
text: "1,1,2,3,5,8",
|
||||
},
|
||||
{
|
||||
data: []byte("foo"),
|
||||
text: "Zm9v",
|
||||
},
|
||||
{
|
||||
data: []interface{}{
|
||||
struct {
|
||||
Name string `json:"name"`
|
||||
}{
|
||||
Name: "john",
|
||||
},
|
||||
},
|
||||
text: `[{"name":"john"}]`,
|
||||
},
|
||||
{
|
||||
data: map[interface{}]interface{}{"foo": "bar"},
|
||||
text: `{"foo":"bar"}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testdata := range testdatum {
|
||||
if got, want := Encode(testdata.data), testdata.text; got != want {
|
||||
t.Errorf("Want interface{} encoded to %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user