Skip to content

Commit

Permalink
Better separate strategies from converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Aug 13, 2024
1 parent c8d6cb0 commit 500105f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
3 changes: 1 addition & 2 deletions lib/Compressor.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ export class Compressor {
return activeCtx;
}

convertValue({termType, value, termInfo}) {
convertValue({termType, value, termInfo, converter}) {
if(typeof value === 'object') {
return;
}
const {converter} = this;
return ValueEncoder.createEncoder({value, converter, termInfo, termType});
}

Expand Down
13 changes: 7 additions & 6 deletions lib/Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ export class Converter {
this.contextLoader = contextLoader;
this.initialActiveCtx = new ActiveContext({contextLoader});

// FIXME: expose differently
// FIXME: consider moving to strategies for better separation of concerns
this.typeTableEncodedAsBytesSet = legacy ?
LEGACY_TYPE_TABLE_ENCODED_AS_BYTES : TYPE_TABLE_ENCODED_AS_BYTES;

// FIXME: eliminate cyclical reference
strategy.converter = this;
}

/**
Expand Down Expand Up @@ -137,7 +134,9 @@ export class Converter {
activeCtx = await strategy.convertContexts({activeCtx, input, output});

// get unique `@type` (and alias) values for the input
const objectTypes = strategy.getObjectTypes({activeCtx, input, output});
const objectTypes = strategy.getObjectTypes({
activeCtx, input, output, converter: this
});

// apply type-scoped contexts
activeCtx = await activeCtx.applyTypeScopedContexts({objectTypes});
Expand Down Expand Up @@ -173,7 +172,9 @@ export class Converter {
return null;
}
// convert value via strategy if possible
let output = this.strategy.convertValue({termType, value, termInfo});
let output = this.strategy.convertValue({
termType, value, termInfo, converter: this
});
if(output !== undefined) {
return output;
}
Expand Down
6 changes: 2 additions & 4 deletions lib/Decompressor.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ export class Decompressor {
return activeCtx.applyEmbeddedContexts({obj: output});
}

convertValue({termType, value, termInfo}) {
convertValue({termType, value, termInfo, converter}) {
if(value instanceof Map) {
return;
}
const {converter} = this;
const decoder = ValueDecoder.createDecoder({
value, converter, termInfo, termType
});
Expand All @@ -103,10 +102,9 @@ export class Decompressor {
return entries.sort(_sortEntriesByTerm);
}

getObjectTypes({activeCtx, input}) {
getObjectTypes({activeCtx, input, converter}) {
const objectTypes = new Set();
// must decode object types to get their original values
const {converter} = this;
const typeTerms = activeCtx.getTypeTerms();
for(const typeTerm of typeTerms) {
// check for encoded singular and plural term IDs
Expand Down

0 comments on commit 500105f

Please sign in to comment.