Skip to content

Commit

Permalink
Execute choose/when/otherwise tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler committed Dec 5, 2024
1 parent 94efa58 commit 16230dd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
46 changes: 43 additions & 3 deletions esi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,50 @@ fn event_receiver(
}
}
Event::ESI(Tag::When { .. }) => {
println!("THIS SHOULD NEVER APPEAR");
println!("Shouldn't be possible to get a When tag here");
}
Event::ESI(Tag::Choose { .. }) => {
println!("GOT A CHOOSE TAG");
Event::ESI(Tag::Choose {
when_branches,
otherwise_events,
}) => {
let mut chose_branch = false;
for (when, events) in when_branches {
if let Tag::When { test, match_name } = when {
let result = evaluate_expression(test, EvalContext::new(&variables))?;
if result.to_bool() {
chose_branch = true;
for event in events {
event_receiver(
event,
queue,
is_escaped,
original_request_metadata,
dispatch_fragment_request,
variables,
)?;
}
break;
}
} else {
println!(
"Somehow got something other than a When in a Choose: {:?}",
when
);
}
}

if !chose_branch {
for event in otherwise_events {
event_receiver(
event,
queue,
is_escaped,
original_request_metadata,
dispatch_fragment_request,
variables,
)?;
}
}
}

// TODO: change to InterpolatedContent
Expand Down
5 changes: 0 additions & 5 deletions esi/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,10 @@ where
// when/choose
Ok(XmlEvent::Start(ref e)) if e.name() == QName(&tag.choose) => {
*choose_depth += 1;
println!("In a choose!");
}
Ok(XmlEvent::End(ref e)) if e.name() == QName(&tag.choose) => {
*choose_depth -= 1;
choose_tag_handler(when_branches, otherwise_events, callback, task, use_queue)?;
println!("Out of a choose!");
}

Ok(XmlEvent::Start(ref e)) if e.name() == QName(&tag.when) => {
Expand All @@ -271,8 +269,6 @@ where
return unexpected_opening_tag_error(e);
}

println!("In a when!");

let when_tag = parse_when(&e)?;
let mut when_events = Vec::new();
do_parse(
Expand All @@ -291,7 +287,6 @@ where
if *choose_depth == 0 {
return unexpected_closing_tag_error(&e);
}
println!("Out of a when!");

return Ok(());
}
Expand Down
51 changes: 48 additions & 3 deletions examples/esi_vars_example/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h3>Choose Tests</h3>
Simple one branch choose:<br>
HELLO=
<esi:choose>
<esi:when test="$test">
<esi:when test="$(test)">
HELLO
</esi:when>
</esi:choose>
Expand All @@ -42,7 +42,7 @@ <h3>Choose Tests</h3>
Choose with otherwise, positive:<br>
HELLO=
<esi:choose>
<esi:when test="$test">
<esi:when test="$(test)">
HELLO
</esi:when>
<esi:otherwise>
Expand All @@ -54,14 +54,59 @@ <h3>Choose Tests</h3>
Choose with otherwise, negative:<br>
GOODBYE=
<esi:choose>
<esi:when test="$nonexistent">
<esi:when test="$(nonexistent)">
HELLO
</esi:when>
<esi:otherwise>
GOODBYE
</esi:when>
</esi:choose>
<br>

Two branch choose with otherwise, first positive:<br>
HELLO=
<esi:choose>
<esi:when test="$(test)">
HELLO
</esi:when>
<esi:when test="$(nonexistent)">
HELLO2
</esi:when>
<esi:otherwise>
GOODBYE
</esi:when>
</esi:choose>
<br>

Two branch choose with otherwise, second positive:<br>
HELLO2=
<esi:choose>
<esi:when test="$(nonexistent)">
HELLO
</esi:when>
<esi:when test="$(test)">
HELLO2
</esi:when>
<esi:otherwise>
GOODBYE
</esi:when>
</esi:choose>
<br>

Two branch choose with otherwise, negative:<br>
HELLO2=
<esi:choose>
<esi:when test="$(nonexistent)">
HELLO
</esi:when>
<esi:when test="$(alsononexistent)">
HELLO2
</esi:when>
<esi:otherwise>
GOODBYE
</esi:when>
</esi:choose>
<br>

</div>
</body>
Expand Down

0 comments on commit 16230dd

Please sign in to comment.