-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgpack.go
54 lines (50 loc) · 1.26 KB
/
msgpack.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
package builtin_libs
import (
"reflect"
. "github.com/zgg-lang/zgg-go/runtime"
"github.com/vmihailenco/msgpack"
)
func libMsgpack(*Context) ValueObject {
lib := NewObject()
lib.SetMember("encode", NewNativeFunction(
"msgpack.encode",
func(c *Context, this Value, args []Value) Value {
if len(args) != 1 {
c.RaiseRuntimeError("msgpack.encode: requires 1 argument")
return nil
}
bs, err := msgpack.Marshal(args[0].ToGoValue())
if err != nil {
c.RaiseRuntimeError("msgpack.encode: " + err.Error())
return nil
}
return NewBytes(bs)
},
), nil)
lib.SetMember("decode", NewNativeFunction(
"msgpack.decode",
func(c *Context, this Value, args []Value) Value {
if len(args) != 1 {
c.RaiseRuntimeError("msgpack.decode: requires 1 argument")
return nil
}
var bs []byte
switch arg := args[0].(type) {
case ValueStr:
bs = []byte(arg.Value())
case ValueBytes:
bs = arg.Value()
default:
c.RaiseRuntimeError("msgpack.decode: argument must be a string or a bytes")
return nil
}
var j interface{}
if err := msgpack.Unmarshal(bs, &j); err != nil {
c.RaiseRuntimeError("msgpack.decode: " + err.Error())
return nil
}
return FromGoValue(reflect.ValueOf(j), c)
},
), nil)
return lib
}