Skip to content

Comments

WIP: Fix a code generation edge case where a oneof is named "result"#368

Draft
okkero wants to merge 1 commit intoanmolitor:masterfrom
okkero:bugfix/oneof-result
Draft

WIP: Fix a code generation edge case where a oneof is named "result"#368
okkero wants to merge 1 commit intoanmolitor:masterfrom
okkero:bugfix/oneof-result

Conversation

@okkero
Copy link

@okkero okkero commented Nov 6, 2025

When a oneof is named "result" (or possibly any other Elm prelude name), the code generator tries to add underscores at the end of the name to avoid conflicts. However, sometimes in longer path names in Internals_.elm the underscore is added after a period, not before as it should (e.g. Foo.Result._Code vs Foo.Result_.Code).

This PR is not a complete fix yet. I have only made a test to demonstrate the issue. The fact that Mapper.Name.escapeType appends one underscore and Mapper.Name.internalize intersperses double underscores makes it a bit problematic to restore the original values in Mapper.Name.externalize. You end up with internal names like Proto__OneofResult__Foo__Result___Code (notice the triple underscore at the end), and since String.split works eagerly, externalize ends up outputting Proto.OneofResult.Foo.Result._Code instead of Proto.OneofResult.Foo.Result_.Code.

The reason I haven't implemented a complete fix yet is that I was unsure about which approach I should take, and I would like some input before I proceed. I considered changing externalize to the following, but that felt a bit too hacky:

externalize : String -> ( ModuleName, String )
externalize =
    String.reverse
        >> String.split "__"
        >> List.map String.reverse
        >> List.reverse
        >> List.Extra.unconsLast
        >> Maybe.map
            (\( last, rest ) -> ( rest, last ))
        >> Maybe.withDefault ( [], "" )

Another alternative would be to change the reserved names suffix from _ to something like 0, but I'm not sure how that would interact with everything else. The best approach might just be to use an alternative split implementation so that split "__" "A___B" splits into ["A_", "B"] instead of ["A", "_B"].

Thoughts? @anmolitor

When a oneof is named "result" (or possibly any other Elm prelude name),
the code generator tries to add underscores at the end of the name to
avoid conflicts. However, sometimes in longer path names in
Internals_.elm the underscore is added after a period, not before as it
should (e.g. Foo.Result._Code vs Foo.Result_.Code).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant