Skip to content

Commit df8cb18

Browse files
JiT context - decode data (#2111)
Instead of exposing a proto struct, returning (mostly) the same object that got passed in the original key at definition.
1 parent 270cafd commit df8cb18

2 files changed

Lines changed: 104 additions & 3 deletions

File tree

core/jit_context.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IActionContext, ITableContext, JitContext, Resolvable } from "df/core/contextables";
22
import { ambiguousActionNameMsg, resolvableAsTarget, ResolvableMap, stringifyResolvable, toResolvable } from "df/core/utils";
3-
import { dataform } from "df/protos/ts";
3+
import { dataform, google } from "df/protos/ts";
44

55
function canonicalTargetValue(target: dataform.ITarget): string {
66
return `${target.database}.${target.schema}.${target.name}`;
@@ -24,7 +24,7 @@ export class SqlActionJitContext implements JitContext<IActionContext> {
2424
actionTarget: dep,
2525
value: canonicalTargetValue(dep)
2626
})));
27-
this.data = request.jitData;
27+
this.data = jitDataToJsValue(request.jitData);
2828
}
2929

3030
public self(): string {
@@ -102,3 +102,47 @@ export class IncrementalTableJitContext extends TableJitContext {
102102
return this.isIncrementalContext;
103103
}
104104
}
105+
106+
function jitDataToJsValue(value?: google.protobuf.IStruct): { [key: string]: {} } | undefined {
107+
if (value === undefined || value === null) {
108+
return
109+
}
110+
function protobufValueToJs(val: google.protobuf.IValue): {} {
111+
if (val.nullValue != null) {
112+
return null;
113+
}
114+
if (val.stringValue != null) {
115+
return val.stringValue;
116+
}
117+
if (val.numberValue != null) {
118+
return val.numberValue;
119+
}
120+
if (val.boolValue != null) {
121+
return val.boolValue;
122+
}
123+
if (val.listValue != null) {
124+
return (val.listValue.values || []).map(protobufValueToJs);
125+
}
126+
if (val.structValue != null) {
127+
return Object.fromEntries(
128+
Object.entries(val.structValue.fields || {}).map(
129+
([fieldKey, fieldValue]) => ([
130+
fieldKey,
131+
protobufValueToJs(fieldValue)
132+
])
133+
)
134+
);
135+
}
136+
137+
throw new Error(`Unsupported protobuf value: ${JSON.stringify(val)}`);
138+
}
139+
140+
return Object.fromEntries(
141+
Object.entries(value.fields || {}).map(
142+
([fieldKey, fieldValue]) => [
143+
fieldKey,
144+
protobufValueToJs(fieldValue)
145+
]
146+
)
147+
);
148+
}

core/jit_context_test.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
import { expect } from "chai";
22

33
import { IncrementalTableJitContext, SqlActionJitContext, TableJitContext } from "df/core/jit_context";
4-
import { dataform } from "df/protos/ts";
4+
import { dataform, google } from "df/protos/ts";
55
import { suite, test } from "df/testing";
66

77
suite("jit_context", () => {
88
suite("SqlActionJitContext", () => {
99
const adapter = {} as dataform.DbAdapter;
10+
const jitData = google.protobuf.Struct.create({
11+
fields: {
12+
key: google.protobuf.Value.create({
13+
structValue: google.protobuf.Struct.create({
14+
fields: {
15+
number: google.protobuf.Value.create({ numberValue: 123 }),
16+
string: google.protobuf.Value.create({ stringValue: "value" }),
17+
boolean: google.protobuf.Value.create({ boolValue: true }),
18+
struct: google.protobuf.Value.create({
19+
structValue: google.protobuf.Struct.create({
20+
fields: {
21+
nestedKey: google.protobuf.Value.create({ stringValue: "nestedValue" })
22+
}
23+
})
24+
}),
25+
list: google.protobuf.Value.create({
26+
listValue: google.protobuf.ListValue.create({
27+
values: [
28+
google.protobuf.Value.create({ stringValue: "a" }),
29+
google.protobuf.Value.create({ stringValue: "b" }),
30+
google.protobuf.Value.create({ stringValue: "c" })
31+
]
32+
})
33+
}),
34+
null: google.protobuf.Value.create({
35+
nullValue: google.protobuf.NullValue.NULL_VALUE
36+
}),
37+
undef: google.protobuf.Value.create({
38+
nullValue: google.protobuf.NullValue.NULL_VALUE
39+
}),
40+
}
41+
})
42+
})
43+
}
44+
});
1045
const request = dataform.JitCompilationRequest.create({
1146
target: dataform.Target.create({
1247
database: "db",
@@ -21,6 +56,7 @@ suite("jit_context", () => {
2156
})
2257
],
2358
filePaths: [],
59+
jitData,
2460
});
2561
const withoutDependenciesRequest = dataform.JitCompilationRequest.create({
2662
target: dataform.Target.create({
@@ -76,6 +112,27 @@ suite("jit_context", () => {
76112
const context = new SqlActionJitContext(adapter, withoutDependenciesRequest);
77113
expect(context.database()).to.equal("db");
78114
});
115+
116+
test("data", () => {
117+
const context = new SqlActionJitContext(adapter, request);
118+
expect(context.data).to.deep.equal({
119+
"key": {
120+
"number": 123,
121+
"string": "value",
122+
"boolean": true,
123+
"struct": {
124+
"nestedKey": "nestedValue"
125+
},
126+
"list": [
127+
"a",
128+
"b",
129+
"c"
130+
],
131+
"null": null,
132+
"undef": null,
133+
}
134+
});
135+
});
79136
});
80137

81138
suite("TableJitContext", () => {

0 commit comments

Comments
 (0)