-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshalingexample_test.go
56 lines (46 loc) · 1.09 KB
/
marshalingexample_test.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
package lisp
import (
"encoding/json"
"github.com/jig/lisp/lib/call"
"github.com/jig/lisp/types"
)
func LoadMarshalExample(ns types.EnvType) {
call.Call(ns, new_marshalexample)
}
type MarshalExample struct {
A int `json:"a"`
B string `json:"b"`
}
type LispMarshalExample struct {
Val MarshalExample
}
func (lec LispMarshalExample) MarshalHashMap() (types.MalType, error) {
return types.HashMap{
Val: map[string]types.MalType{
"ʞa": lec.Val.A,
"ʞb": lec.Val.B,
},
}, nil
}
type LispMarshalExampleFactory struct {
Type MarshalExample
}
func new_marshalexample() (types.MalType, error) {
return LispMarshalExampleFactory{}, nil
}
func (lec LispMarshalExampleFactory) FromHashMap(_hm types.MalType) (types.MalType, error) {
hm := _hm.(types.HashMap)
ex := MarshalExample{
A: hm.Val["ʞa"].(int),
B: hm.Val["ʞb"].(string),
}
return LispMarshalExample{ex}, nil
}
func (lec LispMarshalExampleFactory) FromJSON(b []byte) (types.MalType, error) {
if err := json.Unmarshal(b, &lec.Type); err != nil {
return nil, err
}
return LispMarshalExample{
Val: lec.Type,
}, nil
}