/
database_redis_hget.go
106 lines (97 loc) · 2.19 KB
/
database_redis_hget.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
package elem
import (
"github.com/Bitspark/slang/pkg/core"
"github.com/go-redis/redis"
"github.com/google/uuid"
)
var databaseRedisHGetCfg = &builtinConfig{
safe: true,
blueprint: core.Blueprint{
Id: uuid.MustParse("4b946e4a-e26b-45c7-9759-c60bd57d190d"),
Meta: core.BlueprintMetaDef{
Name: "Redis HGet",
ShortDescription: "executes an HGet command at the specified Redis server",
Icon: "database",
Tags: []string{"database", "redis"},
DocURL: "https://bitspark.de/slang/docs/operator/redis-hget",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"key": {
Type: "string",
},
"field": {
Type: "string",
},
},
},
Out: core.TypeDef{
Type: "string",
},
},
},
DelegateDefs: map[string]*core.DelegateDef{
"creator": {
Out: core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"key": {
Type: "string",
},
"field": {
Type: "string",
},
},
},
In: core.TypeDef{
Type: "string",
},
},
},
PropertyDefs: map[string]*core.TypeDef{
"host": {
Type: "string",
},
"password": {
Type: "string",
},
},
},
opFunc: func(op *core.Operator) {
host := op.Property("host").(string)
password := op.Property("password").(string)
client := redis.NewClient(&redis.Options{
Addr: host, // localhost:6379
Password: password, //
DB: 0, // use default DB
})
in := op.Main().In()
out := op.Main().Out()
for {
i := in.Pull()
if core.IsMarker(i) {
out.Push(i)
continue
}
pair := i.(map[string]interface{})
key := pair["key"].(string)
field := pair["field"].(string)
valueCmd := client.HGet(key, field)
if value, err := valueCmd.Result(); err == nil {
out.Push(value)
} else {
op.Delegate("creator").Out().Push(pair)
value := op.Delegate("creator").In().Pull()
boolCmd := client.HSet(key, field, value)
if _, err := boolCmd.Result(); err == nil {
out.Push(value)
} else {
panic(err)
}
}
}
},
}