Skip to content

Commit

Permalink
Makes the Ion 1.1 text writer write symbol tokens inline by default, …
Browse files Browse the repository at this point in the history
…instead of using symbol identifiers.
  • Loading branch information
tgregg committed Dec 10, 2024
1 parent db7014a commit 06d391e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static _Private_IonTextWriterBuilder_1_1 standard() {
return new _Private_IonTextWriterBuilder_1_1.Mutable();
}

private SymbolInliningStrategy symbolInliningStrategy = SymbolInliningStrategy.NEVER_INLINE;
private SymbolInliningStrategy symbolInliningStrategy = SymbolInliningStrategy.ALWAYS_INLINE;

private _Private_IonTextWriterBuilder_1_1() {
super();
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/com/amazon/ion/impl/bin/ManagedWriterOptions_1_1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@ data class ManagedWriterOptions_1_1(
val lengthPrefixStrategy: LengthPrefixStrategy,
val eExpressionIdentifierStrategy: EExpressionIdentifierStrategy,
) : SymbolInliningStrategy by symbolInliningStrategy, LengthPrefixStrategy by lengthPrefixStrategy {
companion object {
@JvmField
val ION_BINARY_DEFAULT = ManagedWriterOptions_1_1(
internEncodingDirectiveSymbols = true,
invokeTdlMacrosByName = false,
symbolInliningStrategy = SymbolInliningStrategy.NEVER_INLINE,
lengthPrefixStrategy = LengthPrefixStrategy.ALWAYS_PREFIXED,
eExpressionIdentifierStrategy = EExpressionIdentifierStrategy.BY_ADDRESS,
)
@JvmField
val ION_TEXT_DEFAULT = ManagedWriterOptions_1_1(
// Encoding directives are easier to read if we don't intern their keywords.
internEncodingDirectiveSymbols = false,
invokeTdlMacrosByName = true,
symbolInliningStrategy = SymbolInliningStrategy.ALWAYS_INLINE,
// This doesn't actually have any effect for Ion Text since there are no length-prefixed containers.
lengthPrefixStrategy = LengthPrefixStrategy.NEVER_PREFIXED,
eExpressionIdentifierStrategy = EExpressionIdentifierStrategy.BY_NAME,
)
}

/**
* Indicates whether e-expressions should be written using macro
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.amazon.ion.MacroAwareIonWriter;
import com.amazon.ion.SystemSymbols;
import com.amazon.ion.impl.bin.IonRawBinaryWriter_1_1;
import com.amazon.ion.impl.bin.SymbolInliningStrategy;
import com.amazon.ion.impl.macro.EncodingContext;
import com.amazon.ion.impl.macro.Expression;
import com.amazon.ion.impl.macro.Macro;
Expand Down Expand Up @@ -1602,7 +1603,42 @@ public void macroInvocationsProduceEncodingDirectivesThatModifySymbolTableMacroA
substringCount(SystemSymbols_1_1.ADD_MACROS, 0),
substringCount(SystemSymbols_1_1.SET_SYMBOLS, 2),
substringCount(SystemSymbols_1_1.SET_MACROS, 0),
substringCount(SystemSymbols_1_1.ION_ENCODING, 0)
substringCount(SystemSymbols_1_1.ION_ENCODING, 0),
// Symbol tokens are written using inline text, not symbol identifiers.
substringCount("$1", 0),
substringCount("$2", 0),
substringCount("$3", 0)
);
}

@ParameterizedTest(name = "{0},{1},{2}")
@MethodSource("allInputFormatsInputTypesAndOutputFormats")
public void macroInvocationsProduceEncodingDirectivesThatModifySymbolTableMacroAwareTranscodeWithoutInlining(
InputType inputType,
StreamType inputFormat,
StreamType outputFormat
) throws Exception {
byte[] data = macroInvocationsProduceEncodingDirectivesThatModifySymbolTable(inputFormat);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (
MacroAwareIonReader reader = inputType.newMacroAwareReader(data);
MacroAwareIonWriter rewriter = (MacroAwareIonWriter) (outputFormat == StreamType.TEXT
? IonEncodingVersion.ION_1_1.textWriterBuilder().withSymbolInliningStrategy(SymbolInliningStrategy.NEVER_INLINE).build(out)
: IonEncodingVersion.ION_1_1.binaryWriterBuilder().withSymbolInliningStrategy(SymbolInliningStrategy.NEVER_INLINE).build(out))
) {
reader.transcodeAllTo(rewriter);
}
verifyStream(data, out, outputFormat,
substringCount("$ion_1_1", 1),
substringCount(SystemSymbols_1_1.ADD_SYMBOLS, 1),
substringCount(SystemSymbols_1_1.ADD_MACROS, 0),
substringCount(SystemSymbols_1_1.SET_SYMBOLS, 2),
substringCount(SystemSymbols_1_1.SET_MACROS, 0),
substringCount(SystemSymbols_1_1.ION_ENCODING, 0),
// Symbol tokens are written using symbol identifiers, not inline text.
substringCount("$1", 3),
substringCount("$2", 2),
substringCount("$3", 1)
);
}

Expand Down

0 comments on commit 06d391e

Please sign in to comment.