-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisc_builtin_etcd.go
153 lines (141 loc) · 4.48 KB
/
isc_builtin_etcd.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package goscript
import (
c0 "context"
"fmt"
clientv3 "go.etcd.io/etcd/client/v3"
"time"
)
type etcdObject struct {
baseObject
cli *clientv3.Client
}
func (r *Runtime) builtinEtcd_close(call FunctionCall) Value {
thisObj := r.toObject(call.This)
eo, ok := thisObj.self.(*etcdObject)
if !ok {
panic(r.NewTypeError("Method Etcd.prototype.close called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
_ = eo.cli.Close()
return _undefined
}
func (r *Runtime) builtinEtcd_get(call FunctionCall) Value {
thisObj := r.toObject(call.This)
eo, ok := thisObj.self.(*etcdObject)
if !ok {
panic(r.NewTypeError("Method Etcd.prototype.get called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
gr, err := eo.cli.Get(c0.Background(), call.Argument(0).toString().String(), clientv3.WithPrefix())
if err != nil {
return _null
} else {
// 返回一个 KV 数组
var ret0 []map[string]string
for _, item := range gr.Kvs {
_m := map[string]string{
"key": string(item.Key),
"value": string(item.Value),
}
ret0 = append(ret0, _m)
}
return r.ToValue(ret0)
}
}
func (r *Runtime) builtinEtcd_put(call FunctionCall) Value {
thisObj := r.toObject(call.This)
eo, ok := thisObj.self.(*etcdObject)
if !ok {
panic(r.NewTypeError("Method Etcd.prototype.put called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
_key := call.Argument(0).toString().String()
_val := call.Argument(1).toString().String()
_, err := eo.cli.Put(c0.Background(), _key, _val)
if err != nil {
return valueFalse
} else {
return r.toBoolean(err == nil)
}
}
func (r *Runtime) builtinEtcd_delete(call FunctionCall) Value {
thisObj := r.toObject(call.This)
eo, ok := thisObj.self.(*etcdObject)
if !ok {
panic(r.NewTypeError("Method Etcd.prototype.delete called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
_, err := eo.cli.Delete(c0.Background(), call.Argument(0).toString().String())
if err != nil {
return valueFalse
} else {
return r.toBoolean(err == nil)
}
}
func (r *Runtime) builtinEtcd_sync(call FunctionCall) Value {
thisObj := r.toObject(call.This)
eo, ok := thisObj.self.(*etcdObject)
if !ok {
panic(r.NewTypeError("Method Etcd.prototype.sync called on incompatible receiver %s", r.objectproto_toString(FunctionCall{This: thisObj})))
}
err := eo.cli.Sync(c0.Background())
if err != nil {
return valueFalse
} else {
return r.toBoolean(err == nil)
}
}
func (r *Runtime) builtin_newEtcd(args []Value, newTarget *Object) *Object {
if newTarget == nil {
panic(r.needNew("Etcd"))
}
if len(args) != 4 {
panic("number of arguments must be 4")
}
// 连接 etcd
_host := args[0].toString().String()
_port := args[1].ToInteger()
_user := args[2].toString().String()
_password := args[3].toString().String()
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{fmt.Sprintf("%s:%d", _host, _port)},
Username: _user,
Password: _password,
DialTimeout: 5 * time.Second,
})
if err != nil {
return nil
} else {
proto := r.getPrototypeFromCtor(newTarget, r.global.Etcd, r.global.EtcdPrototype)
o := &Object{runtime: r}
eo := &etcdObject{
baseObject: baseObject{
class: classEtcd,
val: o,
prototype: proto,
extensible: true,
values: nil,
},
cli: cli,
}
o.self = eo
eo.init()
return o
}
}
func (r *Runtime) createEtcdProto(val *Object) objectImpl {
o := newBaseObjectObj(val, r.global.ObjectPrototype, classObject)
o._putProp("constructor", r.global.Etcd, true, false, true)
o._putProp("close", r.newNativeFunc(r.builtinEtcd_close, nil, "close", nil, 0), true, false, true)
o._putProp("get", r.newNativeFunc(r.builtinEtcd_get, nil, "get", nil, 1), true, false, true)
o._putProp("put", r.newNativeFunc(r.builtinEtcd_put, nil, "put", nil, 2), true, false, true)
o._putProp("delete", r.newNativeFunc(r.builtinEtcd_delete, nil, "delete", nil, 1), true, false, true)
o._putProp("sync", r.newNativeFunc(r.builtinEtcd_sync, nil, "sync", nil, 0), true, false, true)
o._putSym(SymToStringTag, valueProp(asciiString(classEtcd), false, false, true))
return o
}
func (r *Runtime) createEtcd(val *Object) objectImpl {
o := r.newNativeConstructOnly(val, r.builtin_newEtcd, r.global.EtcdPrototype, "Etcd", 4)
return o
}
func (r *Runtime) initEtcd() {
r.global.EtcdPrototype = r.newLazyObject(r.createEtcdProto)
r.global.Etcd = r.newLazyObject(r.createEtcd)
r.addToGlobal("Etcd", r.global.Etcd)
}