Skip to content

use the key as path in decode error if it is a string#910

Merged
lpil merged 2 commits intogleam-lang:mainfrom
CrowdHailer:string-key-decode
Mar 7, 2026
Merged

use the key as path in decode error if it is a string#910
lpil merged 2 commits intogleam-lang:mainfrom
CrowdHailer:string-key-decode

Conversation

@CrowdHailer
Copy link
Copy Markdown
Contributor

This changes makes working with large json data, like an openapi schema, much easier.

We previously had a patch for this in OAS CrowdHailer/oas#1.
Unfortunately using the same approach in the new decode API is not possible as the decoder type is opaque.

I had a look at a previous PR attempting to do something similar #746. I didn't follow all the points discussed. I've not updated any function internals apart from the one dealing with dictionaries.

Comment thread src/gleam/dynamic/decode.gleam Outdated
}
#(_, errors) -> push_path(#(dict.new(), errors), ["values"])
#(_, errors) -> {
let key_identifier = case run(key, one_of(string, [])) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could this pattern be extended for integer indices into arrays also?

Here is the path I am investigating in a 29K JSON file (converted from YAML!)

expected Discriminator found Dict at paths/values/get/responses/values/content/values/schema/properties/values/items

I might be able to craft some fancy jq path - but having the keys would be a great help!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, good idea!

@lpil lpil force-pushed the string-key-decode branch from 7cde40d to 740141d Compare March 7, 2026 11:27
Copy link
Copy Markdown
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you!!! Very nice improvement

@lpil lpil merged commit 0aabb0b into gleam-lang:main Mar 7, 2026
7 checks passed
@CrowdHailer CrowdHailer deleted the string-key-decode branch March 17, 2026 08:27
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.

3 participants