Skip to content

Add support for parsing to dynamic - #11

Merged
lpil merged 25 commits into
lpil:mainfrom
ollien:dynamic
Apr 30, 2026
Merged

Add support for parsing to dynamic#11
lpil merged 25 commits into
lpil:mainfrom
ollien:dynamic

Conversation

@ollien

@ollien ollien commented Nov 30, 2025

Copy link
Copy Markdown
Contributor

Fixes #10

This PR adds the ability to either parse directly to a dynamic (parse_dynamic) or convert an existing TOML document to a dynamic (to_dynamic). I had to do some dynamic building like we discussed on Discord, so hopefully this matches with what we discussed 😅. I've also added decoders for all custom types, so that callers do not have to use the known forms themselves if they don't want to.

I've also added a JS test target to the CI, since there's FFI involved now.

Let me know if you'd like anything fixed up!

@ollien

ollien commented Dec 7, 2025

Copy link
Copy Markdown
Contributor Author

Hi @lpil! Can I bump you about this PR?

@lpil lpil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Thank you for this! Will be super useful.

I've left some notes inline. One of the main things is that new_primitive_decoder is being used a lot instead of the regular decode API. new_primitive_decoder is for decoding primitives via FFI, it's not needed to compose decoders, they already compose!

Comment thread .github/workflows/test.yml Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom_ffi.erl
Comment thread src/tom_ffi.erl Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam
Comment thread src/tom.gleam
Comment thread src/tom.gleam Outdated
}
}

/// Convert a parsed TOML document into a `Dynamic`.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fill in this documentation with what it is for please 🙏

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I filled this out for both to_dynamic and parse_to_dynamic. Let me know what you think :)

Comment thread src/tom.gleam Outdated
}

/// A convenience for parsing a TOML document and immediately converting it to a Dynamic
pub fn parse_dynamic(input: String) -> Result(Dynamic, ParseError) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, more documentation please.

Also let's call it parse_to_dynamic, to make it a bit clearer 🙏

@ollien

ollien commented Jan 26, 2026

Copy link
Copy Markdown
Contributor Author

@lpil I've addressed your feedback and given this a rebase, when you have a chance!

@lpil lpil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you! I've left some very small notes inline 🙏

Comment thread .github/workflows/test.yml Outdated
Comment thread src/tom.gleam Outdated

@external(erlang, "tom_ffi", "infinity_to_dynamic")
@external(javascript, "./tom_ffi.mjs", "infinity_to_dynamic")
fn infinity_to_dynamic(infinity: InfinityValue) -> Dynamic

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than create new types and making them internal you can use a bool as the argument instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can still use sign though yeah?

Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam
Comment thread test/tom_test.gleam
}

assert decode.run(dynamic, decoder)
== Error([decode.DecodeError("DateTime with offset", "Dict", ["a"])])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love that this says it found a dict? But I don't think there's a better way to say that we found a DateTime with a local offset with the decoder API.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dynamic is structural, and there's no dedicated date time type, so it'll be a dict.

@lpil lpil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant, thank you. Few tiny things inline.

Comment thread test/tom_test.gleam Outdated
Comment thread test/tom_test.gleam Outdated
Comment thread test/tom_test.gleam Outdated
Comment thread src/tom.gleam
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam Outdated
Comment thread src/tom.gleam Outdated
@ollien
ollien requested a review from lpil March 15, 2026 17:12

@lpil lpil left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@lpil
lpil merged commit 33e3163 into lpil:main Apr 30, 2026
1 check passed
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.

Add dynamic decoding

2 participants