@@ -5,7 +5,7 @@ import { dump as dumpYaml } from "js-yaml";
55import * as path from "path" ;
66
77import { version } from "df/core/version" ;
8- import { dataform } from "df/protos/ts" ;
8+ import { dataform , google } from "df/protos/ts" ;
99import { asPlainObject , suite , test } from "df/testing" ;
1010import { TmpDirFixture } from "df/testing/fixtures" ;
1111import {
@@ -708,6 +708,7 @@ select 1 AS \${dataform.projectConfig.vars.selectVar}`
708708 asPlainObject ( {
709709 dataformCoreVersion : version ,
710710 graphErrors : { } ,
711+ jitData : { } ,
711712 projectConfig : {
712713 defaultDatabase : "defaultProject" ,
713714 defaultLocation : "locationInOverride" ,
@@ -884,6 +885,7 @@ select 1 AS \${dataform.projectConfig.vars.columnVar}`
884885 ] ,
885886 dataformCoreVersion : version ,
886887 graphErrors : { } ,
888+ jitData : { } ,
887889 projectConfig : {
888890 defaultLocation : "us" ,
889891 vars : {
@@ -1593,6 +1595,117 @@ assert("name", {
15931595 } ) ;
15941596 } ) ;
15951597
1598+ suite ( "jitData" , ( ) => {
1599+ test ( "jitData is added to the compiled graph" , ( ) => {
1600+ const projectDir = tmpDirFixture . createNewTmpDir ( ) ;
1601+ fs . writeFileSync (
1602+ path . join ( projectDir , "workflow_settings.yaml" ) ,
1603+ VALID_WORKFLOW_SETTINGS_YAML
1604+ ) ;
1605+ fs . mkdirSync ( path . join ( projectDir , "definitions" ) ) ;
1606+ fs . writeFileSync (
1607+ path . join ( projectDir , "definitions/jit.js" ) ,
1608+ `
1609+ dataform.jitData("key", {
1610+ "number": 123,
1611+ "string": "value",
1612+ "boolean": true,
1613+ "struct": {
1614+ "nestedKey": "nestedValue"
1615+ },
1616+ "list": [
1617+ "a",
1618+ "b",
1619+ "c"
1620+ ],
1621+ "null": null,
1622+ "undef": undefined,
1623+ });`
1624+ ) ;
1625+ const result = runMainInVm ( coreExecutionRequestFromPath ( projectDir ) ) ;
1626+
1627+ expect ( result . compile . compiledGraph . graphErrors . compilationErrors ) . deep . equals ( [ ] ) ;
1628+ expect ( result . compile . compiledGraph . jitData ) . to . deep . equal (
1629+ google . protobuf . Struct . create ( {
1630+ fields : {
1631+ key : google . protobuf . Value . create ( {
1632+ structValue : google . protobuf . Struct . create ( {
1633+ fields : {
1634+ number : google . protobuf . Value . create ( { numberValue : 123 } ) ,
1635+ string : google . protobuf . Value . create ( { stringValue : "value" } ) ,
1636+ boolean : google . protobuf . Value . create ( { boolValue : true } ) ,
1637+ struct : google . protobuf . Value . create ( {
1638+ structValue : google . protobuf . Struct . create ( {
1639+ fields : {
1640+ nestedKey : google . protobuf . Value . create ( { stringValue : "nestedValue" } )
1641+ }
1642+ } )
1643+ } ) ,
1644+ list : google . protobuf . Value . create ( {
1645+ listValue : google . protobuf . ListValue . create ( {
1646+ values : [
1647+ google . protobuf . Value . create ( { stringValue : "a" } ) ,
1648+ google . protobuf . Value . create ( { stringValue : "b" } ) ,
1649+ google . protobuf . Value . create ( { stringValue : "c" } )
1650+ ]
1651+ } )
1652+ } ) ,
1653+ null : google . protobuf . Value . create ( {
1654+ nullValue : google . protobuf . NullValue . NULL_VALUE
1655+ } ) ,
1656+ undef : google . protobuf . Value . create ( {
1657+ nullValue : google . protobuf . NullValue . NULL_VALUE
1658+ } ) ,
1659+ }
1660+ } )
1661+ } )
1662+ }
1663+ } )
1664+ ) ;
1665+ } ) ;
1666+
1667+ test ( "jitData with duplicate key throws error" , ( ) => {
1668+ const projectDir = tmpDirFixture . createNewTmpDir ( ) ;
1669+ fs . writeFileSync (
1670+ path . join ( projectDir , "workflow_settings.yaml" ) ,
1671+ VALID_WORKFLOW_SETTINGS_YAML
1672+ ) ;
1673+ fs . mkdirSync ( path . join ( projectDir , "definitions" ) ) ;
1674+ fs . writeFileSync (
1675+ path . join ( projectDir , "definitions/jit.js" ) ,
1676+ `
1677+ dataform.jitData("key", 1);
1678+ dataform.jitData("key", 2);
1679+ `
1680+ ) ;
1681+ const result = runMainInVm ( coreExecutionRequestFromPath ( projectDir ) ) ;
1682+
1683+ expect (
1684+ result . compile . compiledGraph . graphErrors . compilationErrors . map ( e => e . message )
1685+ ) . to . deep . equal ( [ "JiT context data with key key already exists." ] ) ;
1686+ } ) ;
1687+
1688+ test ( "jitData with unsupported type throws error" , ( ) => {
1689+ const projectDir = tmpDirFixture . createNewTmpDir ( ) ;
1690+ fs . writeFileSync (
1691+ path . join ( projectDir , "workflow_settings.yaml" ) ,
1692+ VALID_WORKFLOW_SETTINGS_YAML
1693+ ) ;
1694+ fs . mkdirSync ( path . join ( projectDir , "definitions" ) ) ;
1695+ fs . writeFileSync (
1696+ path . join ( projectDir , "definitions/jit.js" ) ,
1697+ `
1698+ dataform.jitData("key", {test: () => {}});
1699+ `
1700+ ) ;
1701+ const result = runMainInVm ( coreExecutionRequestFromPath ( projectDir ) ) ;
1702+
1703+ expect (
1704+ result . compile . compiledGraph . graphErrors . compilationErrors . map ( e => e . message )
1705+ ) . to . deep . equal ( [ "Unsupported context object: () => {}" ] ) ;
1706+ } ) ;
1707+ } ) ;
1708+
15961709 suite ( "invalid options" , ( ) => {
15971710 [
15981711 {
0 commit comments