From cd3d58ef7881b5c57e9bbc92e7e560891c1b5918 Mon Sep 17 00:00:00 2001 From: marky ercillo Date: Wed, 24 Apr 2024 14:21:19 -0700 Subject: [PATCH 1/6] WIP; working solution but can be refactored --- .npmrc | 1 - core/player/src/view/__tests__/view.test.ts | 49 +++++++++++++++++++++ core/player/src/view/parser/index.ts | 23 ++++++++-- core/player/src/view/plugins/switch.ts | 10 ++++- 4 files changed, 77 insertions(+), 6 deletions(-) delete mode 100644 .npmrc diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 19dd2939c..000000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -registry=https://registry.npmjs.com diff --git a/core/player/src/view/__tests__/view.test.ts b/core/player/src/view/__tests__/view.test.ts index d0e0b4bc9..50bc69e0b 100644 --- a/core/player/src/view/__tests__/view.test.ts +++ b/core/player/src/view/__tests__/view.test.ts @@ -70,6 +70,55 @@ describe('view', () => { expect(updated).toBe(resolved); }); + + test('works with no valid switch cases in an array', () => { + const model = withParser(new LocalModel({}), parseBinding); + const evaluator = new ExpressionEvaluator({ model }); + const schema = new SchemaController(); + + const view = new ViewInstance( + { + id: 'test', + type: 'view', + title: [ + { + staticSwitch: [ + { + case: false, + asset: { + id: 'false-case', + type: 'text', + value: 'some text', + }, + }, + { + case: false, + asset: { + id: 'false-case-2', + type: 'text', + value: 'some text', + }, + }, + ], + }, + ], + }, + { + model, + parseBinding, + evaluator, + schema, + } + ); + + const resolved = view.update(); + + expect(resolved).toStrictEqual({ + id: 'test', + type: 'view', + }); + }); + it('does not return a field object if the case does not resolve an asset', () => { const model = withParser( new LocalModel({ diff --git a/core/player/src/view/parser/index.ts b/core/player/src/view/parser/index.ts index 71a7faf19..159f97211 100644 --- a/core/player/src/view/parser/index.ts +++ b/core/player/src/view/parser/index.ts @@ -128,6 +128,10 @@ export class Parser { ): Node.Node | null { const nodeType = this.hooks.determineNodeType.call(obj); + console.log('@@ PARSEOBJECT[obj]: ',obj) + console.log('@@ PARSEOBJECT[type]: ',type) + + //nodeType is what it should be checking on if (nodeType !== undefined) { const parsedNode = this.hooks.parseNode.call( obj, @@ -173,6 +177,8 @@ export class Parser { const newValue = objEntries.reduce((accumulation, current): NestedObj => { const { children, ...rest } = accumulation; const [localKey, localValue] = current; + console.log('@@ PARSER LOCALKEY: ', localKey) + console.log('@@ PARSER LOCALVALUE: ', localValue) if (localKey === 'asset' && typeof localValue === 'object') { const assetAST = this.parseObject( localValue, @@ -233,16 +239,21 @@ export class Parser { children: [...children, ...templateChildren], } as NestedObj; } else if ( - localValue && - this.hooks.determineNodeType.call(localValue) === NodeType.Switch + (localValue && + this.hooks.determineNodeType.call(localValue) === NodeType.Switch) || localKey === 'staticSwitch' ) { + console.log('@@ PARSER[parseLocalObject] localValue: ',localValue) + console.log('@@ PARSER[parseLocalObject] DetermineNodeType: ',this.hooks.determineNodeType.call(localValue)) + const localSwitch = this.hooks.parseNode.call( - localValue, + localKey === 'staticSwitch' ? {"staticSwitch":localValue}:localValue, NodeType.Value, options, NodeType.Switch ); + + console.log('@@ PARSER[parseLocalObject] localSwitch: ',localSwitch) if ( localSwitch && localSwitch.type === NodeType.Value && @@ -288,13 +299,17 @@ export class Parser { }); } } else if (localValue && Array.isArray(localValue)) { + console.log('@@ IN PARSER[multinode] [localValue]: ', localValue) + localValue.map((childValues)=>console.log("@@CHILDVALUES FOR LOCALVALUE^: ", childValues)) const childValues = localValue .map((childVal) => this.parseObject(childVal, NodeType.Value, options) ) .filter((child): child is Node.Node => !!child); + if (childValues.length > 0) { + console.log('@@ PARSER in childValues: ',childValues) const multiNode = this.hooks.onCreateASTNode.call( { type: NodeType.MultiNode, @@ -310,7 +325,7 @@ export class Parser { v.parent = multiNode; }); } - + console.log('@@ Parser: in multinode: ',multiNode) if (multiNode) { return { ...rest, diff --git a/core/player/src/view/plugins/switch.ts b/core/player/src/view/plugins/switch.ts index 99bfc0821..19f4071c1 100644 --- a/core/player/src/view/plugins/switch.ts +++ b/core/player/src/view/plugins/switch.ts @@ -15,12 +15,13 @@ export default class SwitchPlugin implements ViewPlugin { private resolveSwitch(node: Node.Switch, options: Options): Node.Node { for (const switchCase of node.cases) { const isApplicable = options.evaluate(switchCase.case); - + console.log('@@ resolveSwitch[isApplicable]',isApplicable) if (isApplicable) { return switchCase.value; } } + console.log('@@ resolveSwitch[node.cases] are false. should return empty node') return EMPTY_NODE; } @@ -31,14 +32,18 @@ export default class SwitchPlugin implements ViewPlugin { return this.resolveSwitch(node, this.options); } + console.log('@@ ApplyParse onCreateASTNODE[ Returning node: ',node ) + console.log('@@ ApplyParse onCreateASTNODE[ Returning nodeType: ['+node?.type+']') return node; }); parser.hooks.determineNodeType.tap('switch', (obj) => { + console.log('** determining switch nodetype:',obj) if ( Object.prototype.hasOwnProperty.call(obj, 'dynamicSwitch') || Object.prototype.hasOwnProperty.call(obj, 'staticSwitch') ) { + console.log('** DETERMINED switch nodetype:',obj) return NodeType.Switch; } }); @@ -52,6 +57,7 @@ export default class SwitchPlugin implements ViewPlugin { determinedNodeType: null | NodeType ) => { if (determinedNodeType === NodeType.Switch) { + console.log('@@ PARSENODE SWITCH:', obj) const dynamic = 'dynamicSwitch' in obj; const switchContent = 'dynamicSwitch' in obj ? obj.dynamicSwitch : obj.staticSwitch; @@ -112,9 +118,11 @@ export default class SwitchPlugin implements ViewPlugin { /** Switches resolved during the parsing phase are dynamic */ resolver.hooks.beforeResolve.tap('switch', (node, options) => { if (node && node.type === NodeType.Switch && node.dynamic) { + console.log('@@ applyResolver | beforeResolve calling resolveSwitch', node) return this.resolveSwitch(node, options); } + console.log('@@ applyResolver not a switch', node) return node; }); } From 3a9ac96c6afec3220b94a622bb5ef244d505fdeb Mon Sep 17 00:00:00 2001 From: marky ercillo Date: Wed, 24 Apr 2024 15:02:50 -0700 Subject: [PATCH 2/6] removed logs --- core/player/src/view/parser/index.ts | 14 +------------- core/player/src/view/plugins/switch.ts | 8 -------- 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/core/player/src/view/parser/index.ts b/core/player/src/view/parser/index.ts index 159f97211..3a2d781da 100644 --- a/core/player/src/view/parser/index.ts +++ b/core/player/src/view/parser/index.ts @@ -128,9 +128,6 @@ export class Parser { ): Node.Node | null { const nodeType = this.hooks.determineNodeType.call(obj); - console.log('@@ PARSEOBJECT[obj]: ',obj) - console.log('@@ PARSEOBJECT[type]: ',type) - //nodeType is what it should be checking on if (nodeType !== undefined) { const parsedNode = this.hooks.parseNode.call( @@ -177,8 +174,7 @@ export class Parser { const newValue = objEntries.reduce((accumulation, current): NestedObj => { const { children, ...rest } = accumulation; const [localKey, localValue] = current; - console.log('@@ PARSER LOCALKEY: ', localKey) - console.log('@@ PARSER LOCALVALUE: ', localValue) + if (localKey === 'asset' && typeof localValue === 'object') { const assetAST = this.parseObject( localValue, @@ -242,9 +238,6 @@ export class Parser { (localValue && this.hooks.determineNodeType.call(localValue) === NodeType.Switch) || localKey === 'staticSwitch' ) { - console.log('@@ PARSER[parseLocalObject] localValue: ',localValue) - console.log('@@ PARSER[parseLocalObject] DetermineNodeType: ',this.hooks.determineNodeType.call(localValue)) - const localSwitch = this.hooks.parseNode.call( localKey === 'staticSwitch' ? {"staticSwitch":localValue}:localValue, NodeType.Value, @@ -252,8 +245,6 @@ export class Parser { NodeType.Switch ); - - console.log('@@ PARSER[parseLocalObject] localSwitch: ',localSwitch) if ( localSwitch && localSwitch.type === NodeType.Value && @@ -299,7 +290,6 @@ export class Parser { }); } } else if (localValue && Array.isArray(localValue)) { - console.log('@@ IN PARSER[multinode] [localValue]: ', localValue) localValue.map((childValues)=>console.log("@@CHILDVALUES FOR LOCALVALUE^: ", childValues)) const childValues = localValue .map((childVal) => @@ -309,7 +299,6 @@ export class Parser { if (childValues.length > 0) { - console.log('@@ PARSER in childValues: ',childValues) const multiNode = this.hooks.onCreateASTNode.call( { type: NodeType.MultiNode, @@ -325,7 +314,6 @@ export class Parser { v.parent = multiNode; }); } - console.log('@@ Parser: in multinode: ',multiNode) if (multiNode) { return { ...rest, diff --git a/core/player/src/view/plugins/switch.ts b/core/player/src/view/plugins/switch.ts index 19f4071c1..677441692 100644 --- a/core/player/src/view/plugins/switch.ts +++ b/core/player/src/view/plugins/switch.ts @@ -15,13 +15,11 @@ export default class SwitchPlugin implements ViewPlugin { private resolveSwitch(node: Node.Switch, options: Options): Node.Node { for (const switchCase of node.cases) { const isApplicable = options.evaluate(switchCase.case); - console.log('@@ resolveSwitch[isApplicable]',isApplicable) if (isApplicable) { return switchCase.value; } } - console.log('@@ resolveSwitch[node.cases] are false. should return empty node') return EMPTY_NODE; } @@ -32,8 +30,6 @@ export default class SwitchPlugin implements ViewPlugin { return this.resolveSwitch(node, this.options); } - console.log('@@ ApplyParse onCreateASTNODE[ Returning node: ',node ) - console.log('@@ ApplyParse onCreateASTNODE[ Returning nodeType: ['+node?.type+']') return node; }); @@ -43,7 +39,6 @@ export default class SwitchPlugin implements ViewPlugin { Object.prototype.hasOwnProperty.call(obj, 'dynamicSwitch') || Object.prototype.hasOwnProperty.call(obj, 'staticSwitch') ) { - console.log('** DETERMINED switch nodetype:',obj) return NodeType.Switch; } }); @@ -57,7 +52,6 @@ export default class SwitchPlugin implements ViewPlugin { determinedNodeType: null | NodeType ) => { if (determinedNodeType === NodeType.Switch) { - console.log('@@ PARSENODE SWITCH:', obj) const dynamic = 'dynamicSwitch' in obj; const switchContent = 'dynamicSwitch' in obj ? obj.dynamicSwitch : obj.staticSwitch; @@ -118,11 +112,9 @@ export default class SwitchPlugin implements ViewPlugin { /** Switches resolved during the parsing phase are dynamic */ resolver.hooks.beforeResolve.tap('switch', (node, options) => { if (node && node.type === NodeType.Switch && node.dynamic) { - console.log('@@ applyResolver | beforeResolve calling resolveSwitch', node) return this.resolveSwitch(node, options); } - console.log('@@ applyResolver not a switch', node) return node; }); } From 6f192ef4cb63246605d6c58558c70caccf7a80cc Mon Sep 17 00:00:00 2001 From: marky ercillo Date: Wed, 24 Apr 2024 15:03:24 -0700 Subject: [PATCH 3/6] removed logs --- core/player/src/view/parser/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/core/player/src/view/parser/index.ts b/core/player/src/view/parser/index.ts index 3a2d781da..f6a465b86 100644 --- a/core/player/src/view/parser/index.ts +++ b/core/player/src/view/parser/index.ts @@ -290,7 +290,6 @@ export class Parser { }); } } else if (localValue && Array.isArray(localValue)) { - localValue.map((childValues)=>console.log("@@CHILDVALUES FOR LOCALVALUE^: ", childValues)) const childValues = localValue .map((childVal) => this.parseObject(childVal, NodeType.Value, options) From f76a1120488d9be769e4960f14b592556ea61596 Mon Sep 17 00:00:00 2001 From: marky ercillo Date: Wed, 24 Apr 2024 15:22:14 -0700 Subject: [PATCH 4/6] updated with hasSwitchKey --- core/player/src/view/parser/index.ts | 8 ++++++-- core/player/src/view/plugins/switch.ts | 1 - 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core/player/src/view/parser/index.ts b/core/player/src/view/parser/index.ts index f6a465b86..30f6f2b40 100644 --- a/core/player/src/view/parser/index.ts +++ b/core/player/src/view/parser/index.ts @@ -121,6 +121,10 @@ export class Parser { ); } + private hasSwitchKey(localKey: string){ + return localKey.toLocaleLowerCase() === ('staticswitch' || 'dynamicswitch') + } + public parseObject( obj: object, type: Node.ChildrenTypes = NodeType.Value, @@ -236,10 +240,10 @@ export class Parser { } as NestedObj; } else if ( (localValue && - this.hooks.determineNodeType.call(localValue) === NodeType.Switch) || localKey === 'staticSwitch' + this.hooks.determineNodeType.call(localValue) === NodeType.Switch) || this.hasSwitchKey(localKey) ) { const localSwitch = this.hooks.parseNode.call( - localKey === 'staticSwitch' ? {"staticSwitch":localValue}:localValue, + this.hasSwitchKey(localKey) ? {[localKey]:localValue}:localValue, NodeType.Value, options, NodeType.Switch diff --git a/core/player/src/view/plugins/switch.ts b/core/player/src/view/plugins/switch.ts index 677441692..2848bc034 100644 --- a/core/player/src/view/plugins/switch.ts +++ b/core/player/src/view/plugins/switch.ts @@ -34,7 +34,6 @@ export default class SwitchPlugin implements ViewPlugin { }); parser.hooks.determineNodeType.tap('switch', (obj) => { - console.log('** determining switch nodetype:',obj) if ( Object.prototype.hasOwnProperty.call(obj, 'dynamicSwitch') || Object.prototype.hasOwnProperty.call(obj, 'staticSwitch') From 1f6f75200264af6cc7dff3b3eff323676c2de016 Mon Sep 17 00:00:00 2001 From: marky ercillo Date: Wed, 24 Apr 2024 15:34:01 -0700 Subject: [PATCH 5/6] eslint fixes --- core/player/src/view/__tests__/view.test.ts | 49 ++++++++++----------- core/player/src/view/parser/index.ts | 16 ++++--- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/core/player/src/view/__tests__/view.test.ts b/core/player/src/view/__tests__/view.test.ts index 50bc69e0b..11fde869b 100644 --- a/core/player/src/view/__tests__/view.test.ts +++ b/core/player/src/view/__tests__/view.test.ts @@ -70,39 +70,38 @@ describe('view', () => { expect(updated).toBe(resolved); }); - test('works with no valid switch cases in an array', () => { const model = withParser(new LocalModel({}), parseBinding); const evaluator = new ExpressionEvaluator({ model }); const schema = new SchemaController(); const view = new ViewInstance( - { - id: 'test', - type: 'view', - title: [ - { - staticSwitch: [ - { - case: false, - asset: { - id: 'false-case', - type: 'text', - value: 'some text', - }, + { + id: 'test', + type: 'view', + title: [ + { + staticSwitch: [ + { + case: false, + asset: { + id: 'false-case', + type: 'text', + value: 'some text', }, - { - case: false, - asset: { - id: 'false-case-2', - type: 'text', - value: 'some text', - }, + }, + { + case: false, + asset: { + id: 'false-case-2', + type: 'text', + value: 'some text', }, - ], - }, - ], - }, + }, + ], + }, + ], + }, { model, parseBinding, diff --git a/core/player/src/view/parser/index.ts b/core/player/src/view/parser/index.ts index 30f6f2b40..4f866a0a6 100644 --- a/core/player/src/view/parser/index.ts +++ b/core/player/src/view/parser/index.ts @@ -121,8 +121,8 @@ export class Parser { ); } - private hasSwitchKey(localKey: string){ - return localKey.toLocaleLowerCase() === ('staticswitch' || 'dynamicswitch') + private hasSwitchKey(localKey: string) { + return localKey === ('staticSwitch' || 'dynamicSwitch'); } public parseObject( @@ -132,7 +132,6 @@ export class Parser { ): Node.Node | null { const nodeType = this.hooks.determineNodeType.call(obj); - //nodeType is what it should be checking on if (nodeType !== undefined) { const parsedNode = this.hooks.parseNode.call( obj, @@ -239,11 +238,15 @@ export class Parser { children: [...children, ...templateChildren], } as NestedObj; } else if ( - (localValue && - this.hooks.determineNodeType.call(localValue) === NodeType.Switch) || this.hasSwitchKey(localKey) + (localValue && + this.hooks.determineNodeType.call(localValue) === + NodeType.Switch) || + this.hasSwitchKey(localKey) ) { const localSwitch = this.hooks.parseNode.call( - this.hasSwitchKey(localKey) ? {[localKey]:localValue}:localValue, + this.hasSwitchKey(localKey) + ? { [localKey]: localValue } + : localValue, NodeType.Value, options, NodeType.Switch @@ -299,7 +302,6 @@ export class Parser { this.parseObject(childVal, NodeType.Value, options) ) .filter((child): child is Node.Node => !!child); - if (childValues.length > 0) { const multiNode = this.hooks.onCreateASTNode.call( From 8a0d69f5226482771fdde3f287998384a0c90456 Mon Sep 17 00:00:00 2001 From: marky ercillo Date: Wed, 24 Apr 2024 15:42:48 -0700 Subject: [PATCH 6/6] added .npmrc back --- .npmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 000000000..eb00adf21 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +registry=https://registry.npmjs.com \ No newline at end of file