git.delta.rocks / jrsonnet / refs/commits / 098d931f0d92

difftreelog

fix #[derive(Typed)] should lazily process Thunks

Yaroslav Bolyukin2024-08-15parent: #74ea504.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -957,9 +957,9 @@
 
 	/// 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<()> {
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
477 ident: Ident,477 ident: Ident,
478 ty: Type,478 ty: Type,
479 is_option: bool,479 is_option: bool,
480 is_lazy: bool,
480}481}
481impl TypedField {482impl TypedField {
482 fn parse(field: &syn::Field) -> Result<Self> {483 fn parse(field: &syn::Field) -> Result<Self> {
503 ));504 ));
504 }505 }
506
507 let is_lazy = type_is_path(&ty, "Thunk").is_some();
505508
506 Ok(Self {509 Ok(Self {
507 attr,510 attr,
508 ident,511 ident,
509 ty,512 ty,
510 is_option,513 is_option,
514 is_lazy,
511 })515 })
512 }516 }
513 /// None if this field is flattened in jsonnet output517 /// None if this field is flattened in jsonnet output
596 } else {600 } else {
597 quote! {}601 quote! {}
598 };602 };
599 if self.is_option {603 let value = if self.is_lazy {
600 quote! {604 quote! {
601 if let Some(value) = self.#ident {
602 out.field(#name)605 out.field(#name)
603 #hide606 #hide
604 #add607 #add
605 .try_value(<#ty as Typed>::into_untyped(value)?)?;608 .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
606 }
607 }609 }
608 } else {610 } else {
609 quote! {611 quote! {
610 out.field(#name)612 out.field(#name)
611 #hide613 #hide
612 #add614 #add
613 .try_value(<#ty as Typed>::into_untyped(self.#ident)?)?;615 .try_value(<#ty as Typed>::into_untyped(value)?)?;
614 }616 }
615 }617 };
618 if self.is_option {
619 quote! {
620 if let Some(value) = self.#ident {
621 #value
622 }
623 }
624 } else {
625 quote! {
626 {
627 let value = self.#ident;
628 #value
629 }
630 }
631 }
616 },632 },
617 )633 )
618 }634 }