Skip to content

Commit

Permalink
Merge pull request #62 from ConorStokes/parser_errors_as_null
Browse files Browse the repository at this point in the history
Nullable fields with errors for parsing/dangling references return null by default with "strict" mode to enable old behaviour.
  • Loading branch information
nickcastel50 authored Oct 4, 2023
2 parents dd12122 + 1d3d4e1 commit 4fc1fe0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/ifc/ifc_command_line_main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ function doWork() {
alias: 'm',
default: 128,
})
yargs2.option('strict', {
// eslint-disable-next-line max-len
describe: 'Makes parser/reference errors on nullable fields return null instead of an error',
type: 'boolean',
alias: 's',
default: false,
})

yargs2.positional('filename', { describe: 'IFC File Paths', type: 'string' })
}, async (argv) => {
Expand All @@ -88,6 +95,7 @@ function doWork() {
const geometry = (argv['geometry'] as boolean | undefined)

const outputProperties = (argv['properties'] as boolean | undefined)
const strict = (argv['strict'] as boolean | undefined) ?? false

try {
indexIfcBuffer = fs.readFileSync(ifcFile)
Expand Down Expand Up @@ -148,6 +156,8 @@ function doWork() {
return
}

model.nullOnErrors = !strict

if (geometry) {
console.log(`Data parse time ${parseDataTimeEnd - parseDataTimeStart} ms`)
// Get the filename with extension
Expand Down
36 changes: 16 additions & 20 deletions src/step/step_entity_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !this.model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null ) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down Expand Up @@ -240,7 +240,7 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !this.model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down Expand Up @@ -279,7 +279,7 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !this.model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down Expand Up @@ -319,7 +319,7 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !this.model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down Expand Up @@ -435,16 +435,10 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
const value: Array<any> = []

for (const address of stepExtractArray(buffer, cursor, endCursor)) {
value.push((() => {
const cursor = address
const value = this.extractBufferReference(buffer, cursor, endCursor)

/* if ( !( value instanceof IfcObjectDefinition ) ) {
throw new Error( 'Value in STEP was incorrectly typed for field' )
}*/
const itemValue = this.extractBufferReference(buffer, address, endCursor)

return value
})())
value.push( itemValue )
}
return value
}, false)
Expand Down Expand Up @@ -520,7 +514,7 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !this.model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down Expand Up @@ -587,19 +581,20 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
const cursor = this.getOffsetCursor( offset )
const buffer = this.buffer
const endCursor = buffer.length
const model = this.model

const expressID = stepExtractReference(buffer, cursor, endCursor)
const value =
expressID !== void 0 ? this.model.getElementByExpressID(expressID) :
this.model.getInlineElementByAddress(
expressID !== void 0 ? model.getElementByExpressID(expressID) :
model.getInlineElementByAddress(
stepExtractInlineElemement(buffer, cursor, endCursor))

if (value === void 0) {
if (!optional) {
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down Expand Up @@ -635,10 +630,11 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
entityConstructor: T ):
InstanceType< T > | undefined {

const model = this.model
const expressID = stepExtractReference( buffer, cursor, endCursor )
const value =
expressID !== void 0 ? this.model.getElementByExpressID( expressID ) :
this.model.getInlineElementByAddress(
expressID !== void 0 ? model.getElementByExpressID( expressID ) :
model.getInlineElementByAddress(
stepExtractInlineElemement( buffer, cursor, endCursor ) )

if ( value === void 0 ) {
Expand Down Expand Up @@ -680,7 +676,7 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !this.model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down Expand Up @@ -752,7 +748,7 @@ export default abstract class StepEntityBase<EntityTypeIDs extends number> imple
throw new Error('Value in STEP was incorrectly typed')
}

if (stepExtractOptional(buffer, cursor, endCursor) !== null) {
if ( !this.model.nullOnErrors && stepExtractOptional(buffer, cursor, endCursor) !== null) {
throw new Error('Value in STEP was incorrectly typed')
}

Expand Down
6 changes: 6 additions & 0 deletions src/step/step_model_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ implements Iterable<BaseEntity>, Model {
*/
public elementMemoization: boolean = true

/**
* When an atribute is parsed from an entity in the model that causes a recoverable
* error, and the field is optional, return null instead of throwing an exception.
*/
public nullOnErrors: boolean = true

/**
* Construct this step model with its matching schema, a buffer to read from and an element index.
*
Expand Down

0 comments on commit 4fc1fe0

Please sign in to comment.