-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtransformator.go
More file actions
197 lines (171 loc) · 4.61 KB
/
transformator.go
File metadata and controls
197 lines (171 loc) · 4.61 KB
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
// Copyright 2021 MIMIRO AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package transform
import (
"encoding/json"
"fmt"
"github.com/mimiro-io/datahub-cli/internal/utils"
"github.com/mimiro-io/datahub-cli/internal/web"
"strconv"
"strings"
"github.com/mimiro-io/datahub-cli/pkg/api"
"github.com/gofrs/uuid"
"github.com/pterm/pterm"
"github.com/mimiro-io/datahub-cli/internal/queries"
)
type transformer struct {
query *queries.QueryBuilder
assertedPrefixes map[string]string
}
func newTransformer(server string, token string) *transformer {
namespaces := getNamespaces(server, token)
assertedPrefixes := make(map[string]string, len(namespaces))
for k, v := range namespaces {
assertedPrefixes[v] = k
}
return &transformer{
query: queries.NewQueryBuilder(server, token),
assertedPrefixes: assertedPrefixes,
}
}
func getNamespaces(server string, token string) map[string]string {
ns, err := web.GetRequest(server, token, "/namespaces")
if err != nil {
utils.HandleError(err)
}
var namespaces map[string]string
err = json.Unmarshal(ns, &namespaces)
if err != nil {
utils.HandleError(err)
}
return namespaces
}
func (tf *transformer) Log(thing interface{}, logLevel string) {
switch strings.ToLower(logLevel) {
case "info":
pterm.Info.Println(fmt.Sprintf("- %v", thing))
case "warn", "warning":
pterm.Warning.Println(fmt.Sprintf("- %v", thing))
case "err", "error":
pterm.Error.Println(fmt.Sprintf("- %v", thing))
default:
pterm.Info.Println(fmt.Sprintf("- %v", thing))
}
}
func (tf *transformer) UUID() string {
uid, _ := uuid.NewV4()
return fmt.Sprintf("%s", uid)
}
func (tf *transformer) MakeEntityArray(entities []interface{}) []*api.Entity {
newArray := make([]*api.Entity, 0)
for _, e := range entities {
newArray = append(newArray, e.(*api.Entity))
}
return newArray
}
func (tf *transformer) GetNamespacePrefix(urlExpansion string) string {
result, err := tf.query.GetNamespacePrefix(urlExpansion)
if err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
return "ns0"
}
pterm.Error.Print(err)
return ""
}
return result
}
func (tf *transformer) AssertNamespacePrefix(urlExpansion string) string {
if val, ok := tf.assertedPrefixes[urlExpansion]; ok {
return val
} else {
l := len(tf.assertedPrefixes)
prefix := "ns" + strconv.Itoa(l)
tf.assertedPrefixes[urlExpansion] = prefix
return prefix
}
}
func (tf *transformer) Query(startingEntities []string, predicate string, inverse bool, datasets []string) [][]interface{} {
result, err := tf.query.Query(startingEntities, predicate, inverse, datasets)
if err != nil {
pterm.Error.Print(err)
return nil
}
data := make([][]interface{}, len(result.Data))
for i, e := range result.Data {
r := make([]interface{}, 3)
r[0] = e.Uri
r[1] = e.PredicateUri
r[2] = e.Entity
data[i] = r
}
return data
}
func (tf *transformer) ById(entityId string, datasets []string) *api.Entity {
entity, _, err := tf.query.QuerySingle(entityId, false, datasets)
if err != nil {
return nil
}
return entity
}
func (tf *transformer) NewEntity() *api.Entity {
entity := &api.Entity{}
entity.References = map[string]interface{}{}
entity.Properties = map[string]interface{}{}
return entity
}
func (tf *transformer) ToString(obj interface{}) string {
if obj == nil {
return "undefined"
}
switch obj.(type) {
case *api.Entity:
return fmt.Sprintf("%v", obj)
case map[string]interface{}:
return fmt.Sprintf("%v", obj)
case int, int32, int64:
return fmt.Sprintf("%d", obj)
case float32, float64:
return fmt.Sprintf("%g", obj)
case bool:
return fmt.Sprintf("%v", obj)
default:
return fmt.Sprintf("%s", obj)
}
}
func (tf *transformer) IsValidEntity(entity *api.Entity) bool {
if entity == nil {
return false
}
if entity.Recorded == 0 {
return false
}
return true
}
func (javascriptTransform *transformer) AsEntity(val interface{}) (res *api.Entity) {
if e, ok := val.(*api.Entity); ok {
res = e
return
}
if m, ok := val.(map[string]interface{}); ok {
defer func() {
if recover() != nil {
res = nil
}
}()
res = api.NewEntityFromMap(m)
return
}
res = nil
return
}