Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce function count() for arrays & strings #864

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ static ExpressionConstants()
AddFunction<EndsWith>("endsWith", 2, 2);
AddFunction<Format>("format", 1, Byte.MaxValue);
AddFunction<Join>("join", 1, 2);
AddFunction<Count>("count", 1, 1);
AddFunction<StartsWith>("startsWith", 2, 2);
AddFunction<ToJson>("toJson", 1, 1);
AddFunction<FromJson>("fromJson", 1, 1);
Expand Down
34 changes: 34 additions & 0 deletions src/Sdk/DTExpressions2/Expressions2/Sdk/Functions/Count.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace GitHub.DistributedTask.Expressions2.Sdk.Functions
{
internal sealed class Count : Function
{
protected sealed override Boolean TraceFullyRealized => false;

protected sealed override Object EvaluateCore(
EvaluationContext context,
out ResultMemory resultMemory)
{
resultMemory = null;
var items = Parameters[0].Evaluate(context);

// Array
if (items.TryGetCollectionInterface(out var collection) &&
collection is IReadOnlyArray array)
{
return array.Count;
}
// Primitive
else if (items.IsPrimitive)
{
return items.ConvertToString().Length;
}
// Otherwise return zero
else
{
return 0;
}
}
}
}