Skip to content

Commit

Permalink
fix: #[derive(Typed)] should lazily process Thunks
Browse files Browse the repository at this point in the history
Fixes: #172
  • Loading branch information
CertainLach committed Aug 15, 2024
1 parent 74ea504 commit 098d931
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/jrsonnet-evaluator/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,9 +957,9 @@ impl ObjMemberBuilder<ValueBuilder<'_>> {

/// Tries to insert value, returns an error if it was already defined
pub fn try_value(self, value: impl Into<Val>) -> Result<()> {
self.thunk(Thunk::evaluated(value.into()))
self.try_thunk(Thunk::evaluated(value.into()))
}
pub fn thunk(self, value: impl Into<Thunk<Val>>) -> Result<()> {
pub fn try_thunk(self, value: impl Into<Thunk<Val>>) -> Result<()> {
self.binding(MaybeUnbound::Bound(value.into()))
}
pub fn bindable(self, bindable: impl Unbound<Bound = Val>) -> Result<()> {
Expand Down
32 changes: 24 additions & 8 deletions crates/jrsonnet-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ struct TypedField {
ident: Ident,
ty: Type,
is_option: bool,
is_lazy: bool,
}
impl TypedField {
fn parse(field: &syn::Field) -> Result<Self> {
Expand All @@ -503,11 +504,14 @@ impl TypedField {
));
}

let is_lazy = type_is_path(&ty, "Thunk").is_some();

Ok(Self {
attr,
ident,
ty,
is_option,
is_lazy,
})
}
/// None if this field is flattened in jsonnet output
Expand Down Expand Up @@ -596,21 +600,33 @@ impl TypedField {
} else {
quote! {}
};
if self.is_option {
let value = if self.is_lazy {
quote! {
if let Some(value) = self.#ident {
out.field(#name)
#hide
#add
.try_value(<#ty as Typed>::into_untyped(value)?)?;
}
out.field(#name)
#hide
#add
.try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
}
} else {
quote! {
out.field(#name)
#hide
#add
.try_value(<#ty as Typed>::into_untyped(self.#ident)?)?;
.try_value(<#ty as Typed>::into_untyped(value)?)?;
}
};
if self.is_option {
quote! {
if let Some(value) = self.#ident {
#value
}
}
} else {
quote! {
{
let value = self.#ident;
#value
}
}
}
},
Expand Down

0 comments on commit 098d931

Please sign in to comment.