From 06d391ea74db5c327279eea31233589d85c84e40 Mon Sep 17 00:00:00 2001 From: Tyler Gregg Date: Tue, 10 Dec 2024 15:43:19 -0800 Subject: [PATCH] Makes the Ion 1.1 text writer write symbol tokens inline by default, instead of using symbol identifiers. --- .../_Private_IonTextWriterBuilder_1_1.java | 2 +- .../ion/impl/bin/ManagedWriterOptions_1_1.kt | 20 ---------- .../EncodingDirectiveCompilationTest.java | 38 ++++++++++++++++++- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/_Private_IonTextWriterBuilder_1_1.java b/src/main/java/com/amazon/ion/impl/_Private_IonTextWriterBuilder_1_1.java index 4a5f39bd4..f6166f998 100644 --- a/src/main/java/com/amazon/ion/impl/_Private_IonTextWriterBuilder_1_1.java +++ b/src/main/java/com/amazon/ion/impl/_Private_IonTextWriterBuilder_1_1.java @@ -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(); diff --git a/src/main/java/com/amazon/ion/impl/bin/ManagedWriterOptions_1_1.kt b/src/main/java/com/amazon/ion/impl/bin/ManagedWriterOptions_1_1.kt index 2fa693d2d..4c940f4fe 100644 --- a/src/main/java/com/amazon/ion/impl/bin/ManagedWriterOptions_1_1.kt +++ b/src/main/java/com/amazon/ion/impl/bin/ManagedWriterOptions_1_1.kt @@ -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 diff --git a/src/test/java/com/amazon/ion/impl/EncodingDirectiveCompilationTest.java b/src/test/java/com/amazon/ion/impl/EncodingDirectiveCompilationTest.java index 27a41e4ea..1a75ced10 100644 --- a/src/test/java/com/amazon/ion/impl/EncodingDirectiveCompilationTest.java +++ b/src/test/java/com/amazon/ion/impl/EncodingDirectiveCompilationTest.java @@ -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; @@ -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) ); }