Skip to content

Commit

Permalink
Added support for complex messages in function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Danae Dekker committed Mar 10, 2024
1 parent 66c5ac7 commit 12050ff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Runtime/Messages/MessageComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ public class Function : MessageComponent
public readonly string name;

// The argument of the function
public readonly string argument;
public readonly Message argument;


// Constructor
public Function(string name, string argument = null)
public Function(string name, Message argument = null)
{
this.name = name;
this.argument = argument;
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Messages/MessageFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ string MessageComponent.IVisitor<string, MessageEnvironment>.VisitSelectFormatCo
// Visit a function component
string MessageComponent.IVisitor<string, MessageEnvironment>.VisitFunctionComponent(MessageComponent.Function component, MessageEnvironment env)
{
if (_functionExecutor.TryExecuteFunction(component.name, component.argument, out var value))
var argument = component.argument != null ? Format(component.argument, env) : null;

if (_functionExecutor.TryExecuteFunction(component.name, argument, out var value))
return Format(new Message(value), env);
else
throw new MessageException($"Function \"{component.name}\" could not be found");
Expand Down
6 changes: 5 additions & 1 deletion Runtime/Messages/MessageParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ContextType
{
Plural,
Select,
Function,
}


Expand Down Expand Up @@ -188,7 +189,10 @@ private static MessageComponent ParseFunctionComponent(Scanner scanner, Stack<Co
return new MessageComponent.Function(name);
scanner.SkipWhile(Scanner.IsWhitespace);

var argument = ParseString(scanner, contexts);
contexts.Push(ContextType.Function);
var argument = new Message(ParseComponents(scanner, contexts));
contexts.Pop();

return new MessageComponent.Function(name, argument);
}

Expand Down

0 comments on commit 12050ff

Please sign in to comment.