From d2d118e3be1a1785a30b2b2ec76cbf1290a71ba0 Mon Sep 17 00:00:00 2001 From: "Hongyang Du (hond)" Date: Fri, 20 Dec 2019 18:07:47 +0800 Subject: [PATCH] =?UTF-8?q?Make=20IMemory=20interface=20follow=20the=20con?= =?UTF-8?q?vention=20instead=20of=20returning=20tup=E2=80=A6=20(#1526)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Make IMemory interface follow the convention instead of returning tuple (#1525) * retrigger ci --- .../botbuilder-lg/src/customizedMemory.ts | 16 ++++++-------- .../src/builtInFunction.ts | 9 ++++---- .../src/memory/composedMemory.ts | 6 ++--- .../src/memory/memoryInterface.ts | 8 +++---- .../src/memory/simpleObjectMemory.ts | 22 +++++++++---------- 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/libraries/botbuilder-lg/src/customizedMemory.ts b/libraries/botbuilder-lg/src/customizedMemory.ts index 5671c55271..35442b99b6 100644 --- a/libraries/botbuilder-lg/src/customizedMemory.ts +++ b/libraries/botbuilder-lg/src/customizedMemory.ts @@ -18,13 +18,11 @@ export class CustomizedMemory implements MemoryInterface { this.localMemory = undefined; } - public getValue(path: string): { value: any; error: string } { - let value: any; - let error = ''; + public getValue(path: string): any { if (this.localMemory) { - ({value, error} = this.localMemory.getValue(path)); - if (!error && value) { - return {value, error}; + const value = this.localMemory.getValue(path); + if (value) { + return value; } } @@ -32,12 +30,12 @@ export class CustomizedMemory implements MemoryInterface { return this.globalMemory.getValue(path); } - return {value, error}; + return undefined; } // eslint-disable-next-line @typescript-eslint/no-unused-vars - public setValue(_path: string, _value: any): { value: any; error: string } { - return {value: undefined, error: `LG memory are readonly`}; + public setValue(_path: string, _value: any): void { + return; } public version(): string { diff --git a/libraries/botframework-expressions/src/builtInFunction.ts b/libraries/botframework-expressions/src/builtInFunction.ts index f0d25932a2..7e7ef84470 100644 --- a/libraries/botframework-expressions/src/builtInFunction.ts +++ b/libraries/botframework-expressions/src/builtInFunction.ts @@ -759,7 +759,7 @@ export class BuiltInFunctions { if (left == undefined) { // fully converted to path, so we just delegate to memory scope - return state.getValue(path); + return { value: state.getValue(path), error: undefined }; } else { let newScope: any; let err: string; @@ -768,7 +768,7 @@ export class BuiltInFunctions { return {value: undefined, error: err}; } - return new SimpleObjectMemory(newScope).getValue(path); + return { value: new SimpleObjectMemory(newScope).getValue(path), error: undefined }; } } @@ -784,7 +784,7 @@ export class BuiltInFunctions { ({ value: property, error } = children[1].tryEvaluate(state)); if (!error) { - ({ value, error } = new SimpleObjectMemory(instance).getValue(property.toString())); + value = new SimpleObjectMemory(instance).getValue(property.toString()); } } @@ -893,7 +893,8 @@ export class BuiltInFunctions { return {value: undefined, error: err}; } - return state.setValue(path, value); + state.setValue(path, value); + return {value, error: undefined}; } private static foreach(expression: Expression, state: MemoryInterface): { value: any; error: string } { diff --git a/libraries/botframework-expressions/src/memory/composedMemory.ts b/libraries/botframework-expressions/src/memory/composedMemory.ts index 19a3bcdc3d..2f4eab0865 100644 --- a/libraries/botframework-expressions/src/memory/composedMemory.ts +++ b/libraries/botframework-expressions/src/memory/composedMemory.ts @@ -17,15 +17,15 @@ export class ComposedMemory implements MemoryInterface { this.memoryMap = memoryMap; } - public getValue(path: string): { value: any; error: string } { + public getValue(path: string): any { const prefix: string = path.split('.')[0]; if (this.memoryMap.has(prefix)) { return this.memoryMap.get(prefix).getValue(path.substr(prefix.length + 1)); } - return {value: undefined, error: `path not exists at ${ path }`}; + return undefined; } - public setValue(_path: string, _value: any): { value: any; error: string } { + public setValue(_path: string, _value: any): void { throw new Error('Method not implemented.'); } diff --git a/libraries/botframework-expressions/src/memory/memoryInterface.ts b/libraries/botframework-expressions/src/memory/memoryInterface.ts index b8ea97f6a6..b03354539a 100644 --- a/libraries/botframework-expressions/src/memory/memoryInterface.ts +++ b/libraries/botframework-expressions/src/memory/memoryInterface.ts @@ -16,17 +16,17 @@ export interface MemoryInterface { * get value from a given path, it can be a simple indenfiter like "a", or * a combined path like "a.b", "a.b[2]", "a.b[2].c", inside [] is guranteed to be a int number or a string. * @param path memory path. - * @returnsresovled value and error messsage if any. + * @returns value. */ - getValue(path: string): {value: string; error: string}; + getValue(path: string): any; /** * Set value to a given path. * @param path memory path. * @param value value to set. - * @returns value set and error message if any. + * @returns value */ - setValue(path: string, value: any): {value: string; error: string}; + setValue(path: string, value: any): void; /** * Version is used to identify whether the a particular memory instance has been updated or not. diff --git a/libraries/botframework-expressions/src/memory/simpleObjectMemory.ts b/libraries/botframework-expressions/src/memory/simpleObjectMemory.ts index d9db915d81..756d6c6ef0 100644 --- a/libraries/botframework-expressions/src/memory/simpleObjectMemory.ts +++ b/libraries/botframework-expressions/src/memory/simpleObjectMemory.ts @@ -26,9 +26,9 @@ export class SimpleObjectMemory implements MemoryInterface { return new SimpleObjectMemory(obj); } - public getValue(path: string): { value: any; error: string } { + public getValue(path: string): any { if (this.memory === undefined) { - return {value: undefined, error: undefined}; + return undefined; } const parts: string[] = path.split(/[.\[\]]+/).filter((u: string): boolean => u !== undefined && u !== ''); @@ -45,13 +45,13 @@ export class SimpleObjectMemory implements MemoryInterface { } if (error) { - return {value: undefined, error }; + return undefined; } curScope = value; } - return {value, error: undefined}; + return value; } /** @@ -61,9 +61,9 @@ export class SimpleObjectMemory implements MemoryInterface { * because we can't and shouldn't smart create structure in the middle * you can implement a customzied Scope that support such behavior */ - public setValue(path: string, input: any): { value: any; error: string } { + public setValue(path: string, input: any): void { if (this.memory === undefined) { - return {value: undefined, error: `Can't set value with in a null memory`}; + return;; } const parts: string[] = path.split(/[.\[\]]+/).filter((u: string): boolean => u !== undefined && u !== ''); @@ -83,12 +83,12 @@ export class SimpleObjectMemory implements MemoryInterface { } if (error) { - return {value: undefined, error }; + return; } if (curScope === undefined) { curPath = curPath.replace(/(^\.*)/g, ''); - return {value: undefined, error: `Can't set value to path: '${ path }', reason: '${ curPath }' is null` }; + return; } } @@ -108,17 +108,17 @@ export class SimpleObjectMemory implements MemoryInterface { } if (error) { - return {value: undefined, error}; + return; } } else { error = Extensions.setProperty(curScope,parts[parts.length - 1], input).error; if (error) { - return {value: undefined, error: `Can set value to path: '${ path }', reason: ${ error }`}; + return; } } this.versionNumber++; - return {value: input, error: undefined}; + return; } public version(): string {