-
Notifications
You must be signed in to change notification settings - Fork 33
/
helper.go
70 lines (65 loc) · 1.88 KB
/
helper.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
package msg
import (
"bytes"
"encoding/json"
"github.com/viant/endly"
"github.com/viant/toolbox"
"github.com/viant/toolbox/cred"
"strings"
)
func loadMessages(data []byte) []*Message {
var result = make([]*Message, 0)
var text = string(data)
if toolbox.IsNewLineDelimitedJSON(text) {
if records, err := toolbox.NewLineDelimitedJSON(text); err == nil {
for _, record := range records {
if recordMap, ok := record.(map[string]interface{}); ok {
msg := &Message{}
err := toolbox.DefaultConverter.AssignConverted(msg, recordMap)
if err != nil || (msg.Data == nil && len(msg.Attributes) == 0) {
msg.Data = recordMap
}
result = append(result, msg)
}
}
return result
}
}
err := json.NewDecoder(bytes.NewReader(data)).Decode(&result)
if err != nil {
for _, line := range strings.Split(text, "\n") {
msg := &Message{}
msg.Data = line
result = append(result, msg)
}
}
return result
}
//extractSubPath extract a next matched path fragment i.e iPath /proejcts/x/topics/t1, returns t1 for 'topics' match
func extractSubPath(aPath, match string) string {
fragments := strings.Split(aPath, "/")
for i := 0; i < len(fragments)-1; i++ {
if strings.Contains(fragments[i], match) {
return fragments[i+1]
}
}
return ""
}
func inferResourceTypeFromCredentialConfig(credConfig *cred.Config) string {
if credConfig.Key != "" && credConfig.Secret != "" {
return ResourceVendorAmazonWebService
} else if credConfig.ProjectID != "" {
return ResourceVendorGoogleCloud
}
return ""
}
func expandResource(context *endly.Context, resource *Resource) *Resource {
state := context.State()
return &Resource{
URL: state.ExpandAsText(resource.URL),
Type: state.ExpandAsText(resource.Type),
Name: state.ExpandAsText(resource.Name),
Vendor: resource.Vendor,
Credentials: state.ExpandAsText(resource.Credentials),
}
}