Skip to content

Remove enums from core api #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
Logger,
TraceContext,
} from '@azure/functions';
import { RpcInvocationRequest, RpcLog, RpcParameterBinding } from '@azure/functions-core';
import { RpcInvocationRequest, RpcLogLevel, RpcParameterBinding } from '@azure/functions-core';
import { v4 as uuid } from 'uuid';
import {
convertKeysToCamelCase,
Expand Down Expand Up @@ -107,11 +107,11 @@ class InvocationContext implements Context {
this.bindings = {};

// Log message that is tied to function invocation
this.log = Object.assign((...args: any[]) => userLogCallback(RpcLog.Level.Information, ...args), {
error: (...args: any[]) => userLogCallback(RpcLog.Level.Error, ...args),
warn: (...args: any[]) => userLogCallback(RpcLog.Level.Warning, ...args),
info: (...args: any[]) => userLogCallback(RpcLog.Level.Information, ...args),
verbose: (...args: any[]) => userLogCallback(RpcLog.Level.Trace, ...args),
this.log = Object.assign((...args: any[]) => userLogCallback('information', ...args), {
error: (...args: any[]) => userLogCallback('error', ...args),
warn: (...args: any[]) => userLogCallback('warning', ...args),
info: (...args: any[]) => userLogCallback('information', ...args),
verbose: (...args: any[]) => userLogCallback('trace', ...args),
});

this.bindingData = getNormalizedBindingData(request);
Expand All @@ -130,7 +130,7 @@ export interface InvocationResult {

export type DoneCallback = (err?: unknown, result?: any) => void;

export type UserLogCallback = (level: RpcLog.Level, ...args: any[]) => void;
export type UserLogCallback = (level: RpcLogLevel, ...args: any[]) => void;

export interface Dict<T> {
[key: string]: T;
Expand Down
2 changes: 1 addition & 1 deletion src/FunctionInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class FunctionInfo {

// determine output bindings & assign rpc converter (http has quirks)
Object.keys(bindings)
.filter((name) => bindings[name].direction !== RpcBindingInfo.Direction.in)
.filter((name) => bindings[name].direction !== 'in')
.forEach((name) => {
const type = bindings[name].type;
if (type && type.toLowerCase() === 'http') {
Expand Down
19 changes: 10 additions & 9 deletions src/InvocationModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
CoreInvocationContext,
InvocationArguments,
RpcInvocationResponse,
RpcLog,
RpcLogCategory,
RpcLogLevel,
RpcParameterBinding,
} from '@azure/functions-core';
import { format } from 'util';
Expand Down Expand Up @@ -35,7 +36,7 @@ export class InvocationModel implements coreTypes.InvocationModel {
const { context, inputs } = CreateContextAndInputs(
this.#funcInfo,
this.#coreCtx.request,
(level: RpcLog.Level, ...args: any[]) => this.#userLog(level, ...args),
(level: RpcLogLevel, ...args: any[]) => this.#userLog(level, ...args),
this.#doneEmitter
);
return { context, inputs };
Expand Down Expand Up @@ -142,31 +143,31 @@ export class InvocationModel implements coreTypes.InvocationModel {
return response;
}

#log(level: RpcLog.Level, logCategory: RpcLog.RpcLogCategory, ...args: any[]): void {
#log(level: RpcLogLevel, logCategory: RpcLogCategory, ...args: any[]): void {
this.#coreCtx.log(level, logCategory, format.apply(null, <[any, any[]]>args));
}

#systemLog(level: RpcLog.Level, ...args: any[]) {
this.#log(level, RpcLog.RpcLogCategory.System, ...args);
#systemLog(level: RpcLogLevel, ...args: any[]) {
this.#log(level, 'system', ...args);
}

#userLog(level: RpcLog.Level, ...args: any[]): void {
#userLog(level: RpcLogLevel, ...args: any[]): void {
if (this.#isDone && this.#coreCtx.state !== 'postInvocationHooks') {
let badAsyncMsg =
"Warning: Unexpected call to 'log' on the context object after function execution has completed. Please check for asynchronous calls that are not awaited or calls to 'done' made before function execution completes. ";
badAsyncMsg += `Function name: ${this.#funcInfo.name}. Invocation Id: ${this.#coreCtx.invocationId}. `;
badAsyncMsg += `Learn more: ${asyncDoneLearnMoreLink}`;
this.#systemLog(RpcLog.Level.Warning, badAsyncMsg);
this.#systemLog('warning', badAsyncMsg);
}
this.#log(level, RpcLog.RpcLogCategory.User, ...args);
this.#log(level, 'user', ...args);
}

#onDone(): void {
if (this.#isDone) {
const message = this.#resultIsPromise
? `Error: Choose either to return a promise or call 'done'. Do not use both in your script. Learn more: ${asyncDoneLearnMoreLink}`
: "Error: 'done' has already been called. Please check your script for extraneous calls to 'done'.";
this.#systemLog(RpcLog.Level.Error, message);
this.#systemLog('error', message);
}
this.#isDone = true;
}
Expand Down
15 changes: 2 additions & 13 deletions src/converters/BindingConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// Licensed under the MIT License.

import { BindingDefinition, ContextBindingData } from '@azure/functions';
import { RpcBindingInfo, RpcInvocationRequest } from '@azure/functions-core';
import { RpcInvocationRequest } from '@azure/functions-core';
import { FunctionInfo } from '../FunctionInfo';
import { fromTypedData } from './RpcConverters';

type BindingDirection = 'in' | 'out' | 'inout' | undefined;

export function getBindingDefinitions(info: FunctionInfo): BindingDefinition[] {
const bindings = info.bindings;
if (!bindings) {
Expand All @@ -18,7 +16,7 @@ export function getBindingDefinitions(info: FunctionInfo): BindingDefinition[] {
return {
name: name,
type: bindings[name].type || '',
direction: getDirectionName(bindings[name].direction),
direction: bindings[name].direction || undefined,
};
});
}
Expand All @@ -35,15 +33,6 @@ export function getNormalizedBindingData(request: RpcInvocationRequest): Context
return bindingData;
}

function getDirectionName(direction: RpcBindingInfo.Direction | null | undefined): BindingDirection {
const directionName = Object.keys(RpcBindingInfo.Direction).find((k) => RpcBindingInfo.Direction[k] === direction);
return isBindingDirection(directionName) ? (directionName as BindingDirection) : undefined;
}

function isBindingDirection(input: string | undefined): boolean {
return input == 'in' || input == 'out' || input == 'inout';
}

// Recursively convert keys of objects to camel case
export function convertKeysToCamelCase(obj: any) {
const output = {};
Expand Down
16 changes: 11 additions & 5 deletions src/converters/RpcHttpConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// Licensed under the MIT License.

import { Cookie } from '@azure/functions';
import { RpcHttpCookie, RpcHttpData, RpcNullableString, RpcTypedData } from '@azure/functions-core';
import {
RpcHttpCookie,
RpcHttpCookieSameSite,
RpcHttpData,
RpcNullableString,
RpcTypedData,
} from '@azure/functions-core';
import { Dict } from '../Context';
import {
fromTypedData,
Expand Down Expand Up @@ -101,15 +107,15 @@ export function toRpcHttpCookieList(inputCookies: Cookie[]): RpcHttpCookie[] {
*/
function toRpcHttpCookie(inputCookie: Cookie): RpcHttpCookie {
// Resolve RpcHttpCookie.SameSite enum, a one-off
let rpcSameSite: RpcHttpCookie.SameSite = RpcHttpCookie.SameSite.None;
let rpcSameSite: RpcHttpCookieSameSite = 'none';
if (inputCookie && inputCookie.sameSite) {
const sameSite = inputCookie.sameSite.toLocaleLowerCase();
if (sameSite === 'lax') {
rpcSameSite = RpcHttpCookie.SameSite.Lax;
rpcSameSite = 'lax';
} else if (sameSite === 'strict') {
rpcSameSite = RpcHttpCookie.SameSite.Strict;
rpcSameSite = 'strict';
} else if (sameSite === 'none') {
rpcSameSite = RpcHttpCookie.SameSite.ExplicitNone;
rpcSameSite = 'explicitNone';
}
}

Expand Down
20 changes: 10 additions & 10 deletions test/Context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ describe('Context', () => {
bindings: {
myTimer: {
type: 'timerTrigger',
direction: 0,
dataType: 0,
direction: 'in',
dataType: 'undefined',
},
},
});
Expand All @@ -70,8 +70,8 @@ describe('Context', () => {
bindings: {
req: {
type: 'http',
direction: 0,
dataType: 1,
direction: 'in',
dataType: 'string',
},
},
});
Expand Down Expand Up @@ -104,8 +104,8 @@ describe('Context', () => {
bindings: {
req: {
type: 'http',
direction: 0,
dataType: 1,
direction: 'in',
dataType: 'string',
},
},
});
Expand Down Expand Up @@ -157,8 +157,8 @@ describe('Context', () => {
bindings: {
req: {
type: 'http',
direction: 0,
dataType: 1,
direction: 'in',
dataType: 'string',
},
},
});
Expand Down Expand Up @@ -201,8 +201,8 @@ describe('Context', () => {
bindings: {
req: {
type: 'http',
direction: 0,
dataType: 1,
direction: 'in',
dataType: 'string',
},
},
});
Expand Down
20 changes: 10 additions & 10 deletions test/FunctionInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ describe('FunctionInfo', () => {
bindings: {
req: {
type: 'httpTrigger',
direction: 0,
dataType: 1,
direction: 'in',
dataType: 'string',
},
$return: {
type: 'http',
direction: 1,
dataType: 1,
direction: 'out',
dataType: 'string',
},
},
};
Expand All @@ -33,8 +33,8 @@ describe('FunctionInfo', () => {
bindings: {
req: {
type: 'httpTrigger',
direction: 0,
dataType: 1,
direction: 'in',
dataType: 'string',
},
},
};
Expand All @@ -49,13 +49,13 @@ describe('FunctionInfo', () => {
bindings: {
input: {
type: 'queue',
direction: 0,
dataType: 1,
direction: 'in',
dataType: 'string',
},
$return: {
type: 'queue',
direction: 1,
dataType: 1,
direction: 'out',
dataType: 'string',
},
},
};
Expand Down
8 changes: 4 additions & 4 deletions test/converters/BindingConverters.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import { RpcBindingInfo, RpcFunctionMetadata, RpcInvocationRequest, RpcTypedData } from '@azure/functions-core';
import { RpcFunctionMetadata, RpcInvocationRequest, RpcTypedData } from '@azure/functions-core';
import { expect } from 'chai';
import { fromString } from 'long';
import 'mocha';
Expand Down Expand Up @@ -118,15 +118,15 @@ describe('Binding Converters', () => {
bindings: {
req: {
type: 'httpTrigger',
direction: RpcBindingInfo.Direction.in,
direction: 'in',
},
res: {
type: 'http',
direction: RpcBindingInfo.Direction.out,
direction: 'out',
},
firstQueueOutput: {
type: 'queue',
direction: RpcBindingInfo.Direction.out,
direction: 'out',
},
noDirection: {
type: 'queue',
Expand Down
9 changes: 4 additions & 5 deletions test/converters/RpcHttpConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

import { Cookie } from '@azure/functions';
import { RpcHttpCookie } from '@azure/functions-core';
import { expect } from 'chai';
import 'mocha';
import { toRpcHttp, toRpcHttpCookieList } from '../../src/converters/RpcHttpConverters';
Expand Down Expand Up @@ -69,16 +68,16 @@ describe('Rpc Converters', () => {

const rpcCookies = toRpcHttpCookieList(cookieInputs);
expect(rpcCookies[0].name).to.equal('none-cookie');
expect(rpcCookies[0].sameSite).to.equal(RpcHttpCookie.SameSite.ExplicitNone);
expect(rpcCookies[0].sameSite).to.equal('explicitNone');

expect(rpcCookies[1].name).to.equal('lax-cookie');
expect(rpcCookies[1].sameSite).to.equal(RpcHttpCookie.SameSite.Lax);
expect(rpcCookies[1].sameSite).to.equal('lax');

expect(rpcCookies[2].name).to.equal('strict-cookie');
expect(rpcCookies[2].sameSite).to.equal(RpcHttpCookie.SameSite.Strict);
expect(rpcCookies[2].sameSite).to.equal('strict');

expect(rpcCookies[3].name).to.equal('default-cookie');
expect(rpcCookies[3].sameSite).to.equal(RpcHttpCookie.SameSite.None);
expect(rpcCookies[3].sameSite).to.equal('none');
});

it('throws on invalid input', () => {
Expand Down
3 changes: 0 additions & 3 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
import * as globby from 'globby';
import * as Mocha from 'mocha';
import * as path from 'path';
import { setupTestCoreApi } from './setupTestCoreApi';

export async function run(): Promise<void> {
try {
setupTestCoreApi();

const options: Mocha.MochaOptions = {
color: true,
reporter: 'mocha-multi-reporters',
Expand Down
Loading