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

Yaml2json types #95

Merged
merged 52 commits into from
Jul 10, 2024
Merged

Yaml2json types #95

merged 52 commits into from
Jul 10, 2024

Conversation

bitdivine
Copy link
Member

@bitdivine bitdivine commented Jun 9, 2024

Motivation

yaml2candid is very incomplete and still in some places buggy. Specifically, it does not support many Candid types and the opt test vectors (and implementation that was guided by those test vectors) are wrong.

Changes

  • Restructure the main yaml2json type match so that the compiler will complain when not all types are matched.
  • Add YAML to Candid conversions for almost all missing Candid types. Service and class are still unsupported. I am running out of time and these are both complicated and not needed for my immediate goal.
  • Add lots of tests.
  • Fix the existing test vector that didn't include opt in the candid.

Tests

  • Fairly comprehensive unit tests are included for each individual type.
  • Test coverage was checked with Tarpaulin; the tarpaulin report is generated by CI and can be checked.
  • Most but not all error branches are tested.

TODO in future

  • Ensure that failures have useful error messages.
  • Use proptests to ensure that no corner cases have been overlooked.
  • Perhaps get more test vectors from real-life use to ensure that the test vectors are consistent with conventional usage.

@@ -67,8 +84,8 @@ impl Yaml2Candid {
/// * YAML types do not match the IDL types.
/// * `type_name` is not defined in the converter's `.did` file.
pub fn convert(&self, typ: &IDLType, data: &YamlValue) -> anyhow::Result<IDLValue> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Here the match includes just the Candid type, so that the compiler will complain if there are any unsupported Candid types.

Below many types that were previously unsupported have been added.


/// Converts YAML to Candid using a given did file.
pub struct Yaml2Candid {
/// The types that the converter supports, defined in an IDLProg interface definition.
pub prog: IDLProg,
}
impl Default for Yaml2Candid {
Copy link
Member Author

Choose a reason for hiding this comment

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

Simple default constructor needed for tests. Included here as it may have more uses.

num-bigint = { workspace = true }

[dev-dependencies]
pretty_assertions = "1.4.0"
Copy link
Member Author

Choose a reason for hiding this comment

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

Provides a pretty diff in assert_eq!() errors. This makes debugging much easier.

@@ -16,3 +16,4 @@ version = "0.10.1"
[workspace.dependencies]
candid = { version = "0.10.8" }
candid_parser = { version = "0.1.0" }
num-bigint = { version = "0.4.5" }
Copy link
Member Author

Choose a reason for hiding this comment

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

Needed when converting BigNum types.

@@ -1,60 +1,60 @@
record {
Copy link
Member Author

Choose a reason for hiding this comment

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

In the .did file, many fields below are optional. I believe that, strictly speaking, they should therefore be opt even if parsers might accept either.

@bitdivine bitdivine marked this pull request as ready for review June 9, 2024 21:30
@bitdivine bitdivine requested a review from a team as a code owner June 9, 2024 21:30
@bitdivine bitdivine requested review from ninegua and chenyan-dfinity and removed request for a team June 9, 2024 21:30
@bitdivine bitdivine merged commit 2f5a1be into main Jul 10, 2024
8 checks passed
@bitdivine bitdivine deleted the yaml2json-types branch July 10, 2024 17:10
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.

2 participants