253 lines
6.3 KiB
Go
253 lines
6.3 KiB
Go
package protocol
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
commonv1 "code.icb4dc0.de/buildr/api/generated/common/v1"
|
|
rpcv1 "code.icb4dc0.de/buildr/api/generated/common/v1"
|
|
)
|
|
|
|
var ErrExpectedStruct = errors.New("expected struct")
|
|
|
|
type marshalConfigOption interface {
|
|
applyToMarshalConfig(cfg *marshalConfig)
|
|
}
|
|
|
|
func Marshal(in any, opts ...marshalConfigOption) (*commonv1.ModuleSpec, error) {
|
|
cfg := marshalConfig{
|
|
protocolConfig: defaultConfig(),
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt.applyToMarshalConfig(&cfg)
|
|
}
|
|
|
|
val := reflect.ValueOf(in)
|
|
if val.Kind() == reflect.Ptr {
|
|
val = val.Elem()
|
|
}
|
|
|
|
if val.Kind() != reflect.Struct {
|
|
return nil, fmt.Errorf("%w: got %T", ErrExpectedStruct, in)
|
|
}
|
|
|
|
return cfg.marshal(val)
|
|
}
|
|
|
|
type marshalConfig struct {
|
|
protocolConfig
|
|
}
|
|
|
|
func (cfg marshalConfig) marshal(in reflect.Value) (*rpcv1.ModuleSpec, error) {
|
|
numField := in.NumField()
|
|
|
|
out := &rpcv1.ModuleSpec{
|
|
Values: make(map[string]*rpcv1.ModuleSpec_Value, numField),
|
|
}
|
|
|
|
inputType := in.Type()
|
|
|
|
for i := 0; i < numField; i++ {
|
|
structField := inputType.Field(i)
|
|
if !structField.IsExported() {
|
|
continue
|
|
}
|
|
|
|
fieldName := cfg.fieldName(structField)
|
|
|
|
out.Values[fieldName] = cfg.mapReflectValueToSpecValue(in.Field(i))
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func (cfg marshalConfig) mapReflectValueToSpecValue(in reflect.Value) *rpcv1.ModuleSpec_Value {
|
|
switch in.Kind() {
|
|
case reflect.Map:
|
|
return cfg.mapValue(in)
|
|
case reflect.Bool:
|
|
return boolValue(in.Bool())
|
|
case reflect.String:
|
|
return stringValue(in.String())
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return intValue(in.Int())
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return intValue(in.Uint())
|
|
case reflect.Float32, reflect.Float64:
|
|
return floatValue(in.Float())
|
|
case reflect.Struct:
|
|
return cfg.structValue(in)
|
|
case reflect.Slice, reflect.Array:
|
|
switch in.Type().Elem().Kind() {
|
|
case reflect.Bool:
|
|
return boolSliceValue(in)
|
|
case reflect.String:
|
|
return stringSliceValue(in)
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
return intSliceValue(in, reflect.Value.Int)
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
return intSliceValue(in, func(v reflect.Value) int64 {
|
|
return int64(v.Uint())
|
|
})
|
|
case reflect.Float32, reflect.Float64:
|
|
return floatSliceValue(in)
|
|
default:
|
|
return nil
|
|
}
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (cfg marshalConfig) fieldName(field reflect.StructField) string {
|
|
tagVal, ok := field.Tag.Lookup(cfg.TagName)
|
|
if !ok {
|
|
return field.Name
|
|
}
|
|
|
|
split := strings.Split(tagVal, ",")
|
|
|
|
switch {
|
|
case len(split) == 0:
|
|
fallthrough
|
|
case split[0] == "":
|
|
return strings.ToLower(field.Name)
|
|
default:
|
|
return strings.ToLower(split[0])
|
|
}
|
|
}
|
|
|
|
func (cfg marshalConfig) mapValue(in reflect.Value) *rpcv1.ModuleSpec_Value {
|
|
result := &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeMap,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
ComplexValue: make(map[string]*rpcv1.ModuleSpec_Value, in.Len()),
|
|
}
|
|
|
|
iter := in.MapRange()
|
|
for iter.Next() {
|
|
result.ComplexValue[iter.Key().String()] = cfg.mapReflectValueToSpecValue(iter.Value())
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func (cfg marshalConfig) structValue(in reflect.Value) *rpcv1.ModuleSpec_Value {
|
|
inputType := in.Type()
|
|
numFields := inputType.NumField()
|
|
result := &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeObject,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
ComplexValue: make(map[string]*rpcv1.ModuleSpec_Value, numFields),
|
|
}
|
|
|
|
for i := 0; i < numFields; i++ {
|
|
structField := inputType.Field(i)
|
|
if !structField.IsExported() {
|
|
continue
|
|
}
|
|
|
|
result.ComplexValue[structField.Name] = cfg.mapReflectValueToSpecValue(in.Field(i))
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func boolValue(in bool) *rpcv1.ModuleSpec_Value {
|
|
return &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeSingle,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
SingleValue: &rpcv1.ModuleSpec_Value_BoolValue{
|
|
BoolValue: in,
|
|
},
|
|
}
|
|
}
|
|
|
|
func boolSliceValue(val reflect.Value) *rpcv1.ModuleSpec_Value {
|
|
result := &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeBoolSlice,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
BoolValues: make([]bool, 0, val.Len()),
|
|
}
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
result.BoolValues = append(result.BoolValues, val.Index(i).Bool())
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func intValue[T Integer](in T) *rpcv1.ModuleSpec_Value {
|
|
return &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeSingle,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
SingleValue: &rpcv1.ModuleSpec_Value_IntValue{
|
|
IntValue: int64(in),
|
|
},
|
|
}
|
|
}
|
|
|
|
func intSliceValue(val reflect.Value, selector func(v reflect.Value) int64) *rpcv1.ModuleSpec_Value {
|
|
result := &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeIntSlice,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
IntValues: make([]int64, 0, val.Len()),
|
|
}
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
result.IntValues = append(result.IntValues, selector(val.Index(i)))
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func floatValue[T Float](in T) *rpcv1.ModuleSpec_Value {
|
|
return &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeSingle,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
SingleValue: &rpcv1.ModuleSpec_Value_DoubleValue{
|
|
DoubleValue: float64(in),
|
|
},
|
|
}
|
|
}
|
|
|
|
func floatSliceValue(val reflect.Value) *rpcv1.ModuleSpec_Value {
|
|
result := &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeDoubleSlice,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
DoubleValues: make([]float64, 0, val.Len()),
|
|
}
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
result.DoubleValues = append(result.DoubleValues, val.Index(i).Float())
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
func stringValue(in string) *rpcv1.ModuleSpec_Value {
|
|
return &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeSingle,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
SingleValue: &rpcv1.ModuleSpec_Value_StringValue{
|
|
StringValue: in,
|
|
},
|
|
}
|
|
}
|
|
|
|
func stringSliceValue(val reflect.Value) *rpcv1.ModuleSpec_Value {
|
|
result := &rpcv1.ModuleSpec_Value{
|
|
Type: rpcv1.ModuleSpec_ValueTypeStringSlice,
|
|
Kind: rpcv1.ModuleSpec_ValueKindAttribute,
|
|
StringValues: make([]string, 0, val.Len()),
|
|
}
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
result.StringValues = append(result.StringValues, val.Index(i).String())
|
|
}
|
|
|
|
return result
|
|
}
|