fix: blueprints gen failing on List

closes #569

* added new methods to Definitions
  it doesn't use expect
* lookup was failing for the special map/pair case
  when resolving list generics

Co-authored-by: Pi <pi@sundaeswap.finance>
This commit is contained in:
rvcas 2023-06-02 17:50:01 -04:00
parent 6609ab335c
commit 9c29f4f26b
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
3 changed files with 31 additions and 29 deletions

View File

@ -33,7 +33,7 @@ impl<T> Definitions<T> {
self.inner.is_empty()
}
/// Retrieve a definition, if it exists.
/// Retrieve a definition, if it exists; fail if not resolved
pub fn lookup(&self, reference: &Reference) -> Option<&T> {
self.inner
.get(&reference.as_key())
@ -43,6 +43,11 @@ impl<T> Definitions<T> {
)
}
/// Retrieve a definition, if it exists and is resolved.
pub fn try_lookup(&self, reference: &Reference) -> Option<&T> {
self.inner.get(&reference.as_key()).and_then(|v| v.as_ref())
}
/// Merge two set of definitions together. Prioritize callee.
pub fn merge(&mut self, other: &mut Definitions<T>) {
self.inner.append(&mut other.inner);

View File

@ -293,17 +293,11 @@ impl Annotated<Schema> {
// from the PlutusTx / LedgerApi Haskell codebase, which encodes some elements
// as such. We don't have a concept of language maps in Aiken, so we simply
// make all types abide by this convention.
let data = match definitions
.lookup(&generic)
.expect(
"Generic type argument definition was registered just above.",
)
.clone()
{
Annotated {
let data = match definitions.try_lookup(&generic).cloned() {
Some(Annotated {
annotated: Schema::Data(Data::List(Items::Many(xs))),
..
} if xs.len() == 2 => {
}) if xs.len() == 2 => {
definitions.remove(&generic);
Data::Map(
xs.first()
@ -314,6 +308,7 @@ impl Annotated<Schema> {
.to_owned(),
)
}
_ => Data::List(Items::One(Declaration::Referenced(generic))),
};

View File

@ -89,10 +89,7 @@ impl Validator {
let mut definitions = Definitions::new();
Ok(Validator {
title: format!("{}.{}", &module.name, &func.name),
description: None,
parameters: params
let parameters = params
.iter()
.map(|param| {
Annotated::from_type(modules.into(), &param.tipo, &mut definitions)
@ -109,7 +106,12 @@ impl Validator {
),
})
})
.collect::<Result<_, _>>()?,
.collect::<Result<_, _>>()?;
Ok(Validator {
title: format!("{}.{}", &module.name, &func.name),
description: func.doc.clone(),
parameters,
datum: datum
.map(|datum| {
Annotated::from_type(modules.into(), &datum.tipo, &mut definitions).map_err(