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

ConvertBytes and ConvertInterface use types like enums/constants #4

Open
leighmcculloch opened this issue Feb 17, 2025 · 1 comment · May be fixed by #9
Open

ConvertBytes and ConvertInterface use types like enums/constants #4

leighmcculloch opened this issue Feb 17, 2025 · 1 comment · May be fixed by #9
Assignees

Comments

@leighmcculloch
Copy link
Member

leighmcculloch commented Feb 17, 2025

I think the interface created by ConvertBytes and ConvertInterface is a little odd.

Use looks like this in the stellar-rpc:

json, _ := xdr2json.ConvertBytes(xdr.TransactionResult{}, tx.Result)

Ref: https://github.com/stellar/stellar-rpc/blob/cd2ef199c70b19a8630a2c3059fd6dc4382223b6/cmd/stellar-rpc/internal/methods/json.go

json, _ := xdr2json.ConvertInterface(topic)

Ref: https://github.com/stellar/stellar-rpc/blob/cd2ef199c70b19a8630a2c3059fd6dc4382223b6/cmd/stellar-rpc/internal/methods/get_events.go

It encourages using the types defined outside the package as a name enum of sorts. Those types happen to be named the same, which is lucky given that if the Go types were named according to Go naming conventions they wouldn't match the Rust names encoded in the stellar-xdr Rust crate.

Instead of the approach taken today, this package could provide a list of constants for all the types, and that would provide the same amount of certainty if folks were wanting to avoid having hardcoded strings.

The above example would become:

json, _ := xdr2json.ConvertBytes(xdr2json.TRANSACTION_RESULT, tx.Result)

This issue was inspired by reviewing the code at:

@leighmcculloch
Copy link
Member Author

leighmcculloch commented Feb 17, 2025

A list of all names from the stellar-xdr crate can be generated using the crate itself, it exports a type TypeVariant which is a list of all the types supported:


Alternatively, use the stellar-xdr CLI. The stellar-xdr CLI is built into the stellar-xdr crate, and has a sub-command that will output a list of all the types.

Install the stellar-xdr CLI that matches the version depended on by the Rust library in this Go package.

For example:

cargo install [email protected] --features cli

Or:

cargo install stellar-xdr@"$(cargo metadata --format-version 1 | jq -r '.packages.[] | select(.name == "stellar-xdr") | .version')" --features cli

Then get the list of types with:

stellar-xdr types list

Note that the stellar xdr command in the stellar-cli, is just a reexport of the stellar-xdr CLI, they're the same thing. But in this case it is better to use the stellar-xdr CLI directly because it's guaranteed to be available for any version of the stellar-xdr crate.

@leighmcculloch leighmcculloch self-assigned this Feb 19, 2025
@leighmcculloch leighmcculloch linked a pull request Feb 19, 2025 that will close this issue
1 task
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 a pull request may close this issue.

1 participant