-
Notifications
You must be signed in to change notification settings - Fork 0
/
codecs.go
83 lines (65 loc) · 1.73 KB
/
codecs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package adversarylab
import (
"fmt"
"reflect"
"github.com/ugorji/go/codec"
)
type Named interface {
Name() string
}
type NamedType struct {
Name string
Value interface{}
}
type RawNamedType struct {
Name string
Value interface{}
}
type NamedTypeExt struct{}
func NamedTypeHandle() codec.Handle {
var h *codec.CborHandle = new(codec.CborHandle)
namedType := reflect.TypeOf(NamedType{})
var namedTypeExt NamedTypeExt
h.SetExt(namedType, 78, namedTypeExt)
return h
}
func RawNamedTypeHandle() codec.Handle {
var h *codec.CborHandle = new(codec.CborHandle)
namedType := reflect.TypeOf(RawNamedType{})
var namedTypeExt NamedTypeExt
h.SetExt(namedType, 78, namedTypeExt)
return h
}
func (x NamedTypeExt) WriteExt(interface{}) []byte {
panic("unsupported")
}
func (x NamedTypeExt) ReadExt(interface{}, []byte) {
panic("unsupported")
}
// From NamedWrapper to NamedType
func (x NamedTypeExt) ConvertExt(v interface{}) interface{} {
// fmt.Println("Converting Ext")
switch v.(type) {
case NamedType:
var nt = v.(NamedType)
return RawNamedType{Name: nt.Name, Value: nt.Value}
// case *Named:
// var named Named = *v
// return NamedType{Name: named.Name(), Value: named}
default:
panic(fmt.Sprintf("unsupported format for named type conversion: expecting NamedType; got %T", v))
}
}
// From NamedType to NamedType
func (x NamedTypeExt) UpdateExt(dest interface{}, v interface{}) {
// fmt.Println("Updating Ext")
ret := dest.(*NamedType)
switch v.(type) {
case map[interface{}]interface{}:
rnt := v.(map[interface{}]interface{})
nt := NamedType{Name: rnt["Name"].(string), Value: rnt["Value"]}
*ret = nt
default:
panic(fmt.Sprintf("unsupported format for named type update: expecting RawNamedType; got %T", v))
}
}