Skip to content

Commit

Permalink
fix: blueprints gen failing on List
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
rvcas and Quantumplation committed Jun 2, 2023
1 parent 6609ab3 commit 9c29f4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
7 changes: 6 additions & 1 deletion crates/aiken-project/src/blueprint/definitions.rs
Expand Up @@ -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())
Expand All @@ -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);
Expand Down
13 changes: 4 additions & 9 deletions crates/aiken-project/src/blueprint/schema.rs
Expand Up @@ -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()
Expand All @@ -314,6 +308,7 @@ impl Annotated<Schema> {
.to_owned(),
)
}

_ => Data::List(Items::One(Declaration::Referenced(generic))),
};

Expand Down
40 changes: 21 additions & 19 deletions crates/aiken-project/src/blueprint/validator.rs
Expand Up @@ -89,27 +89,29 @@ impl Validator {

let mut definitions = Definitions::new();

let parameters = params
.iter()
.map(|param| {
Annotated::from_type(modules.into(), &param.tipo, &mut definitions)
.map(|schema| Parameter {
title: Some(param.arg_name.get_label()),
schema,
})
.map_err(|error| Error::Schema {
error,
location: param.location,
source_code: NamedSource::new(
module.input_path.display().to_string(),
module.code.clone(),
),
})
})
.collect::<Result<_, _>>()?;

Ok(Validator {
title: format!("{}.{}", &module.name, &func.name),
description: None,
parameters: params
.iter()
.map(|param| {
Annotated::from_type(modules.into(), &param.tipo, &mut definitions)
.map(|schema| Parameter {
title: Some(param.arg_name.get_label()),
schema,
})
.map_err(|error| Error::Schema {
error,
location: param.location,
source_code: NamedSource::new(
module.input_path.display().to_string(),
module.code.clone(),
),
})
})
.collect::<Result<_, _>>()?,
description: func.doc.clone(),
parameters,
datum: datum
.map(|datum| {
Annotated::from_type(modules.into(), &datum.tipo, &mut definitions).map_err(
Expand Down

0 comments on commit 9c29f4f

Please sign in to comment.