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

Add byte slice(&[u8]) support for idl-build #2622

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lang/syn/src/idl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ pub fn idl_type_ts_from_syn_type(
}
}
}
syn::Type::Reference(reference) => match reference.elem.as_ref() {
syn::Type::Slice(slice) if matches!(&*slice.elem, syn::Type::Path(path) if the_only_segment_is(path, "u8")) =>
{
return Ok((quote! {#idl::IdlType::Bytes}, vec![]));
}
_ => panic!("Reference types other than byte slice(`&[u8]`) are not allowed"),
},
_ => Err(()),
}
}
Expand Down Expand Up @@ -846,7 +853,7 @@ pub fn gen_idl_print_function_for_constant(item: &syn::ItemConst) -> TokenStream

let impl_ts = match idl_type_ts_from_syn_type(&item.ty, &vec![]) {
Ok((ty, _)) => quote! {
let value = format!("{}", #expr);
let value = format!("{:?}", #expr);

let idl = #idl::IdlConst {
name: #name.into(),
Expand Down
15 changes: 10 additions & 5 deletions tests/idl/idls/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
],
"constants": [
{
"name": "BAR_CONST",
"type": "u8",
"value": "6"
"name": "BYTES_STR",
"type": "bytes",
"value": "[116, 101, 115, 116]"
},
{
"name": "BYTE_STR",
"type": "u8",
"value": "116"
},
{
"name": "FOO_CONST",
"type": "u128",
"name": "I128",
"type": "i128",
"value": "1000000"
},
{
"name": "U8",
"type": "u8",
"value": "6"
}
],
"instructions": [
Expand Down
18 changes: 9 additions & 9 deletions tests/idl/idls/parse.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
],
"constants": [
{
"name": "FOO_CONST",
"type": "u128",
"value": "1_000_000"
},
{
"name": "BAR_CONST",
"name": "U8",
"type": "u8",
"value": "6"
},
{
"name": "BYTES_STR",
"type": "bytes",
"value": "[116, 101, 115, 116]"
"name": "I128",
"type": "i128",
"value": "1_000_000"
},
{
"name": "BYTE_STR",
"type": "u8",
"value": "116"
},
{
"name": "BYTES_STR",
"type": "bytes",
"value": "[116, 101, 115, 116]"
}
],
"instructions": [
Expand Down
8 changes: 4 additions & 4 deletions tests/idl/programs/idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use std::str::FromStr;
declare_id!("id11111111111111111111111111111111111111111");

#[constant]
pub const FOO_CONST: u128 = 1_000_000;
pub const U8: u8 = 6;

#[constant]
pub const BAR_CONST: u8 = 6;
pub const I128: i128 = 1_000_000;

#[constant]
pub const BYTES_STR: &[u8] = b"test";
pub const BYTE_STR: u8 = b't';

#[constant]
pub const BYTE_STR: u8 = b't';
pub const BYTES_STR: &[u8] = b"test";

pub const NO_IDL: u16 = 55;

Expand Down
33 changes: 19 additions & 14 deletions tests/idl/tests/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,29 @@ describe("IDL", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.idl as Program<Idl>;

it("Should include `FOO_CONST`", () => {
assert.isDefined(
program.idl.constants.find(
(c) =>
c.name === "FOO_CONST" && c.type === "u128" && c.value === "1000000"
)
);
});
it("Includes constants that use `#[constant]` macro", () => {
const checkDefined = (
cb: (constant: typeof program["idl"]["constants"][number]) => boolean
) => {
program.idl.constants.find((c) => cb(c));
};

it("Should include `BAR_CONST`", () => {
assert.isDefined(
program.idl.constants.find(
(c) => c.name === "BAR_CONST" && c.type === "u8" && c.value === "6"
)
checkDefined((c) => c.name === "U8" && c.type === "u8" && c.value === "6");
checkDefined(
(c) => c.name === "I128" && c.type === "i128" && c.value === "1000000"
);
checkDefined(
(c) => c.name === "BYTE_STR" && c.type === "u8" && c.value === "116"
);
checkDefined(
(c) =>
c.name === "BYTES_STR" &&
c.type === "bytes" &&
c.value === "[116, 101, 115, 116]"
);
});

it("Should not include `NO_IDL` const", () => {
it("Does not include constants that does not use `#[constant]` macro ", () => {
// @ts-expect-error
assert.isUndefined(program.idl.constants.find((c) => c.name === "NO_IDL"));
});
Expand Down