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

Code Generator for Go Types #1805

Merged
merged 17 commits into from
Aug 24, 2023
Merged

Code Generator for Go Types #1805

merged 17 commits into from
Aug 24, 2023

Conversation

chipshort
Copy link
Collaborator

This adds a code generator for the Go types we need in wasmvm. It uses cosmwasm-schema to generate json schema from a type and traverses that to generate Go code from it.
I'm open to refactoring recommendations. Some of it is a bit messy (though that's mostly because of the schemars api and the many cases that have to be covered)

Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Very very nice!

We need naming convension conversion for acronyms. E.g. pub struct Ibc3ChannelOpenResponse vs. type IBC3ChannelOpenResponse struct or field names pub port_id: String vs. PortID string. Maybe a map which converts words to uppercase (Ibc, Id, ..)

packages/go-gen/src/main.rs Show resolved Hide resolved
packages/go-gen/Cargo.toml Show resolved Hide resolved
packages/go-gen/src/go.rs Show resolved Hide resolved
packages/go-gen/src/go.rs Show resolved Hide resolved
match ty {
"Uint128" => Some("string"),
"Binary" => Some("[]byte"),
"HexBinary" => Some("Checksum"),
Copy link
Member

@webmaster128 webmaster128 Aug 21, 2023

Choose a reason for hiding this comment

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

This should be string by default and only Checksum in cases where we have a 32 byte checksum.

Do we have a way to annotate this somehow in Rust?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's a bit of a problem. What we could do is use #[schemars(title = "Checksum")] on the field. That would set metadata.title for the field in the schema.

Copy link
Member

Choose a reason for hiding this comment

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

Oh but what we also do is change CodeInfoResponse::checksum from HexBinary to cosmwasm_std::Checksum. With that we can map

// both hex strings in JSON
"HexBinary" => Some("string"),
"Checksum" => Some("Checksum"),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately, that only works if we make Checksum a newtype like struct Checksum(HexBinary), which would be a contract-breaking change for CodeInfoResponse.
Type aliases are already resolved to their underlying types at the json schema level (type Checksum = HexBinary becomes HexBinary in the schema).

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I'm thinking about a source breaking change here. In contrast to HexBinary, Checksum would guarantee to store 32 bytes. We have Checksum already and would just have to add serde.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, I see now, we have Checksum in cosmwasm-vm. Yeah, that seems like a good solution and would even make the type a bit nicer (as you mentioned, guaranteed 32 bytes, but also the docs specify that it is sha256, etc.).
But that means this would have to wait for 2.0, right?

Copy link
Member

Choose a reason for hiding this comment

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

we have Checksum in cosmwasm-vm.

Ah, right. I did not realize we don't have it in cosmwasm-std yet. But yes, this can be the same type by moving it to cosmwasm-std and adding serde support.

wait for 2.0, right?

Yes. In the meantime we can keep "HexBinary" => Some("Checksum"), because this is only used in one place.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alright, I created an issue for that: #1835
Anything left to do here, or can we merge?

Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

🔥🔥🔥 Now we're talking

packages/go-gen/src/utils.rs Show resolved Hide resolved
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Very nice! Let's merge here and see if that is useful for #1788

@webmaster128 webmaster128 merged commit 05ece0b into main Aug 24, 2023
3 checks passed
@webmaster128 webmaster128 deleted the go-codegen branch August 24, 2023 13:11
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