-
Notifications
You must be signed in to change notification settings - Fork 16
/
metadataReading.ts
188 lines (177 loc) · 7.54 KB
/
metadataReading.ts
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
import * as _ from "lodash";
import { HandleCommand } from "../../HandleCommand";
import * as GraphQL from "../../internal/graph/graphQL";
import {
CommandHandlerMetadata,
EventHandlerMetadata,
MappedParameterDeclaration,
Parameter,
SecretDeclaration,
ValueDeclaration,
} from "../../metadata/automationMetadata";
import * as decorators from "./decoratorSupport";
import {
isCommandHandlerMetadata,
isEventHandlerMetadata,
} from "./metadata";
/**
* Extract metadata from a handler instance. We need an
* instance to pull the decorator data from
* @param h handler instance
* @return {any}
*/
export function metadataFromInstance(h: any): CommandHandlerMetadata | EventHandlerMetadata {
let md = null;
if (isEventHandlerMetadata(h)) {
md = addName(h);
} else if (isCommandHandlerMetadata(h)) {
md = addName(h);
} else {
// We need to find the instance from which to extract metadata.
// It will be the handler itself unless it implements the optional freshParametersInstance method
const knowsHowToMakeMyParams = h as HandleCommand;
if (!!knowsHowToMakeMyParams.freshParametersInstance) {
const paramsInstance = knowsHowToMakeMyParams.freshParametersInstance();
if (!paramsInstance.__kind) {
paramsInstance.__kind = "parameters";
}
md = metadataFromDecorator(h, paramsInstance);
} else {
md = metadataFromDecorator(h, h);
}
}
// Clone metadata as otherwise we mess with previous created instances
return _.cloneDeep(md);
}
function addName(r: CommandHandlerMetadata | EventHandlerMetadata): CommandHandlerMetadata | EventHandlerMetadata {
if (!r.name) {
r.name = r.constructor.name;
}
return r;
}
function metadataFromDecorator(h: any, params: any): CommandHandlerMetadata | EventHandlerMetadata {
switch (params.__kind) {
case "command-handler":
return {
name: params.__name,
description: params.__description,
tags: params.__tags ? params.__tags : [],
intent: params.__intent ? params.__intent : [],
auto_submit: params.__autoSubmit,
parameters: parametersFromInstance(params),
mapped_parameters: mappedParameterMetadataFromInstance(params),
secrets: secretsMetadataFromInstance(params),
values: valueMetadataFromInstance(params),
};
case "parameters":
return {
name: h.__name,
description: h.__description,
tags: h.__tags ? h.__tags : [],
intent: h.__intent ? h.__intent : [],
auto_submit: h.__autoSubmit,
parameters: parametersFromInstance(params),
mapped_parameters: mappedParameterMetadataFromInstance(params),
secrets: secretsMetadataFromInstance(params),
values: valueMetadataFromInstance(params),
};
case "event-handler":
// Remove any linebreaks and spaces from those subscription
const subscription = GraphQL.inlineQuery(GraphQL.replaceOperationName(params.__subscription, h.__name));
const subscriptionName = GraphQL.operationName(subscription);
return {
name: h.__name,
description: h.__description,
tags: h.__tags ? h.__tags : [],
subscription,
subscriptionName,
secrets: secretsMetadataFromInstance(h),
values: valueMetadataFromInstance(h),
};
default:
throw new Error(`Unsupported automation '${params.constructor.name}'`);
}
}
function parametersFromInstance(r: any, prefix: string = ""): Parameter[] {
const directParams = directParameters(r, prefix);
const nestedParameters = _.flatten(Object.keys(r)
.map(key => [key, r[key]])
.filter(nestedFieldInfo => !!nestedFieldInfo[1])
.filter(nestedFieldInfo => typeof nestedFieldInfo[1] === "object")
.map(nestedFieldInfo => parametersFromInstance(nestedFieldInfo[1], prefix + nestedFieldInfo[0] + ".")),
);
const allParameters = directParams.concat(nestedParameters);
return allParameters.sort((p1, p2) => {
const o1 = p1.order || Number.MAX_SAFE_INTEGER;
const o2 = p2.order || Number.MAX_SAFE_INTEGER;
return o1 - o2;
});
}
function directParameters(r: any, prefix: string) {
return !!r && r.__parameters ? (r.__parameters as decorators.Parameter[]).map(p => {
const nameToUse = prefix + p.name;
const parameter: Parameter = {
name: nameToUse,
pattern: p.pattern ? p.pattern.source : "^.*$",
description: p.description,
required: p.required,
group: p.group,
displayable: p.displayable !== false,
type: p.type,
max_length: p.maxLength,
min_length: p.minLength,
valid_input: p.validInput,
default_value: r[p.name],
display_name: p.displayName ? p.displayName : nameToUse,
order: p.order,
control: p.control,
};
// Make this optional parameter explicit
if (parameter.default_value) {
// right now the bot only supports string parameter values
parameter.default_value = parameter.default_value.toString();
}
return parameter;
}) : [];
}
function secretsMetadataFromInstance(r: any, prefix: string = ""): SecretDeclaration[] {
const directSecrets = !!r && r.__secrets ? r.__secrets.map(s => ({ name: prefix + s.name, uri: s.uri })) : [];
const nestedParameters = _.flatten(Object.keys(r)
.map(key => [key, r[key]])
.filter(nestedFieldInfo => !!nestedFieldInfo[1])
.filter(nestedFieldInfo => typeof nestedFieldInfo[1] === "object")
.map(nestedFieldInfo => secretsMetadataFromInstance(nestedFieldInfo[1], prefix + nestedFieldInfo[0] + ".")),
);
return directSecrets.concat(nestedParameters);
}
function mappedParameterMetadataFromInstance(r: any, prefix: string = ""): MappedParameterDeclaration[] {
const directMappedParams = !!r && r.__mappedParameters ? r.__mappedParameters.map(mp =>
({
name: prefix + mp.name,
uri: mp.uri,
required: mp.required !== false,
})) : [];
const nestedParameters = _.flatten(Object.keys(r)
.map(key => [key, r[key]])
.filter(nestedFieldInfo => !!nestedFieldInfo[1])
.filter(nestedFieldInfo => typeof nestedFieldInfo[1] === "object")
.map(nestedFieldInfo => mappedParameterMetadataFromInstance(nestedFieldInfo[1], prefix + nestedFieldInfo[0] + ".")),
);
return directMappedParams.concat(nestedParameters);
}
function valueMetadataFromInstance(r: any, prefix: string = ""): ValueDeclaration[] {
const directValues = !!r && r.__values ? r.__values.map(mp =>
({
name: prefix + mp.name,
path: mp.value.path,
required: mp.value.required !== false,
type: mp.value.type ? mp.value.type : "string",
})) : [];
const nestedValues = _.flatten(Object.keys(r)
.map(key => [key, r[key]])
.filter(nestedFieldInfo => !!nestedFieldInfo[1])
.filter(nestedFieldInfo => typeof nestedFieldInfo[1] === "object")
.map(nestedFieldInfo => valueMetadataFromInstance(nestedFieldInfo[1], prefix + nestedFieldInfo[0] + ".")),
);
return directValues.concat(nestedValues);
}