Skip to content

Commit c70deff

Browse files
committed
Remove enums from core api (#622)
1 parent 35db835 commit c70deff

17 files changed

+617
-90
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Microsoft Azure Functions NodeJS Worker",
66
"license": "(MIT OR Apache-2.0)",
77
"dependencies": {
8-
"@azure/functions": "^3.5.0-alpha.2",
8+
"@azure/functions": "^3.5.0-alpha.3",
99
"@grpc/grpc-js": "^1.2.7",
1010
"@grpc/proto-loader": "^0.6.4",
1111
"blocked-at": "^1.2.0",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* Removes some unnecessary properties that may have been set to `undefined` during conversion
6+
*/
7+
export function ensureKeysMatch<TData, TResult>(data: TData, result: TResult): TResult {
8+
for (const key of Object.keys(result)) {
9+
if (!(key in data)) {
10+
delete result[key];
11+
}
12+
}
13+
return result;
14+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import * as coreTypes from '@azure/functions-core';
5+
import { AzureFunctionsRpcMessages as rpc } from '../../../azure-functions-language-worker-protobuf/src/rpc';
6+
import { ensureKeysMatch } from './ensureKeysMatch';
7+
import { fromCoreStatusResult } from './fromCoreStatusResult';
8+
import { handleDefaultEnumCase } from './handleDefaultEnumCase';
9+
10+
export function fromCoreFunctionMetadata(data: coreTypes.RpcFunctionMetadata): rpc.IRpcFunctionMetadata {
11+
const result = {
12+
...data,
13+
bindings: fromCoreBindings(data.bindings),
14+
status: fromCoreStatusResult(data.status),
15+
};
16+
return ensureKeysMatch(data, result);
17+
}
18+
19+
function fromCoreBindings(
20+
data: { [key: string]: coreTypes.RpcBindingInfo } | null | undefined
21+
): { [key: string]: rpc.IBindingInfo } | null | undefined {
22+
if (data) {
23+
const result = {};
24+
for (const [key, value] of Object.entries(data)) {
25+
result[key] = fromCoreBinding(value);
26+
}
27+
return ensureKeysMatch(data, result);
28+
} else {
29+
return data;
30+
}
31+
}
32+
33+
function fromCoreBinding(data: coreTypes.RpcBindingInfo | null | undefined): rpc.IBindingInfo | null | undefined {
34+
if (data) {
35+
const result = {
36+
...data,
37+
dataType: fromCoreBindingDataType(data.dataType),
38+
direction: fromCoreBindingDirection(data.direction),
39+
};
40+
return ensureKeysMatch(data, result);
41+
} else {
42+
return data;
43+
}
44+
}
45+
46+
function fromCoreBindingDataType(
47+
data: coreTypes.RpcBindingDataType | null | undefined
48+
): rpc.BindingInfo.DataType | null | undefined {
49+
switch (data) {
50+
case 'binary':
51+
return rpc.BindingInfo.DataType.binary;
52+
case 'stream':
53+
return rpc.BindingInfo.DataType.stream;
54+
case 'string':
55+
return rpc.BindingInfo.DataType.string;
56+
case 'undefined':
57+
return rpc.BindingInfo.DataType.undefined;
58+
default:
59+
return handleDefaultEnumCase(data, 'CoreRpcBindingDataType');
60+
}
61+
}
62+
63+
function fromCoreBindingDirection(
64+
data: coreTypes.RpcBindingDirection | null | undefined
65+
): rpc.BindingInfo.Direction | null | undefined {
66+
switch (data) {
67+
case 'in':
68+
return rpc.BindingInfo.Direction.in;
69+
case 'inout':
70+
return rpc.BindingInfo.Direction.inout;
71+
case 'out':
72+
return rpc.BindingInfo.Direction.out;
73+
default:
74+
return handleDefaultEnumCase(data, 'CoreRpcBindingDirection');
75+
}
76+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import * as coreTypes from '@azure/functions-core';
5+
import { AzureFunctionsRpcMessages as rpc } from '../../../azure-functions-language-worker-protobuf/src/rpc';
6+
import { ensureKeysMatch } from './ensureKeysMatch';
7+
import { fromCoreStatusResult } from './fromCoreStatusResult';
8+
import { fromCoreTypedData } from './fromCoreTypedData';
9+
10+
export function fromCoreInvocationResponse(data: coreTypes.RpcInvocationResponse): rpc.IInvocationResponse {
11+
const result = {
12+
...data,
13+
outputData: fromCoreParameterBindings(data.outputData),
14+
result: fromCoreStatusResult(data.result),
15+
returnValue: fromCoreTypedData(data.returnValue),
16+
};
17+
return ensureKeysMatch(data, result);
18+
}
19+
20+
function fromCoreParameterBindings(
21+
data: coreTypes.RpcParameterBinding[] | null | undefined
22+
): rpc.IParameterBinding[] | null | undefined {
23+
if (data) {
24+
return data.map(fromCoreParameterBinding);
25+
} else {
26+
return data;
27+
}
28+
}
29+
30+
function fromCoreParameterBinding(data: coreTypes.RpcParameterBinding): rpc.IParameterBinding {
31+
const result = {
32+
...data,
33+
data: fromCoreTypedData(data.data),
34+
};
35+
return ensureKeysMatch(data, result);
36+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import * as coreTypes from '@azure/functions-core';
5+
import { AzureFunctionsRpcMessages as rpc } from '../../../azure-functions-language-worker-protobuf/src/rpc';
6+
import { ensureKeysMatch } from './ensureKeysMatch';
7+
import { handleDefaultEnumCase } from './handleDefaultEnumCase';
8+
9+
export function fromCoreStatusResult(
10+
data: coreTypes.RpcStatusResult | null | undefined
11+
): rpc.IStatusResult | null | undefined {
12+
if (data) {
13+
const result = {
14+
...data,
15+
logs: fromCoreLogs(data.logs),
16+
status: fromCoreStatus(data.status),
17+
};
18+
return ensureKeysMatch(data, result);
19+
} else {
20+
return data;
21+
}
22+
}
23+
24+
function fromCoreLogs(data: coreTypes.RpcLog[] | null | undefined): rpc.IRpcLog[] | null | undefined {
25+
if (data) {
26+
return data.map(fromCoreLog);
27+
} else {
28+
return data;
29+
}
30+
}
31+
32+
function fromCoreLog(data: coreTypes.RpcLog): rpc.IRpcLog {
33+
const result = {
34+
...data,
35+
level: fromCoreLogLevel(data.level),
36+
logCategory: fromCoreLogCategory(data.logCategory),
37+
};
38+
return ensureKeysMatch(data, result);
39+
}
40+
41+
export function fromCoreLogLevel(data: coreTypes.RpcLogLevel | null | undefined): rpc.RpcLog.Level | null | undefined {
42+
switch (data) {
43+
case 'critical':
44+
return rpc.RpcLog.Level.Critical;
45+
case 'debug':
46+
return rpc.RpcLog.Level.Debug;
47+
case 'error':
48+
return rpc.RpcLog.Level.Error;
49+
case 'information':
50+
return rpc.RpcLog.Level.Information;
51+
case 'none':
52+
return rpc.RpcLog.Level.None;
53+
case 'trace':
54+
return rpc.RpcLog.Level.Trace;
55+
case 'warning':
56+
return rpc.RpcLog.Level.Warning;
57+
default:
58+
return handleDefaultEnumCase(data, 'CoreRpcLogLevel');
59+
}
60+
}
61+
62+
export function fromCoreLogCategory(
63+
data: coreTypes.RpcLogCategory | null | undefined
64+
): rpc.RpcLog.RpcLogCategory | null | undefined {
65+
switch (data) {
66+
case 'customMetric':
67+
return rpc.RpcLog.RpcLogCategory.CustomMetric;
68+
case 'system':
69+
return rpc.RpcLog.RpcLogCategory.System;
70+
case 'user':
71+
return rpc.RpcLog.RpcLogCategory.User;
72+
default:
73+
return handleDefaultEnumCase(data, 'CoreRpcLogCategory');
74+
}
75+
}
76+
77+
function fromCoreStatus(data: coreTypes.RpcStatus | null | undefined): rpc.StatusResult.Status | null | undefined {
78+
switch (data) {
79+
case 'cancelled':
80+
return rpc.StatusResult.Status.Cancelled;
81+
case 'failure':
82+
return rpc.StatusResult.Status.Failure;
83+
case 'success':
84+
return rpc.StatusResult.Status.Success;
85+
default:
86+
return handleDefaultEnumCase(data, 'CoreRpcStatus');
87+
}
88+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import * as coreTypes from '@azure/functions-core';
5+
import { AzureFunctionsRpcMessages as rpc } from '../../../azure-functions-language-worker-protobuf/src/rpc';
6+
import { ensureKeysMatch } from './ensureKeysMatch';
7+
import { handleDefaultEnumCase } from './handleDefaultEnumCase';
8+
9+
export function fromCoreTypedData(data: coreTypes.RpcTypedData | null | undefined): rpc.ITypedData | null | undefined {
10+
if (data) {
11+
const result = {
12+
...data,
13+
http: fromCoreHttpData(data.http),
14+
};
15+
return ensureKeysMatch(data, result);
16+
} else {
17+
return data;
18+
}
19+
}
20+
21+
function fromCoreHttpData(data: coreTypes.RpcHttpData | null | undefined): rpc.IRpcHttp | null | undefined {
22+
if (data) {
23+
const result = {
24+
...data,
25+
body: fromCoreTypedData(data.body),
26+
cookies: fromCoreHttpCookies(data.cookies),
27+
rawBody: fromCoreTypedData(data.rawBody),
28+
};
29+
return ensureKeysMatch(data, result);
30+
} else {
31+
return data;
32+
}
33+
}
34+
35+
function fromCoreHttpCookies(
36+
data: coreTypes.RpcHttpCookie[] | null | undefined
37+
): rpc.IRpcHttpCookie[] | null | undefined {
38+
if (data) {
39+
return data.map(fromCoreHttpCookie);
40+
} else {
41+
return data;
42+
}
43+
}
44+
45+
function fromCoreHttpCookie(data: coreTypes.RpcHttpCookie): rpc.IRpcHttpCookie {
46+
const result = {
47+
...data,
48+
sameSite: fromCoreHttpCookieSameSite(data.sameSite),
49+
};
50+
return ensureKeysMatch(data, result);
51+
}
52+
53+
function fromCoreHttpCookieSameSite(
54+
data: coreTypes.RpcHttpCookieSameSite | null | undefined
55+
): rpc.RpcHttpCookie.SameSite | null | undefined {
56+
switch (data) {
57+
case 'explicitNone':
58+
return rpc.RpcHttpCookie.SameSite.ExplicitNone;
59+
case 'lax':
60+
return rpc.RpcHttpCookie.SameSite.Lax;
61+
case 'none':
62+
return rpc.RpcHttpCookie.SameSite.None;
63+
case 'strict':
64+
return rpc.RpcHttpCookie.SameSite.Strict;
65+
default:
66+
return handleDefaultEnumCase(data, 'CoreRpcHttpCookieSameSite');
67+
}
68+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
export function handleDefaultEnumCase(data: undefined | null | string, typeName: string): undefined | null {
5+
switch (data) {
6+
case undefined:
7+
return undefined;
8+
case null:
9+
return null;
10+
default:
11+
throw new RangeError(`Unexpected value "${data}" for type "${typeName}"`);
12+
}
13+
}

0 commit comments

Comments
 (0)