fix(compiler): allow serializing duration and regex in Json - #7278
Merged
Conversation
Structs with duration or regex fields can now be converted to/from Json. Duration values serialize as a string of integer milliseconds, and regex values as a JavaScript RegExp string (e.g. "/p[a-z]+ch/"). Fixes #7038
|
Thanks for opening this pull request! 🎉 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Closes #7038.
A struct with a
durationorregexfield cannot be converted to/fromJson— the compiler reported:The issue requests a standardized serialization:
durationserializes as a string of integer milliseconds, andregexas a JavaScript RegExp string (e.g./p[a-z]+ch/).What changed
wingc):Type::DurationandType::Regexare now considered to have a JSON representation (has_json_representation), so structs containing them can definefromJson/schema.duration/regexare emitted in the JSON schema as{type:"string", format:"duration"}/{type:"string", format:"regex"}so the runtime can identify and convert them.json_schema.ts):JsonSchema._fromJsonnow walks the schema alongside the parsed values and convertsduration/regexstring representations into realDuration/Regexinstances, including through nested structs, arrays, maps, and optional fields.Verification
packages/@winglang/sdk/test/std/json_schema.test.ts) cover top-level, nested struct, array, map, and optional fields; all pass.tests/valid/struct_json_serialization.test.w) exercises the issue's example end to end.wingcbuilds cleanly (cargo build).