-
Notifications
You must be signed in to change notification settings - Fork 137
JSON support #345
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
JSON support #345
Conversation
so that we can get better type information than simply `any` in the .ts/flow type annotations. This is a multi-phase process: 1) Generate (using `schemars` traits) .json schemas for all types. This is done by running the program in rust/json-gen which will export these to rust/json-gen/schemas 2) Running scripts/run-json2ts.js to convert these to .ts files 3) We will need to convert the names to add in the JSON suffix to these (not implemented yet) 4) Running scripts/json-ts-types.js to replace any `any` return types with the correct *JSON type generated above some files are hardcoded so they don't line up right now and some steps aren't working as the schemas generated in 1) inline ALL referenced schemas within a single schema so in step 2) we end up with a lot of duplicate type definitions. We might be able to just have a stage to remove duplicates from the schemas, or maybe there's a way to change the json to ts tool to not do that. Some types also throw runtime errors when serializing e.g. non-string keys despite compiling fine since serde != serde_json
|
More byte types should probably be manually declared as hex strings too since schemars/serde default to a number array. |
| rand_os = "0.1" | ||
| rand_chacha = "0.1" | ||
|
|
||
| [build-dependencies] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can probably get rid of these. I was experimenting with rust build scripts but decided to just have a subcrate to run the script instead since I couldn't access the main crate from it (but I've never worked with rust build scripts so maybe it's possible). It makes more sense to only run these when we do the wasm builds/flowgen I think though anyway.
-get rid of [k: string]: unknown in structs -add js:ts-json-gen command that runs all stages of rust->schemas->ts gen -fixes for some runtime errors (maps) -adding some missing json impls
All definitions are wiped from schemas now and all references to those local definitions are transformed into external definitions which we pass in a param to the schema compiler to not generate definitions for.
Fixes for: -Strings/empty MapOf_ defs -to_js_value() to actually be a JsValue instead of to_json() -swap out type defs in .ts with the JSON ones -get rid of infinite recursion in certain types (no longer using external $refs) -finalize npm build commands -probably some other fixes
| # wasm | ||
| [target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies] | ||
| wasm-bindgen = "=0.2.78" | ||
| wasm-bindgen = { version = "=0.2.78", features = ["serde-serialize"] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SebastienGllmt I had to use this to construct JsValue from a serde-serialization value (I didn't see a way from JSON strings either without making the object a string itself using JsValue::from_str()). I had to do this as otherwise you just end up with a string return type. I'm still confused why we were manually specifying types like ErgoBoxJSON in our flow types when we used sigma-rust from Yoroi since those serde_json calls are the same as I used here in to_json() which definitely return a string (tested from wasm). That's why I put in both to_json() and to_js_value().
Did we have some magic conversion while ignoring the true type going on for sigma-rust that ran JSON.parse()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To answer your question, no we didn't do anything special.
|
|
||
| // just for now we'll do json-in-json until I can figure this out better | ||
| // TODO: maybe not generate this? or how do we do this? | ||
| impl JsonSchema for TransactionMetadatum { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the best solution but the options are either:
- Just make our own (incompatible with cardano-node) JSON rep using schemars/serde
- Manually write .ts defs for the cardano-node JSON schema we follow (I picked
DetailedSchemasince it supports everything (besides edge-cases withi64in serde-json) and merge those in as a final step. We'd want to do the same with plutus datums I guess if we go this route.
I'm leaning towards 2 but wanted to get opinions on it. We might have to write them as JSON schemas and generate from there and hope we can get it to match the .ts we want as otherwise other schemas referring to metadata (e.g. tx) might break.
|
|
||
| #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] | ||
| pub fn to_js_value(&self) -> Result<JsValue, JsError> { | ||
| JsValue::from_serde(&self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above in the cargo.toml
| } | ||
| } | ||
|
|
||
| impl serde::Serialize for BigNum { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to_json()/to_js_value()/from_json() here (and BigInt, etc) for consistency even though to_str()/from_str() do the same thing as this would do?
| // TODO: we might want to make sure we don't have other cases where this would replace | ||
| // things it shouldn't. We'd have to do some go-back-a-few-lines replace to only do this | ||
| // for to_json() comments. | ||
| inputFile[i] = line.replace(/(\s?\*\s?\@returns\s\{)(any)(\})/, `$1${currentClass}JSON$3`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of the types return any in our library for now but this could be a problem in the future.
| # wasm | ||
| [target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies] | ||
| wasm-bindgen = "=0.2.78" | ||
| wasm-bindgen = { version = "=0.2.78", features = ["serde-serialize"] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To answer your question, no we didn't do anything special.
|
@rooooooooob could you update cardano_serialization_lib.js.flow ? I'm afraid I do smth wrong because I got different result then in the branch. |
|
@lisicky dcSpark will no longer be contributing to this repo because we are instead managing our fork (cardano-multiplatform-lib) and it would be too much work to back-port all our changes back to here. |
|
@lisicky it's a generated file you shouldn't need to manually edit it |
# Conflicts: # rust/pkg/cardano_serialization_lib.js.flow # rust/src/utils.rs
|
|
lisicky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/check
lisicky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/check
# Conflicts: # rust/pkg/cardano_serialization_lib.js.flow # rust/src/crypto.rs # rust/src/lib.rs # rust/src/plutus.rs # rust/src/tx_builder.rs # rust/src/utils.rs
lisicky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/check
# Conflicts: # rust/pkg/cardano_serialization_lib.js.flow # rust/src/lib.rs # rust/src/metadata.rs
lisicky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/check
# Conflicts: # rust/src/plutus.rs
lisicky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/check
lisicky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/check
# Conflicts: # rust/pkg/cardano_serialization_lib.js.flow
This also auto-generates .ts (and thus flow) definitions for all types and inserts them into the
to_js_value(): JsValue.ts/flow defs in place ofany.This is a multi-phase process:
schemarstraits) .json schemas for all types. Thisis done by running the program in rust/json-gen which will export these
to rust/json-gen/schemas
anyreturn typeswith the correct *JSON type generated above and merging these into the main .ts def file