-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
178 lines (159 loc) · 4.93 KB
/
types.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
package types
import (
"encoding/base64"
"fmt"
"strings"
"time"
)
type Name struct {
Instance int `json:"instance"`
UniqueName string `json:"uniqueName"`
UserMemory string `json:"userMemory"`
}
type ActivationMessage struct {
Action Action `json:"action"`
ActivationID string `json:"activationId"`
Blocking bool `json:"blocking"`
Parameters interface{} `json:"content"`
Revision string `json:"revision"`
Controller struct {
AsString string `json:"asString"`
} `json:"rootControllerIndex"`
TransactionID []interface{} `json:"transid"`
User User `json:"user"`
}
type Action struct {
Name string `json:"name"`
Path string `json:"path"`
Version string `json:"version"`
}
type User struct {
AuthKey struct {
APIKey string `json:"api_key"`
} `json:"authKey"`
Limits interface{} `json:"limits"`
Namespace struct {
Name string `json:"name"`
UUID string `json:"uuid"`
} `json:"namespace"`
Rights []interface{} `json:"rights"`
Subject string `json:"subject"`
}
type CompletionMessage struct {
ActivationID string `json:"activationId"`
Invoker Name `json:"invoker"`
SystemError bool `json:"isSystemError"`
TransactionID []interface{} `json:"transid"`
}
type CompletionResponseMessage struct {
Response Response `json:"response"`
TransactionID []interface{} `json:"transid"`
}
// type Response struct {
// ActivationID string `json:"activationId"`
// Annotations []storage.Annotation `json:"annotations"`
// }
type ActivationDoc struct {
ID string `json:"_id"`
Revision string `json:"_rev,omitempty"`
Updated int `json:"updated"`
Response
}
type Response struct {
ActivationID string `json:"activationId"`
Annotations []Annotation `json:"annotations"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Response ResponseValue `json:"response"`
Start int `json:"start"`
End int `json:"end"`
Duration int `json:"duration"`
Subject string `json:"subject"`
EntityType string `json:"entityType"`
Logs []interface{} `json:"logs"`
Publish bool `json:"publish"`
Version string `json:"version"`
}
type ResponseValue struct {
Result interface{} `json:"result"`
StatusCode int `json:"statusCode"`
}
func GenerateResponse(activationMessage *ActivationMessage, function *FunctionDoc, result interface{}) Response {
logs := make([]interface{}, 0)
return Response{
ActivationID: activationMessage.ActivationID,
Annotations: function.Annotations,
Name: function.Name,
Namespace: function.Namespace,
Response: ResponseValue{
Result: result,
StatusCode: 0,
},
Start: int(time.Now().Unix()),
End: int(time.Now().Unix() + 2),
Duration: 5,
Subject: activationMessage.User.Subject,
EntityType: "activation",
Logs: logs,
Publish: function.Publish,
Version: function.Version,
}
}
type FunctionDoc struct {
ID string `json:"_id"`
Revision string `json:"_rev"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Executable Executable `json:"exec"`
Binary bool `json:"binary"`
Limits Limits `json:"limits"`
Parameters []interface{} `json:"parameters"`
Annotations []Annotation `json:"annotations"`
EntityType string `json:"entityType"`
Publish bool `json:"publish"`
Updated int `json:"updated"`
Version string `json:"version"`
}
type Executable struct {
Kind string `json:"kind"`
Code interface{} `json:"code"`
}
func (f *FunctionDoc) CodeString() (string, error) {
var functionCode string
switch v := f.Executable.Code.(type) {
case string:
functionCode = v
case map[string]interface{}:
attachment := v["attachmentName"]
attachmentString, ok := attachment.(string)
if !ok {
return "", fmt.Errorf("attachment code was not string: %s", v)
}
// Java code is a b64 encoded jar
if f.Executable.Kind == "java" {
b64url := attachmentString[4:]
// For some reason when it comes through whisk it is b64url encoded
b64 := strings.ReplaceAll(strings.ReplaceAll(b64url, "-", "+"), "_", "/")
return b64, nil
}
b64code := attachmentString[4:] // mem:b64encodedcode
functionBytes, err := base64.StdEncoding.DecodeString(b64code)
if err != nil {
return "", fmt.Errorf("could not decode function %s: %s", v, err)
}
functionCode = string(functionBytes)
default:
return "", fmt.Errorf("function code not in expected type: %s", f.Executable)
}
return functionCode, nil
}
type Limits struct {
Concurrency int `json:"concurrency"`
Logs int `json:"logs"`
Memory int `json:"memory"`
Timeout int `json:"timeout"`
}
type Annotation struct {
Key string `json:"key"`
Value interface{} `json:"value"`
}