Skip to content

Commit 2465730

Browse files
authored
Added schema function (#1432)
* Removed redundant toResolvable calls * Refactored resolve/normalize in session * Added schema function to common context objects * Removed unused session import * Bumped minor version * Simplified schema assertions in core.spec.ts * Refactored normalize* => finalize* in session helpers * Refactored with/without suffix tests to run as parameterized tests * Removed redundant 'Test' import
1 parent 08f81ce commit 2465730

File tree

8 files changed

+133
-66
lines changed

8 files changed

+133
-66
lines changed

core/assertion.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,13 @@ export class AssertionContext implements ICommonContext {
202202
}
203203

204204
public resolve(ref: Resolvable | string[], ...rest: string[]) {
205-
return this.assertion.session.resolve(toResolvable(ref, rest));
205+
return this.assertion.session.resolve(ref, ...rest);
206+
}
207+
208+
public schema(): string {
209+
return this.assertion.session.finalizeSchema(
210+
this.assertion.proto.target.schema
211+
);
206212
}
207213

208214
public dependencies(name: Resolvable | Resolvable[]) {

core/common.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export interface ICommonContext {
4545
* See the `ref` function for example usage.
4646
*/
4747
resolve: (ref: Resolvable | string[], ...rest: string[]) => string;
48+
49+
/**
50+
* Returns the schema of this dataset.
51+
*/
52+
schema: () => string;
4853
}
4954

5055
/**
@@ -193,9 +198,9 @@ export interface IRecordDescriptor {
193198
*
194199
* These should be the fully qualified identifier of the tag, including the project name, location, and taxonomy,
195200
* which can be copied from the policy tags page in GCP.
196-
*
201+
*
197202
* For example: "projects/1/locations/eu/taxonomies/2/policyTags/3"
198-
*
203+
*
199204
* Currently BigQuery supports only a single tag per column.
200205
*/
201206
bigqueryPolicyTags?: string | string[];

core/operation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ export class OperationContext implements ICommonContext {
234234
}
235235

236236
public resolve(ref: Resolvable | string[], ...rest: string[]) {
237-
return this.operation.session.resolve(toResolvable(ref, rest));
237+
return this.operation.session.resolve(ref, ...rest);
238+
}
239+
240+
public schema(): string {
241+
return this.operation.session.finalizeSchema(
242+
this.operation.proto.target.schema
243+
);
238244
}
239245

240246
public dependencies(name: Resolvable | Resolvable[]) {

core/session.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,9 @@ export class Session {
245245
...resolved.proto.target,
246246
database:
247247
resolved.proto.target.database &&
248-
this.adapter().normalizeIdentifier(
249-
`${resolved.proto.target.database}${this.getDatabaseSuffixWithUnderscore()}`
250-
),
251-
schema: this.adapter().normalizeIdentifier(
252-
`${resolved.proto.target.schema}${this.getSchemaSuffixWithUnderscore()}`
253-
),
254-
name: this.adapter().normalizeIdentifier(
255-
`${this.getTablePrefixWithUnderscore()}${resolved.proto.target.name}`
256-
)
248+
this.finalizeDatabase(resolved.proto.target.database),
249+
schema: this.finalizeSchema(resolved.proto.target.schema),
250+
name: this.finalizeName(resolved.proto.target.name),
257251
});
258252
}
259253
// TODO: Here we allow 'ref' to go unresolved. This is for backwards compatibility with projects
@@ -266,27 +260,20 @@ export class Session {
266260
utils.target(
267261
this.adapter(),
268262
this.config,
269-
this.adapter().normalizeIdentifier(`${this.getTablePrefixWithUnderscore()}${ref}`),
270-
this.adapter().normalizeIdentifier(
271-
`${this.config.defaultSchema}${this.getSchemaSuffixWithUnderscore()}`
272-
),
263+
this.finalizeName(ref),
264+
this.finalizeSchema(this.config.defaultSchema),
273265
this.config.defaultDatabase &&
274-
this.adapter().normalizeIdentifier(
275-
`${this.config.defaultDatabase}${this.getDatabaseSuffixWithUnderscore()}`
276-
)
266+
this.finalizeDatabase(this.config.defaultDatabase),
277267
)
278268
);
279269
}
280270
return this.adapter().resolveTarget(
281271
utils.target(
282272
this.adapter(),
283273
this.config,
284-
this.adapter().normalizeIdentifier(`${this.getTablePrefixWithUnderscore()}${ref.name}`),
285-
this.adapter().normalizeIdentifier(`${ref.schema}${this.getSchemaSuffixWithUnderscore()}`),
286-
ref.database &&
287-
this.adapter().normalizeIdentifier(
288-
`${ref.database}${this.getDatabaseSuffixWithUnderscore()}`
289-
)
274+
this.finalizeName(ref.name),
275+
this.finalizeSchema(ref.schema),
276+
ref.database && this.finalizeName(ref.database),
290277
)
291278
);
292279
}
@@ -444,6 +431,21 @@ export class Session {
444431
return encode64(dataform.CompiledGraph, this.compile());
445432
}
446433

434+
public finalizeDatabase(database: string): string {
435+
return this.adapter().normalizeIdentifier(
436+
`${database}${this.getDatabaseSuffixWithUnderscore()}`);
437+
}
438+
439+
public finalizeSchema(schema: string): string {
440+
return this.adapter().normalizeIdentifier(
441+
`${schema}${this.getSchemaSuffixWithUnderscore()}`);
442+
}
443+
444+
public finalizeName(name: string): string {
445+
return this.adapter().normalizeIdentifier(
446+
`${this.getTablePrefixWithUnderscore()}${name}`);
447+
}
448+
447449
private getDatabaseSuffixWithUnderscore() {
448450
return !!this.config.databaseSuffix ? `_${this.config.databaseSuffix}` : "";
449451
}

core/table.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,11 @@ export class TableContext implements ITableContext {
787787
}
788788

789789
public resolve(ref: Resolvable | string[], ...rest: string[]) {
790-
return this.table.session.resolve(toResolvable(ref, rest));
790+
return this.table.session.resolve(ref, ...rest);
791+
}
792+
793+
public schema(): string {
794+
return this.table.session.finalizeSchema(this.table.proto.target.schema);
791795
}
792796

793797
public type(type: TableType) {

core/test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ class RefReplacingContext implements ITableContext {
178178
return "";
179179
}
180180

181+
public schema() {
182+
return "";
183+
}
184+
181185
public where(where: Contextable<ITableContext, string>) {
182186
return "";
183187
}

0 commit comments

Comments
 (0)