@@ -575,7 +575,7 @@ impl Value {
575575 // done with a layer, any references that we saw there have been successfully
576576 // resolved, and don't matter for the next layer we're interpolating).
577577 let mut st = state. clone ( ) ;
578- r. merge ( v. interpolate ( root, & mut st) ?) ?;
578+ r. merge ( v. interpolate ( root, & mut st) ?, & mut st ) ?;
579579 }
580580 // Depending on the structure of the ValueList, we may end up with a final
581581 // interpolated Value which contains more ValueLists due to mapping merges. Such
@@ -601,7 +601,7 @@ impl Value {
601601 ///
602602 /// Note that this method will call [`Value::flatten()`] after merging two Mappings to ensure
603603 /// that the resulting Value doesn't contain any `ValueList` elements.
604- fn merge ( & mut self , other : Self ) -> Result < ( ) > {
604+ fn merge ( & mut self , other : Self , state : & mut ResolveState ) -> Result < ( ) > {
605605 if other. is_null ( ) {
606606 // Any value can be replaced by null,
607607 let _prev = std:: mem:: replace ( self , other) ;
@@ -610,7 +610,7 @@ impl Value {
610610
611611 // If `other` is a ValueList, flatten it before trying to merge
612612 let other = if other. is_value_list ( ) {
613- other. flattened ( ) ?
613+ other. flattened ( state ) ?
614614 } else {
615615 other
616616 } ;
@@ -624,21 +624,31 @@ impl Value {
624624 Self :: Mapping ( m) => match other {
625625 // merge mapping and mapping
626626 Self :: Mapping ( other) => m. merge ( & other) ?,
627- _ => return Err ( anyhow ! ( "Can't merge {} over mapping" , other. variant( ) ) ) ,
627+ _ => {
628+ return Err ( state. render_flattening_error ( & format ! (
629+ "Can't merge {} over mapping" ,
630+ other. variant( )
631+ ) ) )
632+ }
628633 } ,
629634 Self :: Sequence ( s) => match other {
630635 // merge sequence and sequence
631636 Self :: Sequence ( mut other) => s. append ( & mut other) ,
632- _ => return Err ( anyhow ! ( "Can't merge {} over sequence" , other. variant( ) ) ) ,
637+ _ => {
638+ return Err ( state. render_flattening_error ( & format ! (
639+ "Can't merge {} over sequence" ,
640+ other. variant( )
641+ ) ) )
642+ }
633643 } ,
634644 Self :: Literal ( _) | Self :: Bool ( _) | Self :: Number ( _) => {
635645 if other. is_mapping ( ) || other. is_sequence ( ) {
636646 // We can't merge simple non-null types over mappings or sequences
637- return Err ( anyhow ! (
647+ return Err ( state . render_flattening_error ( & format ! (
638648 "Can't merge {} over {}" ,
639649 other. variant( ) ,
640650 self . variant( )
641- ) ) ;
651+ ) ) ) ;
642652 }
643653 // overwrite self with the value that's being merged
644654 let _prev = std:: mem:: replace ( self , other) ;
@@ -666,42 +676,42 @@ impl Value {
666676 ///
667677 /// Note that we don't recommend calling `flattened()` on arbitrary Values. Users should always
668678 /// prefer calling [`Value::rendered()`] or one of the in-place variations of that method.
669- pub ( crate ) fn flattened ( & self ) -> Result < Self > {
679+ pub ( crate ) fn flattened ( & self , state : & mut ResolveState ) -> Result < Self > {
670680 match self {
671681 // Flatten ValueList by iterating over its elements and merging each element into a
672682 // base Value.
673683 Self :: ValueList ( l) => {
674684 // NOTE(sg): Empty ValueLists get flattened to Value::Null
675685 let mut base = Value :: Null ;
676686 for v in l {
677- base. merge ( v. clone ( ) ) ?;
687+ base. merge ( v. clone ( ) , state ) ?;
678688 }
679689 Ok ( base)
680690 }
681691 // Flatten Mapping by flattening each value and inserting it into a new Mapping.
682- Self :: Mapping ( m) => Ok ( Self :: Mapping ( m. flattened ( ) ?) ) ,
692+ Self :: Mapping ( m) => Ok ( Self :: Mapping ( m. flattened ( state ) ?) ) ,
683693 // Flatten Sequence by flattening each element and inserting it into a new Sequence
684694 Self :: Sequence ( s) => {
685695 let mut n = Vec :: with_capacity ( s. len ( ) ) ;
686696 for v in s {
687- n. push ( v. flattened ( ) ?) ;
697+ n. push ( v. flattened ( state ) ?) ;
688698 }
689699 Ok ( Self :: Sequence ( n) )
690700 }
691701 // Simple values are flattened as themselves
692702 Self :: Null | Self :: Bool ( _) | Self :: Literal ( _) | Self :: Number ( _) => Ok ( self . clone ( ) ) ,
693703 // Flattening an unparsed string is an error
694- Self :: String ( _) => Err ( anyhow ! (
695- "Can't flatten unparsed String, did you mean to call `rendered()`?"
704+ Self :: String ( _) => Err ( state . render_flattening_error (
705+ "Can't flatten unparsed String, did you mean to call `rendered()`?" ,
696706 ) ) ,
697707 }
698708 }
699709
700710 /// Flattens the Value in-place.
701711 ///
702712 /// See [`Value::flattened()`] for details.
703- pub ( super ) fn flatten ( & mut self ) -> Result < ( ) > {
704- let _prev = std:: mem:: replace ( self , self . flattened ( ) ?) ;
713+ pub ( super ) fn flatten ( & mut self , state : & mut ResolveState ) -> Result < ( ) > {
714+ let _prev = std:: mem:: replace ( self , self . flattened ( state ) ?) ;
705715 Ok ( ( ) )
706716 }
707717
@@ -718,7 +728,7 @@ impl Value {
718728 let mut v = self
719729 . interpolate ( root, & mut state)
720730 . map_err ( |e| anyhow ! ( "While resolving references: {e}" ) ) ?;
721- v. flatten ( ) ?;
731+ v. flatten ( & mut state ) ?;
722732 Ok ( v)
723733 }
724734
0 commit comments