Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for macro table appends in binary 1.1 reader #978

Closed
jobarr-amzn opened this issue Oct 21, 2024 · 2 comments
Closed

Support for macro table appends in binary 1.1 reader #978

jobarr-amzn opened this issue Oct 21, 2024 · 2 comments
Labels

Comments

@jobarr-amzn
Copy link
Contributor

This test case fails when added to EncodingDirectiveCompilationTest:

    @ParameterizedTest(name = "{0},{1}")
    @MethodSource("allCombinations")
    public void macroAppendWithoutMacros(InputType inputType, StreamType streamType) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IonRawWriter_1_1 writer = streamType.newWriter(out);
        writer.writeIVM();

        startEncodingDirective(writer);
        startMacroTable(writer);
        writer.writeSymbol(SystemSymbols_1_1.ION_ENCODING);
        endMacroTable(writer);
        endEncodingDirective(writer);

        byte[] data = getBytes(writer, out);
        try (IonReader reader = inputType.newReader(data)) {
           assertNull(reader.next());
        }
    }

This is equivalent to:

$ion_encoding::( (macro_table $ion_encoding) )
@jobarr-amzn
Copy link
Contributor Author

Partially fixed by #976, but now this test fails:

    @ParameterizedTest(name = "{0},{1}")
    @MethodSource("allCombinations")
    public void macroAppendWithoutMacros(InputType inputType, StreamType streamType) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IonRawWriter_1_1 writer = streamType.newWriter(out);
        writer.writeIVM();

        SortedMap<String, Macro> macroTable = new TreeMap<>();
        macroTable.put("foo", new TemplateMacro(
          Collections.singletonList(new Macro.Parameter("foo", Macro.ParameterEncoding.Tagged, Macro.ParameterCardinality.ExactlyOne)),
          Collections.singletonList(new Expression.VariableRef(0))
        ));
        Map<String, Integer> symbols = Collections.emptyMap();

        // $ion_encoding::(
        //   ( macro_table (macro foo (x) (%x) ) )
        // )
        startEncodingDirective(writer); {
            startMacroTable(writer); {
                startMacro(writer, symbols, "foo"); {
                    writeMacroSignature(writer, symbols, "x");
                    writeVariableExpansion(writer, symbols, "x");
                } endMacro(writer);
            } endMacroTable(writer);
        } endEncodingDirective(writer);
        
        // $ion_encoding::(
        //   ( macro_table $ion_encoding )
        //   ( symbol_table ["bar"])
        // )
        startEncodingDirective(writer); {
            startMacroTable(writer); {
                writer.writeSymbol(SystemSymbols_1_1.ION_ENCODING);
            } endMacroTable(writer);
            writeEncodingDirectiveSymbolTable(writer, true, "bar");
        } endEncodingDirective(writer);

        // (:0 $1) i.e. (:foo bar)
        writer.stepInEExp(0, true, macroTable.get("foo")); {
            writer.writeSymbol(1);
        } writer.stepOut();

        byte[] data = getBytes(writer, out);
        try (IonReader reader = inputType.newReader(data)) {
           assertEquals(IonType.SYMBOL, reader.next());
           assertEquals("bar", reader.stringValue());
        }
    }

@jobarr-amzn
Copy link
Contributor Author

The problem comes from this line, where newMacros is installed as the encoding context macro map. newMacros is a field of the EncodingDirectiveReader, which retains the reference and clears the map the before it reads the next encoding directive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant