Add support for generics in the blueprint generation.

This commit is contained in:
KtorZ
2023-01-28 18:47:56 +01:00
parent 0bd9d045b0
commit 9a44cba007
2 changed files with 130 additions and 18 deletions

View File

@@ -10,7 +10,10 @@ use serde::{
self,
ser::{Serialize, SerializeStruct, Serializer},
};
use std::fmt::{self, Display};
use std::{
collections::HashMap,
fmt::{self, Display},
};
use uplc::ast::{NamedDeBruijn, Program};
#[derive(Debug, PartialEq, Clone)]
@@ -88,10 +91,11 @@ impl Validator {
purpose,
datum: datum
.map(|datum| {
Annotated::from_type(modules.into(), &datum.tipo).map_err(Error::Schema)
Annotated::from_type(modules.into(), &datum.tipo, &HashMap::new())
.map_err(Error::Schema)
})
.transpose()?,
redeemer: Annotated::from_type(modules.into(), &redeemer.tipo)
redeemer: Annotated::from_type(modules.into(), &redeemer.tipo, &HashMap::new())
.map_err(Error::Schema)?,
program: generator
.generate(&def.body, &def.arguments, true)
@@ -453,4 +457,76 @@ mod test {
}),
)
}
#[test]
fn validator_generics() {
assert_validator(
r#"
type Either<left, right> {
Left(left)
Right(right)
}
type Interval<a> {
Finite(a)
Infinite
}
fn withdraw(redeemer: Either<ByteArray, Interval<Int>>, ctx: Void) {
True
}
"#,
json!(
{
"title": "test_module",
"purpose": "withdraw",
"hash": "da4a98cee05a17be402b07c414d59bf894c9ebd0487186417121de8f",
"redeemer": {
"title": "Either",
"anyOf": [
{
"title": "Left",
"dataType": "constructor",
"index": 0,
"fields": [
{
"dataType": "bytes"
}
]
},
{
"title": "Right",
"dataType": "constructor",
"index": 1,
"fields": [
{
"title": "Interval",
"anyOf": [
{
"title": "Finite",
"dataType": "constructor",
"index": 0,
"fields": [
{
"dataType": "integer"
}
]
},
{
"title": "Infinite",
"dataType": "constructor",
"index": 1,
"fields": []
}
]
}
]
}
]
},
"compiledCode": "581d010000210872656465656d657200210363747800533357349445261601"
}
),
)
}
}