Skip to content

Commit

Permalink
Make IMemory interface follow the convention instead of returning tup… (
Browse files Browse the repository at this point in the history
#1526)

* Make IMemory interface follow the convention instead of returning tuple (#1525)

* retrigger ci
  • Loading branch information
Danieladu authored Dec 20, 2019
1 parent 05e8d9f commit d2d118e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
16 changes: 7 additions & 9 deletions libraries/botbuilder-lg/src/customizedMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,24 @@ 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;
}
}

if (this.globalMemory) {
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 {
Expand Down
9 changes: 5 additions & 4 deletions libraries/botframework-expressions/src/builtInFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
}
}

Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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 } {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions libraries/botframework-expressions/src/memory/simpleObjectMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '');
Expand All @@ -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;
}

/**
Expand All @@ -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 !== '');
Expand All @@ -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;
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit d2d118e

Please sign in to comment.