-
Notifications
You must be signed in to change notification settings - Fork 1
/
vom.go
72 lines (58 loc) · 1.35 KB
/
vom.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
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package miniv
// #cgo CFLAGS: -I/usr/include/nacl -std=c99
// #include "miniv.h"
// #include "teststruct.h"
import "C"
import (
"errors"
"v.io/v23/uniqueid"
)
var ErrNotEqual = errors.New("not equal")
type inner struct {
String string
}
type testStruct struct {
A int
B float64
Inner inner
}
func decodeStruct(in []byte) (interface{}, error) {
var zero testStruct
C.v23_uniqueid_register()
C.testStruct_register()
var d C.decoder_t
var v C.value_t
defer C.valueDealloc(&v)
var done C._Bool
C.decoderFeed(&d, toBuf_t(in))
err := C.vomDecode(&d, &v, &done)
if err != C.ERR_OK {
return zero, GoError(err)
}
if v.ptr == nil {
return zero, errors.New("expected v.ptr not nil")
}
if v.typ == nil {
return zero, errors.New("expected v.typ not nil")
}
typ := bufToString(v.typ.tname)
switch typ {
case "github.com/jeffallen/miniv.testStruct":
ts := (*C.struct_testStruct)((v.ptr))
return testStruct{
A: int(ts.A),
B: float64(ts.B),
Inner: inner{String: bufToString(ts.Inner.String)},
}, nil
case "v.io/v23/uniqueid.Id":
var id uniqueid.Id
x := C.GoStringN((*C.char)(v.ptr), 16)
copy(id[:], x)
return id, nil
default:
return nil, nil
}
}