Skip to content

Commit

Permalink
fluent overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
majastrz committed Dec 12, 2020
1 parent 215385b commit 9835986
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 44 deletions.
3 changes: 0 additions & 3 deletions src/Bicep.Core/Semantics/FunctionOverload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,5 @@ public FunctionMatchResult Match(IList<TypeSymbol> argumentTypes, out ArgumentCo

return FunctionMatchResult.Match;
}

public static FunctionOverload CreatePartialFixed(string name, TypeSymbol returnType, IEnumerable<TypeSymbol> fixedArgumentTypes, TypeSymbol variableArgumentType) =>
new FunctionOverload(name, returnType, fixedArgumentTypes.Count(), null, fixedArgumentTypes, variableArgumentType);
}
}
16 changes: 14 additions & 2 deletions src/Bicep.Core/Semantics/FunctionOverloadBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,32 @@ public FunctionOverloadBuilder WithDynamicReturnType(FunctionOverload.ReturnType
return this;
}

public FunctionOverloadBuilder WithFixedParameters(params TypeSymbol[] parameterTypes)
public FunctionOverloadBuilder WithFixedParameters(params TypeSymbol[] parameterTypes) =>
this.WithOptionalFixedParameters(parameterTypes.Length, parameterTypes);

public FunctionOverloadBuilder WithOptionalFixedParameters(int minimumArgumentCount, params TypeSymbol[] parameterTypes)
{
this.fixedParameterTypes.Clear();
foreach (TypeSymbol parameterType in parameterTypes)
{
this.fixedParameterTypes.Add(parameterType);
}

this.minimumArgumentCount = parameterTypes.Length;
this.minimumArgumentCount = minimumArgumentCount;
this.maximumArgumentCount = parameterTypes.Length;

return this;
}

public FunctionOverloadBuilder WithFixedParametersAndOptionalVariableParameters(TypeSymbol variableArgumentType, params TypeSymbol[] fixedArgumentTypes)
{
this.WithFixedParameters(fixedArgumentTypes);
this.maximumArgumentCount = null;
this.variableParameterType = variableArgumentType;

return this;
}

public FunctionOverloadBuilder WithVariableParameters(int minimumArgumentCount, TypeSymbol parameterType)
{
this.fixedParameterTypes.Clear();
Expand Down
22 changes: 17 additions & 5 deletions src/Bicep.Core/Semantics/Namespaces/AzNamespaceSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private static NamedObjectType GetDeploymentReturnType(ResourceScopeType targetS
// This list should be kept in-sync with ScopeHelper.CanConvertToArmJson().

var allScopes = ResourceScopeType.TenantScope | ResourceScopeType.ManagementGroupScope | ResourceScopeType.SubscriptionScope | ResourceScopeType.ResourceGroupScope;

yield return (new FunctionOverloadBuilder("tenant").WithDynamicReturnType(GetRestrictedTenantReturnValue).WithFixedParameters().Build(), allScopes);

yield return (new FunctionOverloadBuilder("managementGroup").WithDynamicReturnType(GetRestrictedManagementGroupReturnValue).WithFixedParameters().Build(), ResourceScopeType.ManagementGroupScope);
Expand All @@ -155,6 +156,7 @@ private static IEnumerable<FunctionOverload> GetAzOverloads(ResourceScopeType re
{
yield return functionOverload;
}

// TODO: add banned function to explain why a given function isn't available
}

Expand Down Expand Up @@ -191,14 +193,24 @@ private static IEnumerable<FunctionOverload> GetAzOverloads(ResourceScopeType re
.Build();

// TODO: Not sure about return type
yield return new FunctionOverload("providers", LanguageConstants.Array, 1, 2, Enumerable.Repeat(LanguageConstants.String, 2), null);
yield return new FunctionOverloadBuilder("providers")
.WithReturnType(LanguageConstants.Array)
.WithOptionalFixedParameters(1, LanguageConstants.String, LanguageConstants.String)
.Build();

// TODO: return type is string[]
yield return new FunctionOverload("pickZones", LanguageConstants.Array, 3, 5, new[] {LanguageConstants.String, LanguageConstants.String, LanguageConstants.String, LanguageConstants.Int, LanguageConstants.Int}, null);
yield return new FunctionOverloadBuilder("pickZones")
.WithReturnType(LanguageConstants.Array)
.WithOptionalFixedParameters(3, LanguageConstants.String, LanguageConstants.String, LanguageConstants.String, LanguageConstants.Int, LanguageConstants.Int)
.Build();

yield return new FunctionOverloadBuilder("reference")
.WithReturnType(LanguageConstants.Object)
.WithOptionalFixedParameters(1, LanguageConstants.String, LanguageConstants.String, LanguageConstants.String)
.WithFlags(FunctionFlags.RequiresInlining)
.Build();

// the use of FunctionPlacementConstraints.Resources prevents use of these functions anywhere where they can't be directly inlined into a resource body
yield return new FunctionOverload("reference", LanguageConstants.Object, 1, 3, Enumerable.Repeat(LanguageConstants.String, 3), null, FunctionFlags.RequiresInlining);
yield return new FunctionWildcardOverload("list*", LanguageConstants.Any, 2, 3, new[] { LanguageConstants.String, LanguageConstants.String, LanguageConstants.Object }, null, new Regex("^list[a-zA-Z]*"), FunctionFlags.RequiresInlining);
yield return new FunctionWildcardOverload("list*", LanguageConstants.Any, 2, 3, new[] {LanguageConstants.String, LanguageConstants.String, LanguageConstants.Object}, null, new Regex("^list[a-zA-Z]*"), FunctionFlags.RequiresInlining);
}

public AzNamespaceSymbol(ResourceScopeType resourceScope)
Expand Down
83 changes: 49 additions & 34 deletions src/Bicep.Core/Semantics/Namespaces/SystemNamespaceSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithReturnType(LanguageConstants.Array)
.WithVariableParameters(1, LanguageConstants.Array)
.Build(),

new FunctionOverloadBuilder("concat")
.WithReturnType(LanguageConstants.String)
.WithVariableParameters(1, UnionType.Create(LanguageConstants.String, LanguageConstants.Int, LanguageConstants.Bool))
.Build(),
FunctionOverload.CreatePartialFixed("format", LanguageConstants.String, new[]
{
LanguageConstants.String
}, LanguageConstants.Any),

new FunctionOverloadBuilder("format")
.WithReturnType(LanguageConstants.String)
.WithFixedParametersAndOptionalVariableParameters(LanguageConstants.Any, LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("base64")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),
new FunctionOverload("padLeft", LanguageConstants.String, 2, 3, new[]
{
UnionType.Create(LanguageConstants.String, LanguageConstants.Int), LanguageConstants.Int, LanguageConstants.String
}, null),

new FunctionOverloadBuilder("padLeft")
.WithReturnType(LanguageConstants.String)
.WithOptionalFixedParameters(2, UnionType.Create(LanguageConstants.String, LanguageConstants.Int), LanguageConstants.Int, LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("replace")
.WithReturnType(LanguageConstants.String)
Expand All @@ -54,12 +54,12 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("length")
.WithReturnType(LanguageConstants.Int)
.WithFixedParameters(UnionType.Create(LanguageConstants.String, LanguageConstants.Object, LanguageConstants.Array))
.Build(),

new FunctionOverloadBuilder("split")
.WithReturnType(LanguageConstants.Array)
.WithFixedParameters(LanguageConstants.String, UnionType.Create(LanguageConstants.String, LanguageConstants.Array))
Expand All @@ -86,7 +86,7 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.Build(),

new FunctionOverloadBuilder("trim")
.WithReturnType( LanguageConstants.String)
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),

Expand All @@ -95,7 +95,10 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithFixedParameters(LanguageConstants.String, LanguageConstants.String)
.Build(),

new FunctionOverload("substring", LanguageConstants.String, 2, 3, new[] {LanguageConstants.String, LanguageConstants.Int, LanguageConstants.Int}, null),
new FunctionOverloadBuilder("substring")
.WithReturnType(LanguageConstants.String)
.WithOptionalFixedParameters(2, LanguageConstants.String, LanguageConstants.Int, LanguageConstants.Int)
.Build(),

new FunctionOverloadBuilder("take")
.WithReturnType(LanguageConstants.Array)
Expand All @@ -121,17 +124,17 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithReturnType(LanguageConstants.Bool)
.WithFixedParameters(UnionType.Create(LanguageConstants.Null, LanguageConstants.Object, LanguageConstants.Array, LanguageConstants.String))
.Build(),

new FunctionOverloadBuilder("contains")
.WithReturnType(LanguageConstants.Bool)
.WithFixedParameters(LanguageConstants.Object, LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("contains")
.WithReturnType(LanguageConstants.Bool)
.WithFixedParameters(LanguageConstants.Array, LanguageConstants.Any)
.Build(),

new FunctionOverloadBuilder("contains")
.WithReturnType(LanguageConstants.Bool)
.WithFixedParameters(LanguageConstants.String, LanguageConstants.String)
Expand Down Expand Up @@ -166,12 +169,12 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("last")
.WithReturnType(LanguageConstants.Any)
.WithFixedParameters(LanguageConstants.Array)
.Build(),

new FunctionOverloadBuilder("last")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
Expand All @@ -186,17 +189,17 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithReturnType(LanguageConstants.Int)
.WithFixedParameters(LanguageConstants.String, LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("startsWith")
.WithReturnType(LanguageConstants.Bool)
.WithFixedParameters(LanguageConstants.String, LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("endsWith")
.WithReturnType(LanguageConstants.Bool)
.WithFixedParameters(LanguageConstants.String, LanguageConstants.String)
.Build(),

// TODO: Needs to support number type as well
new FunctionOverloadBuilder("min")
.WithReturnType(LanguageConstants.Int)
Expand All @@ -207,7 +210,7 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithReturnType(LanguageConstants.Int)
.WithFixedParameters(LanguageConstants.Array)
.Build(),

// TODO: Needs to support number type as well
new FunctionOverloadBuilder("max")
.WithReturnType(LanguageConstants.Int)
Expand All @@ -223,37 +226,37 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithReturnType(LanguageConstants.Array)
.WithFixedParameters(LanguageConstants.Int, LanguageConstants.Int)
.Build(),

new FunctionOverloadBuilder("base64ToString")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("base64ToJson")
.WithReturnType(LanguageConstants.Any)
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("uriComponentToString")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("uriComponent")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("dataUriToString")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverloadBuilder("dataUri")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters(LanguageConstants.Any)
.Build(),

new FunctionOverloadBuilder("array")
.WithReturnType(LanguageConstants.Array)
.WithFixedParameters(LanguageConstants.Any)
Expand All @@ -280,11 +283,23 @@ public class SystemNamespaceSymbol : NamespaceSymbol
.WithFixedParameters(LanguageConstants.String)
.Build(),

new FunctionOverload("dateTimeAdd", LanguageConstants.String, 2, 3, Enumerable.Repeat(LanguageConstants.String, 3), null),
new FunctionOverloadBuilder("dateTimeAdd")
.WithReturnType(LanguageConstants.String)
.WithOptionalFixedParameters(2, LanguageConstants.String, LanguageConstants.String, LanguageConstants.String)
.Build(),

// newGuid and utcNow are only allowed in parameter default values
new FunctionOverload("utcNow", LanguageConstants.String, 0, 1, Enumerable.Repeat(LanguageConstants.String, 1), null, FunctionFlags.ParamDefaultsOnly),
new FunctionOverload("newGuid", LanguageConstants.String, 0, 0, Enumerable.Empty<TypeSymbol>(), null, FunctionFlags.ParamDefaultsOnly),
new FunctionOverloadBuilder("utcNow")
.WithReturnType(LanguageConstants.String)
.WithOptionalFixedParameters(0, LanguageConstants.String)
.WithFlags(FunctionFlags.ParamDefaultsOnly)
.Build(),

new FunctionOverloadBuilder("newGuid")
.WithReturnType(LanguageConstants.String)
.WithFixedParameters()
.WithFlags(FunctionFlags.ParamDefaultsOnly)
.Build(),
}.ToImmutableArray();

// TODO: Add copyIndex here when we support loops.
Expand Down

0 comments on commit 9835986

Please sign in to comment.