2016-02-22 14:44:12 -05:00
|
|
|
package godo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-04-08 15:52:57 -04:00
|
|
|
"io"
|
2016-02-22 14:44:12 -05:00
|
|
|
"reflect"
|
2018-11-21 12:04:55 -05:00
|
|
|
"strings"
|
2016-02-22 14:44:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var timestampType = reflect.TypeOf(Timestamp{})
|
|
|
|
|
2018-11-21 12:04:55 -05:00
|
|
|
type ResourceWithURN interface {
|
|
|
|
URN() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToURN converts the resource type and ID to a valid DO API URN.
|
|
|
|
func ToURN(resourceType string, id interface{}) string {
|
|
|
|
return fmt.Sprintf("%s:%s:%v", "do", strings.ToLower(resourceType), id)
|
|
|
|
}
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
// Stringify attempts to create a string representation of DigitalOcean types
|
|
|
|
func Stringify(message interface{}) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
v := reflect.ValueOf(message)
|
|
|
|
stringifyValue(&buf, v)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// stringifyValue was graciously cargoculted from the goprotubuf library
|
2017-04-08 15:52:57 -04:00
|
|
|
func stringifyValue(w io.Writer, val reflect.Value) {
|
2016-02-22 14:44:12 -05:00
|
|
|
if val.Kind() == reflect.Ptr && val.IsNil() {
|
|
|
|
_, _ = w.Write([]byte("<nil>"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
v := reflect.Indirect(val)
|
|
|
|
|
|
|
|
switch v.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
fmt.Fprintf(w, `"%s"`, v)
|
|
|
|
case reflect.Slice:
|
2017-04-08 15:52:57 -04:00
|
|
|
stringifySlice(w, v)
|
2016-02-22 14:44:12 -05:00
|
|
|
return
|
|
|
|
case reflect.Struct:
|
2017-04-08 15:52:57 -04:00
|
|
|
stringifyStruct(w, v)
|
|
|
|
default:
|
|
|
|
if v.CanInterface() {
|
|
|
|
fmt.Fprint(w, v.Interface())
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2017-04-08 15:52:57 -04:00
|
|
|
}
|
|
|
|
}
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
func stringifySlice(w io.Writer, v reflect.Value) {
|
|
|
|
_, _ = w.Write([]byte{'['})
|
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
if i > 0 {
|
|
|
|
_, _ = w.Write([]byte{' '})
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
stringifyValue(w, v.Index(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = w.Write([]byte{']'})
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyStruct(w io.Writer, v reflect.Value) {
|
|
|
|
if v.Type().Name() != "" {
|
|
|
|
_, _ = w.Write([]byte(v.Type().String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// special handling of Timestamp values
|
|
|
|
if v.Type() == timestampType {
|
|
|
|
fmt.Fprintf(w, "{%s}", v.Interface())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = w.Write([]byte{'{'})
|
|
|
|
|
|
|
|
var sep bool
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
fv := v.Field(i)
|
|
|
|
if fv.Kind() == reflect.Ptr && fv.IsNil() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fv.Kind() == reflect.Slice && fv.IsNil() {
|
|
|
|
continue
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
2017-04-08 15:52:57 -04:00
|
|
|
if sep {
|
|
|
|
_, _ = w.Write([]byte(", "))
|
|
|
|
} else {
|
|
|
|
sep = true
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2017-04-08 15:52:57 -04:00
|
|
|
|
|
|
|
_, _ = w.Write([]byte(v.Type().Field(i).Name))
|
|
|
|
_, _ = w.Write([]byte{':'})
|
|
|
|
stringifyValue(w, fv)
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2017-04-08 15:52:57 -04:00
|
|
|
|
|
|
|
_, _ = w.Write([]byte{'}'})
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|