dropkiq
Version:
Liquid Expressions Simplified
1 lines • 2.85 kB
JavaScript
;var __read=this&&this.__read||function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,o,a=r.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(n=a.next()).done;)i.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(r=a.return)&&r.call(a)}finally{if(o)throw o.error}}return i},__spread=this&&this.__spread||function(){for(var e=[],t=0;t<arguments.length;t++)e=e.concat(__read(arguments[t]));return e};Object.defineProperty(exports,"__esModule",{value:!0});var CycleDetector_1=require("./CycleDetector"),FilterRegistry_1=require("./FilterRegistry"),DataSchema=function(){function e(e,t,r){this.schema=e,this.context=t,this.scope=r,this.cycleDetector=new CycleDetector_1.CycleDetector,this.constructedScope=this.constructFakeScopeForArrays(),this.filterRegistry=new FilterRegistry_1.FilterRegistry}return e.prototype.setCurrentPath=function(e){return this.currentPath=e,this.currentPath},e.prototype.buildPathSchema=function(e,t,r){void 0===e&&(e=[]),void 0===t&&(t=this.context),void 0===r&&(r=__spread(this.currentPath));var n=r.shift(),o=/\w+\[\d*\]$/.test(n);if(o){n=n.replace(/\[\d*\]$/,"")}if(""===n)throw new Error("Liquid is invalid (You likely have too many periods)");if(n){if(!t[n])throw new Error('Unable to find "'+n+'" in the current context');var a=Object.assign({},t[n],{name:n});switch("ColumnTypes::HasMany"===a.type&&o&&(a=Object.assign(a,{type:"ColumnTypes::HasOne"})),e.push(a),a.type){case"ColumnTypes::HasOne":var i=a.foreign_table_name,s=this.schema[i];if(!s)throw new Error('Table "'+i+'" was not found in data schema');this.buildPathSchema(e,s.methods,r);break;default:if(r.length)throw new Error("Liquid expression extends beyond a terminating node")}}return e},e.prototype.nextOptions=function(e,t){void 0===e&&(e=this.buildPathSchema()),void 0===t&&(t=this.context);var r=e[e.length-1];if(!r)return t;var n=r.foreign_table_name;switch(r.type){case"ColumnTypes::HasOne":return this.schema[n].methods;default:return{}}},e.prototype.constructFakeScopeForArrays=function(e,t,r,n){var o=this;return void 0===e&&(e=this.context),void 0===t&&(t={}),void 0===r&&(r=""),void 0===n&&(n=0),t._table_name=r,Object.keys(e).forEach((function(r){var a=e[r];if("ColumnTypes::HasOne"==a.type){var i=a.foreign_table_name;if(!(c=o.schema[i]))throw new Error('Table "'+i+'" was not found in data schema');var s=c.methods;try{o.cycleDetector.register(i,n)}catch(e){return t}return t[r]=o.constructFakeScopeForArrays(s,{},i,n+1),o.cycleDetector.unregister(i),t}if("ColumnTypes::HasMany"==a.type){var c;i=a.foreign_table_name;if(!(c=o.schema[i]))throw new Error('Table "'+i+'" was not found in data schema');s=c.methods;try{o.cycleDetector.register(i,n)}catch(e){return t}return t[r]=[o.constructFakeScopeForArrays(s,{},i,n+1)],o.cycleDetector.unregister(i),t}})),t},e}();exports.DataSchema=DataSchema;