-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation for extra properties in column descriptors (#714)
* Add validation for extra properties in column descriptors * Expand excess property testing * Add warehouse checks and fix lints * Add expect, newline eof * Bump version
- Loading branch information
Showing
13 changed files
with
368 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
IColumnsDescriptor, | ||
IRecordDescriptor, | ||
IRecordDescriptorProperties | ||
} from "@dataform/core/common"; | ||
import * as utils from "@dataform/core/utils"; | ||
import { dataform } from "@dataform/protos"; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
export class ColumnDescriptors { | ||
public static mapToColumnProtoArray(columns: IColumnsDescriptor): dataform.IColumnDescriptor[] { | ||
return utils.flatten( | ||
Object.keys(columns).map(column => | ||
ColumnDescriptors.mapColumnDescriptionToProto([column], columns[column]) | ||
) | ||
); | ||
} | ||
|
||
public static mapColumnDescriptionToProto( | ||
currentPath: string[], | ||
description: string | IRecordDescriptor | ||
): dataform.IColumnDescriptor[] { | ||
if (typeof description === "string") { | ||
return [ | ||
dataform.ColumnDescriptor.create({ | ||
description, | ||
path: currentPath | ||
}) | ||
]; | ||
} | ||
utils.checkExcessProperties( | ||
description, | ||
IRecordDescriptorProperties(), | ||
`${currentPath.join(".")} column descriptor` | ||
); | ||
const columnDescriptor: dataform.IColumnDescriptor[] = !!description | ||
? [ | ||
dataform.ColumnDescriptor.create({ | ||
path: currentPath, | ||
description: description.description, | ||
displayName: description.displayName, | ||
dimensionType: ColumnDescriptors.mapDimensionType(description.dimension), | ||
aggregation: ColumnDescriptors.mapAggregation(description.aggregator), | ||
expression: description.expression | ||
}) | ||
] | ||
: []; | ||
const nestedColumns = description.columns ? Object.keys(description.columns) : []; | ||
return columnDescriptor.concat( | ||
utils.flatten( | ||
nestedColumns.map(nestedColumn => | ||
ColumnDescriptors.mapColumnDescriptionToProto( | ||
currentPath.concat([nestedColumn]), | ||
description.columns[nestedColumn] | ||
) | ||
) | ||
) | ||
); | ||
} | ||
|
||
public static mapAggregation(aggregation: string) { | ||
switch (aggregation) { | ||
case "sum": | ||
return dataform.ColumnDescriptor.Aggregation.SUM; | ||
case "distinct": | ||
return dataform.ColumnDescriptor.Aggregation.DISTINCT; | ||
case "derived": | ||
return dataform.ColumnDescriptor.Aggregation.DERIVED; | ||
case undefined: | ||
return undefined; | ||
default: | ||
throw new Error(`'${aggregation}' is not a valid aggregation option.`); | ||
} | ||
} | ||
|
||
public static mapDimensionType(dimensionType: string) { | ||
switch (dimensionType) { | ||
case "category": | ||
return dataform.ColumnDescriptor.DimensionType.CATEGORY; | ||
case "timestamp": | ||
return dataform.ColumnDescriptor.DimensionType.TIMESTAMP; | ||
case undefined: | ||
return undefined; | ||
default: | ||
throw new Error(`'${dimensionType}' is not a valid dimension type.`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.