/
actions.go
203 lines (175 loc) · 5.59 KB
/
actions.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* _ _
*__ _____ __ ___ ___ __ _| |_ ___
*\ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
* \ V V / __/ (_| |\ V /| | (_| | || __/
* \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
*
* Copyright © 2016 - 2019 Weaviate. All rights reserved.
* LICENSE: https://github.com/creativesoftwarefdn/weaviate/blob/develop/LICENSE.md
* DESIGN & CONCEPT: Bob van Luijt (@bobvanluijt)
* CONTACT: hello@creativesoftwarefdn.org
*/
package main
import (
"fmt"
"strconv"
"time"
"github.com/creativesoftwarefdn/weaviate/client/actions"
"github.com/creativesoftwarefdn/weaviate/models"
spew "github.com/davecgh/go-spew/spew"
"github.com/go-openapi/strfmt"
)
func createActions() {
for _, action := range demoDataset.Actions {
className := action["class"].(string)
classKinds[className] = "Action"
uuid := action["uuid"].(string)
properties := map[string]interface{}{}
for key, value := range action {
if key == "class" || key == "uuid" {
continue
}
ref, isRef := value.(map[string]interface{})
if isRef {
actionFixups = append(actionFixups, fixupAddRef{
fromId: uuid,
fromProperty: key,
toClass: ref["class"].(string),
toId: ref["uuid"].(string),
})
} else {
class := findClass(schema.Actions, className)
property := findProperty(class, key)
if len(property.AtDataType) != 1 {
panic(fmt.Sprintf("Only one datatype supported for import. Failed in action %s.%s", className, property.Name))
}
dataType := property.AtDataType[0]
switch dataType {
case "string", "date":
properties[key] = value
case "int":
// Attempt to parse as float64 first.
valFloat, ok := value.(float64)
if ok {
properties[key] = valFloat
} else {
v, err := strconv.ParseInt(value.(string), 10, 64)
if err != nil {
panic(err)
}
properties[key] = v
}
case "number":
properties[key] = value.(float64)
case "boolean":
properties[key] = value.(bool)
default:
panic(fmt.Sprintf("No such datatype supported: %s", dataType))
}
}
}
t := models.ActionCreate{
AtContext: "http://example.org",
AtClass: className,
Schema: properties,
}
action := assertCreateAction(&t)
idMap[uuid] = string(action.ActionID) // Store mapping of ID's
fmt.Printf("Created Action %s\n", action.ActionID)
}
}
func fixupActions() {
fmt.Printf("Checking if all actions that need a patch are created.\n")
for {
allExist := true
for _, fixup := range actionFixups {
if !checkActionExists(idMap[fixup.fromId]) {
allExist = false
fmt.Printf("From does not exist! %v\n", idMap[fixup.fromId])
}
if classKinds[fixup.toClass] == "Action" {
if !checkActionExists(idMap[fixup.toId]) {
allExist = false
fmt.Printf("To action does not exist! %v\n", idMap[fixup.toId])
break
}
} else { // assume it's a thing.
if !checkThingExists(idMap[fixup.toId]) {
allExist = false
fmt.Printf("To thing does not exist! %v\n", idMap[fixup.toId])
break
}
}
}
if allExist {
fmt.Printf("Everything that needs to be patched exists!\n")
break
} else {
fmt.Printf("Not everything that needs to be patched exists\n")
var waitSecondsUntilSettled time.Duration = 2 * time.Second
fmt.Printf("Waiting for %v to settle\n", waitSecondsUntilSettled)
time.Sleep(waitSecondsUntilSettled)
continue
}
}
// Now fix up refs
op := "add"
for _, fixup := range actionFixups {
path := fmt.Sprintf("/schema/%s", fixup.fromProperty)
_, ok := classKinds[fixup.toClass]
if !ok {
panic(fmt.Sprintf("Unknown class '%s'", fixup.toClass))
}
patch := &models.PatchDocument{
Op: &op,
Path: &path,
Value: map[string]interface{}{
"$cref": fmt.Sprintf("weaviate://localhost/things/%s", idMap[fixup.toId]),
},
}
assertPatchAction(idMap[fixup.fromId], patch)
fmt.Printf("Patched action %s\n", idMap[fixup.fromId])
}
}
func checkActionExists(id string) bool {
params := actions.NewWeaviateActionsGetParams().WithActionID(strfmt.UUID(id))
resp, err := client.Actions.WeaviateActionsGet(params)
if err != nil {
switch v := err.(type) {
case *actions.WeaviateActionsGetNotFound:
return false
default:
panic(fmt.Sprintf("Can't create action %#v, because %#v", resp, spew.Sdump(v)))
}
}
return true
}
func assertCreateAction(t *models.ActionCreate) *models.ActionGetResponse {
params := actions.NewWeaviateActionsCreateParams().WithBody(actions.WeaviateActionsCreateBody{Action: t})
resp, _, err := client.Actions.WeaviateActionsCreate(params)
if err != nil {
switch v := err.(type) {
case *actions.WeaviateActionsCreateUnprocessableEntity:
panic(fmt.Sprintf("Can't create action %#v, because %s", t, joinErrorMessages(v.Payload)))
default:
panic(fmt.Sprintf("Can't create action %#v, because %#v", t, spew.Sdump(err)))
}
}
return resp.Payload
}
func assertPatchAction(id string, p *models.PatchDocument) *models.ActionGetResponse {
params := actions.NewWeaviateActionsPatchParams().WithBody([]*models.PatchDocument{p}).WithActionID(strfmt.UUID(id))
resp, _, err := client.Actions.WeaviateActionsPatch(params)
if err != nil {
switch v := err.(type) {
case *actions.WeaviateActionsPatchNotFound:
panic(fmt.Sprintf("Can't patch action with %s, because action cannot be found", spew.Sdump(p)))
case *actions.WeaviateActionsPatchUnprocessableEntity:
panic(fmt.Sprintf("Can't patch action, because %s", joinErrorMessages(v.Payload)))
default:
_ = v
panic(fmt.Sprintf("Can't patch action with %#v, because %#v", p, spew.Sdump(err)))
}
}
return resp.Payload
}