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

feat: Added support for optional method args #72

Merged
merged 1 commit into from
Jan 4, 2025
Merged
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
10 changes: 8 additions & 2 deletions src/SuperFluid.Tests/DemoApiDefinition.fluid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ Methods:
- "Enter"
- Name: "Start"
Arguments:
- Name: "speed"
Type: "int"
# These are deliberately out of order to test that the parser sticks the defaults to the end of the argument list
- Name: "direction"
Type: "string"
DefaultValue: "\"Forward\"" # Note that we need the quotes here
- Name: "speed"
Type: "int"
- Name: "hotwire"
Type: "bool"
DefaultValue: "false"

# These constraints are pointless but are here to test the parser
GenericArguments:
- Name: "T"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace SuperFluid.Tests.Cars;

public interface ICanStartOrExit
{
public ICanStopOrBuild Start<T,X>(int speed, string direction) where T : class, INumber where X : notnull;
public ICanStopOrBuild Start<T,X>(int speed, string direction = "Forward", bool hotwire = false) where T : class, INumber where X : notnull;
public ICanLockOrEnter Exit();
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ internal record FluidApiArgumentDefinition
{
public required string Type { get; init; }
public required string Name { get; init; }

public string? DefaultValue { get; init; }
}
5 changes: 4 additions & 1 deletion src/SuperFluid/Internal/Model/FluidApiArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ namespace SuperFluid.Internal.Model;
[DebuggerDisplay("{Type} {Name}")]
internal class FluidApiArgument
{
public FluidApiArgument(string argName, string argType)
public FluidApiArgument(string argName, string argType, string? defaultValue)
{
Name = argName;
Type = argType;
DefaultValue = defaultValue;
}

public string Type { get; init; }
public string Name { get; init; }

public string? DefaultValue { get; init; }
}
34 changes: 19 additions & 15 deletions src/SuperFluid/Internal/Model/FluidApiMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@ namespace SuperFluid.Internal.Model;
[DebuggerDisplay("{Name}")]
internal record FluidApiMethod
{
public FluidApiMethod(string name, string? returnType, IEnumerable<FluidApiMethod> transitions, IEnumerable<FluidApiArgument> args, IEnumerable<FluidGenericArgument> genericArgs)
{
Name = name;
ReturnType = returnType;
Arguments = [..args];
CanTransitionTo = [..transitions];
GenericArguments = [..genericArgs];
}
public FluidApiMethod(string name, string? returnType, IEnumerable<FluidApiMethod> transitions, IEnumerable<FluidApiArgument> args,
IEnumerable<FluidGenericArgument> genericArgs)
{
Name = name;
ReturnType = returnType;
CanTransitionTo = [..transitions];
GenericArguments = [..genericArgs];

internal string Name { get; init; }
// We need to order the arguments so that the ones with defaults are last
FluidApiArgument[] enumeratedArgs = args as FluidApiArgument[] ?? args.ToArray();
var orderedArgs = enumeratedArgs.Where(a => a.DefaultValue is null).Concat(enumeratedArgs.Where(a => a.DefaultValue is not null)).ToList();
Arguments = [..orderedArgs];
}

internal string? ReturnType { get; init; }
internal HashSet<FluidApiMethod> CanTransitionTo { get; init; } = new();
internal string Name { get; init; }

internal HashSet<FluidApiArgument> Arguments { get; init; } = new();

internal HashSet<FluidGenericArgument> GenericArguments { get; init; } = new();
}
internal string? ReturnType { get; init; }
internal HashSet<FluidApiMethod> CanTransitionTo { get; init; } = new();

internal HashSet<FluidApiArgument> Arguments { get; init; } = new();

internal HashSet<FluidGenericArgument> GenericArguments { get; init; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private FluidApiMethod FindOrCreateMethod(FluidApiDefinition definition, FluidAp
return state;
}

List<FluidApiArgument> args = method.Arguments.Select(a => new FluidApiArgument(a.Name, a.Type)).ToList();
List<FluidApiArgument> args = method.Arguments.Select(a => new FluidApiArgument(a.Name, a.Type, a.DefaultValue)).ToList();

List<FluidGenericArgument> genericArgs = method.GenericArguments.Select(a => new FluidGenericArgument(a.Name, a.Constraints)).ToList();

Expand Down
32 changes: 20 additions & 12 deletions src/SuperFluid/Internal/Services/FluidGeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,7 @@ public interface {{model.Name}}: {{string.Join(",", model.States.Select(s => s.N

private string GenerateStateSource(FluidApiState fluidApiState, FluidApiModel model)
{
IEnumerable<string> methodDeclarations = fluidApiState.MethodTransitions.Select(kvp
=>
{
string genericArgs = kvp.Key.GenericArguments.Count > 0 ? $"<{string.Join(",", kvp.Key.GenericArguments.Select(a => $"{a.Name}"))}>" : string.Empty;

string constraints = kvp.Key.GenericArguments.Count > 0 ? $" {string.Join(" ", kvp.Key.GenericArguments.Select(a => $"where {a.Name} : {string.Join(", ", a.Constraints)}"))}" : string.Empty;


return $"""
public {kvp.Key.ReturnType ?? kvp.Value.Name} {kvp.Key.Name}{genericArgs}({string.Join(", ", kvp.Key.Arguments.Select(a => $"{a.Type} {a.Name}"))}){constraints};
""";
});
IEnumerable<string> methodDeclarations = fluidApiState.MethodTransitions.Select((kvp) => GenerateMethodSource(kvp.Key, kvp.Value));

string source = $$"""
namespace {{model.Namespace}};
Expand All @@ -69,5 +58,24 @@ public interface {{fluidApiState.Name}}
return source;
}

private string GenerateMethodSource(FluidApiMethod method, FluidApiState state)
{
string genericArgs = method.GenericArguments.Count > 0 ? $"<{string.Join(",", method.GenericArguments.Select(a => $"{a.Name}"))}>" : string.Empty;

string constraints = method.GenericArguments.Count > 0 ? $" {string.Join(" ", method.GenericArguments.Select(GenerateGenericConstraintSource))}" : string.Empty;

return $"""
public {method.ReturnType ?? state.Name} {method.Name}{genericArgs}({string.Join(", ", method.Arguments.Select(GenerateMethodArgsSource))}){constraints};
""";
}

private string GenerateMethodArgsSource(FluidApiArgument a)
{
return $"{a.Type} {a.Name}{(a.DefaultValue is not null ? " = " + a.DefaultValue : string.Empty)}";
}

private string GenerateGenericConstraintSource(FluidGenericArgument a)
{
return $"where {a.Name} : {string.Join(", ", a.Constraints)}";
}
}
Loading