|
| 1 | +import { verifyObjectMatchesProto, VerifyProtoErrorBehaviour } from "df/common/protos"; |
| 2 | +import { ActionBuilder } from "df/core/actions"; |
| 3 | +import { Resolvable } from "df/core/common"; |
| 4 | +import * as Path from "df/core/path"; |
| 5 | +import { Session } from "df/core/session"; |
| 6 | +import { |
| 7 | + addDependenciesToActionDependencyTargets, |
| 8 | + nativeRequire, |
| 9 | + resolveActionsConfigFilename |
| 10 | +} from "df/core/utils"; |
| 11 | +import { dataform } from "df/protos/ts"; |
| 12 | + |
| 13 | +/** |
| 14 | + * @hidden |
| 15 | + */ |
| 16 | +export class DataPreparation extends ActionBuilder<dataform.DataPreparation> { |
| 17 | + public session: Session; |
| 18 | + |
| 19 | + // TODO: make this field private, to enforce proto update logic to happen in this class. |
| 20 | + public proto: dataform.IDataPreparation = dataform.DataPreparation.create(); |
| 21 | + |
| 22 | + constructor( |
| 23 | + session?: Session, |
| 24 | + config?: dataform.ActionConfig.DataPreparationConfig, |
| 25 | + configPath?: string |
| 26 | + ) { |
| 27 | + super(session); |
| 28 | + this.session = session; |
| 29 | + |
| 30 | + if (!config.name) { |
| 31 | + config.name = Path.basename(config.filename); |
| 32 | + } |
| 33 | + |
| 34 | + config.filename = resolveActionsConfigFilename(config.filename, configPath); |
| 35 | + const dataPreparationContents = nativeRequire(config.filename).asJson; |
| 36 | + const dataPreparationDefinition = parseDataPreparationDefinitionJson(dataPreparationContents); |
| 37 | + this.proto.dataPreparation = dataPreparationDefinition; |
| 38 | + |
| 39 | + // Find targets |
| 40 | + const targets = getTargets(dataPreparationDefinition) |
| 41 | + this.proto.targets = targets.map((target) => this.applySessionToTarget( |
| 42 | + target, |
| 43 | + session.projectConfig, |
| 44 | + config.filename, |
| 45 | + true |
| 46 | + )); |
| 47 | + this.proto.canonicalTargets = targets.map((target) => this.applySessionToTarget(target, session.canonicalProjectConfig)); |
| 48 | + |
| 49 | + // Set the unique target key as the first target defined. |
| 50 | + // TODO: Remove once multiple targets are supported. |
| 51 | + this.proto.target = this.proto.targets[0]; |
| 52 | + this.proto.canonicalTarget = this.proto.canonicalTargets[0]; |
| 53 | + |
| 54 | + this.proto.tags = config.tags; |
| 55 | + this.dependencies(config.dependencyTargets); |
| 56 | + this.proto.fileName = config.filename; |
| 57 | + if (config.disabled) { |
| 58 | + this.proto.disabled = config.disabled; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @hidden |
| 64 | + */ |
| 65 | + public config(config: any) { |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @hidden |
| 71 | + */ |
| 72 | + public dependencies(value: Resolvable | Resolvable[]) { |
| 73 | + const newDependencies = Array.isArray(value) ? value : [value]; |
| 74 | + newDependencies.forEach(resolvable => |
| 75 | + addDependenciesToActionDependencyTargets(this, resolvable) |
| 76 | + ); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @hidden |
| 82 | + */ |
| 83 | + public getFileName() { |
| 84 | + return this.proto.fileName; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @hidden |
| 89 | + */ |
| 90 | + public getTarget() { |
| 91 | + // Return only the first target for now. |
| 92 | + return dataform.Target.create(this.proto.target); |
| 93 | + } |
| 94 | + |
| 95 | + public compile() { |
| 96 | + return verifyObjectMatchesProto( |
| 97 | + dataform.DataPreparation, |
| 98 | + this.proto, |
| 99 | + VerifyProtoErrorBehaviour.SUGGEST_REPORTING_TO_DATAFORM_TEAM |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +function parseDataPreparationDefinitionJson( |
| 105 | + dataPreparationAsJson: { [key: string]: unknown }): dataform.DataPreparationDefinition { |
| 106 | + try { |
| 107 | + return dataform.DataPreparationDefinition.create( |
| 108 | + verifyObjectMatchesProto( |
| 109 | + dataform.DataPreparationDefinition, |
| 110 | + dataPreparationAsJson as { |
| 111 | + [key: string]: any; |
| 112 | + }, |
| 113 | + VerifyProtoErrorBehaviour.SHOW_DOCS_LINK |
| 114 | + ) |
| 115 | + ); |
| 116 | + } catch (e) { |
| 117 | + if (e instanceof ReferenceError) { |
| 118 | + throw ReferenceError(`Data Preparation parsing error: ${e.message}`); |
| 119 | + } |
| 120 | + throw e; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +function getTargets( |
| 125 | + definition: dataform.DataPreparationDefinition |
| 126 | +): dataform.Target[] { |
| 127 | + const targets: dataform.Target[] = []; |
| 128 | + |
| 129 | + definition.nodes.forEach(node => { |
| 130 | + const table = node.destination?.table; |
| 131 | + if (table) { |
| 132 | + const compiledGraphTarget: dataform.ITarget = { |
| 133 | + database: table.project, |
| 134 | + schema:table.dataset, |
| 135 | + name: table.table |
| 136 | + }; |
| 137 | + targets.push(dataform.Target.create(compiledGraphTarget)) |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + return targets; |
| 142 | +} |
0 commit comments