2019-11-05 22:35:45 +00:00
|
|
|
package sii
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func genericMarshal(in interface{}) ([]byte, error) {
|
2019-12-10 22:20:25 +00:00
|
|
|
if reflect.TypeOf(in).Kind() == reflect.Ptr {
|
|
|
|
in = reflect.ValueOf(in).Elem().Interface()
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 22:20:25 +00:00
|
|
|
if reflect.TypeOf(in).Kind() != reflect.Struct {
|
|
|
|
return nil, errors.New("Calling marshaller with non-struct")
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 22:20:25 +00:00
|
|
|
var buf = new(bytes.Buffer)
|
|
|
|
|
|
|
|
st := reflect.ValueOf(in)
|
2019-11-05 22:35:45 +00:00
|
|
|
for i := 0; i < st.NumField(); i++ {
|
|
|
|
valField := st.Field(i)
|
|
|
|
typeField := st.Type().Field(i)
|
|
|
|
|
|
|
|
attributeName := typeField.Tag.Get("sii")
|
|
|
|
if attributeName == "" {
|
|
|
|
// Names must be explicitly defined, library does not support name guessing
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typeField.Type {
|
|
|
|
|
|
|
|
case reflect.TypeOf(Ptr{}):
|
2019-12-10 22:20:25 +00:00
|
|
|
v := valField.Interface().(Ptr).MarshalSII()
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
|
2019-11-05 22:35:45 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
case reflect.TypeOf(Placement{}):
|
2019-12-10 22:20:25 +00:00
|
|
|
v, err := valField.Interface().(Placement).MarshalSII()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode Placement")
|
2019-11-10 12:41:24 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
|
2019-11-05 22:35:45 +00:00
|
|
|
continue
|
|
|
|
|
2019-12-11 17:24:12 +00:00
|
|
|
case reflect.TypeOf(RawValue{}):
|
|
|
|
v, err := valField.Interface().(RawValue).MarshalSII()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode RawValue")
|
|
|
|
}
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
|
|
|
|
continue
|
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch typeField.Type.Kind() {
|
2019-12-10 22:20:25 +00:00
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
case reflect.Bool:
|
2019-12-10 22:20:25 +00:00
|
|
|
v := valField.Bool()
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, strconv.FormatBool(v)))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
2019-11-10 12:59:37 +00:00
|
|
|
case reflect.Float32:
|
2019-12-10 22:20:25 +00:00
|
|
|
v, err := float2sii(float32(valField.Float()))
|
2019-11-05 22:35:45 +00:00
|
|
|
if err != nil {
|
2019-12-10 22:20:25 +00:00
|
|
|
return nil, errors.Wrap(err, "Unable to encode float32")
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
|
|
|
case reflect.Int, reflect.Int64:
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %d\n", attributeName, valField.Int()))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
|
|
|
case reflect.String:
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %q\n", attributeName, valField.String()))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
|
|
|
case reflect.Uint64:
|
2019-12-10 22:20:25 +00:00
|
|
|
v := strconv.FormatUint(valField.Uint(), 10)
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
|
|
|
|
|
2019-12-11 17:24:12 +00:00
|
|
|
case reflect.Array:
|
|
|
|
|
|
|
|
switch typeField.Type.Elem().Kind() {
|
|
|
|
|
|
|
|
case reflect.Float32:
|
|
|
|
var vals [][]byte
|
|
|
|
|
|
|
|
switch typeField.Type.Len() {
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
for _, v := range valField.Interface().([3]float32) {
|
|
|
|
bv, err := float2sii(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode float32")
|
|
|
|
}
|
|
|
|
vals = append(vals, bv)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("Unsupported type: [%d]%s", typeField.Type.Len(), typeField.Type.Elem().Kind())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s: (%s)\n", attributeName, bytes.Join(vals, []byte(", "))))
|
|
|
|
|
2019-12-23 20:32:10 +00:00
|
|
|
case reflect.Int64:
|
|
|
|
var vals [][]byte
|
|
|
|
|
|
|
|
switch typeField.Type.Len() {
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
for _, v := range valField.Interface().([3]int64) {
|
|
|
|
bv := []byte(strconv.FormatInt(v, 10))
|
|
|
|
vals = append(vals, bv)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("Unsupported type: [%d]%s", typeField.Type.Len(), typeField.Type.Elem().Kind())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s: (%s)\n", attributeName, bytes.Join(vals, []byte(", "))))
|
|
|
|
|
2019-12-11 17:24:12 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("Unsupported type: [%d]%s", typeField.Type.Len(), typeField.Type.Elem().Kind())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-10 22:20:25 +00:00
|
|
|
case reflect.Ptr:
|
|
|
|
|
|
|
|
switch typeField.Type.Elem().Kind() {
|
|
|
|
|
|
|
|
case reflect.Int64:
|
|
|
|
var v string
|
|
|
|
if valField.IsNil() {
|
|
|
|
v = "nil"
|
|
|
|
} else {
|
|
|
|
v = strconv.FormatInt(valField.Elem().Int(), 10)
|
|
|
|
}
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s: %s\n", attributeName, v))
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("Unsupported type: *%s", typeField.Type.Elem().Kind())
|
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case reflect.Slice:
|
2019-12-10 22:20:25 +00:00
|
|
|
var values []string
|
2019-11-05 22:35:45 +00:00
|
|
|
|
2019-11-10 12:59:37 +00:00
|
|
|
switch typeField.Type.Elem() {
|
|
|
|
|
|
|
|
case reflect.TypeOf(Ptr{}):
|
2019-12-10 22:20:25 +00:00
|
|
|
for _, val := range valField.Interface().([]Ptr) {
|
|
|
|
values = append(values, string(val.MarshalSII()))
|
2019-11-10 12:59:37 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
2019-11-10 12:59:37 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
case reflect.TypeOf(Placement{}):
|
2019-12-10 22:20:25 +00:00
|
|
|
for _, val := range valField.Interface().([]Placement) {
|
|
|
|
ev, err := val.MarshalSII()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode Placement")
|
2019-11-10 14:49:54 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
values = append(values, string(ev))
|
2019-11-10 14:49:54 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
2019-11-10 14:49:54 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
case reflect.TypeOf(RawValue{}):
|
2019-12-10 22:20:25 +00:00
|
|
|
for _, val := range valField.Interface().([]RawValue) {
|
|
|
|
ev, err := val.MarshalSII()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode RawValue")
|
2019-11-10 12:59:37 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
values = append(values, string(ev))
|
2019-11-10 12:59:37 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
2019-11-10 12:59:37 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
switch typeField.Type.Elem().Kind() {
|
2019-12-10 22:20:25 +00:00
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
case reflect.Bool:
|
2019-12-10 22:20:25 +00:00
|
|
|
for _, val := range valField.Interface().([]bool) {
|
|
|
|
values = append(values, strconv.FormatBool(val))
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
2019-12-11 16:16:48 +00:00
|
|
|
case reflect.Float32:
|
|
|
|
for _, val := range valField.Interface().([]float32) {
|
|
|
|
v, err := float2sii(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode float32")
|
|
|
|
}
|
|
|
|
values = append(values, string(v))
|
|
|
|
}
|
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
case reflect.Int:
|
2019-12-10 22:20:25 +00:00
|
|
|
for _, val := range valField.Interface().([]int) {
|
|
|
|
values = append(values, strconv.FormatInt(int64(val), 10))
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
2019-11-10 12:59:37 +00:00
|
|
|
case reflect.Int64:
|
2019-12-10 22:20:25 +00:00
|
|
|
for _, val := range valField.Interface().([]int64) {
|
|
|
|
values = append(values, strconv.FormatInt(val, 10))
|
2019-11-10 12:59:37 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
2019-11-10 12:59:37 +00:00
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
case reflect.String:
|
2019-12-10 22:20:25 +00:00
|
|
|
for _, val := range valField.Interface().([]string) {
|
2019-12-11 16:17:12 +00:00
|
|
|
values = append(values, fmt.Sprintf("%q", val))
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
2019-12-11 18:21:49 +00:00
|
|
|
case reflect.Array:
|
|
|
|
|
|
|
|
switch typeField.Type.Elem().Elem().Kind() {
|
|
|
|
|
|
|
|
case reflect.Float32:
|
|
|
|
switch typeField.Type.Elem().Len() {
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
for _, val := range valField.Interface().([][3]float32) {
|
|
|
|
var vals [][]byte
|
|
|
|
for _, v := range val {
|
|
|
|
bv, err := float2sii(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode float32")
|
|
|
|
}
|
|
|
|
vals = append(vals, bv)
|
|
|
|
}
|
|
|
|
values = append(values, fmt.Sprintf("(%s)", bytes.Join(vals, []byte(", "))))
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
for _, val := range valField.Interface().([][4]float32) {
|
|
|
|
var vals [][]byte
|
|
|
|
for _, v := range val {
|
|
|
|
bv, err := float2sii(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to encode float32")
|
|
|
|
}
|
|
|
|
vals = append(vals, bv)
|
|
|
|
}
|
|
|
|
values = append(values, fmt.Sprintf("(%s; %s)", vals[0], bytes.Join(vals[1:], []byte(", "))))
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("Unsupported len of type: [][%d]%s", typeField.Type.Elem().Len(), typeField.Type.Elem().Elem().Kind())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("Unsupported type: [][%d]%s", typeField.Type.Elem().Len(), typeField.Type.Elem().Elem().Kind())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.Write(encodeSliceValue(attributeName, values))
|
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
default:
|
2019-12-10 22:20:25 +00:00
|
|
|
return nil, errors.Errorf("Unsupported type: []%s", typeField.Type.Elem().Kind())
|
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-12-10 22:20:25 +00:00
|
|
|
return nil, errors.Errorf("Unsupported type: %s", typeField.Type.Kind())
|
2019-11-05 22:35:45 +00:00
|
|
|
|
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
2019-12-10 22:20:25 +00:00
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 22:20:25 +00:00
|
|
|
func encodeSliceValue(attributeName string, values []string) []byte {
|
|
|
|
var buf = new(bytes.Buffer)
|
2019-11-05 22:35:45 +00:00
|
|
|
|
2019-12-10 22:20:25 +00:00
|
|
|
fmt.Fprintf(buf, " %s: %d\n", attributeName, len(values))
|
2019-11-05 22:35:45 +00:00
|
|
|
|
2019-12-10 22:20:25 +00:00
|
|
|
for i, v := range values {
|
|
|
|
fmt.Fprintf(buf, " %s[%d]: %s\n", attributeName, i, v)
|
2019-11-05 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 22:20:25 +00:00
|
|
|
return buf.Bytes()
|
2019-11-10 14:49:54 +00:00
|
|
|
}
|