Skip to content

Commit

Permalink
Merge pull request #65 from hughesjs/generic-args
Browse files Browse the repository at this point in the history
Added support for generic method args
  • Loading branch information
hughesjs authored Jan 4, 2025
2 parents 094133a + fb95696 commit 8fc1304
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/SuperFluid.Tests/DemoApiDefinition.fluid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Methods:
Type: "int"
- Name: "direction"
Type: "string"
GenericArguments:
# Pointless generic arg for testing
- "T"
CanTransitionTo:
- "Stop"
- "Build"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ public void CanGenerateDemoApi()
result["ICarActor.fluid.g.cs"].ShouldBe(CarActorSource);
}


private const string CanLockOrEnterSource = """
namespace SuperFluid.Tests.Cars;
namespace SuperFluid.Tests.Cars;
public interface ICanLockOrEnter
{
public ICanUnlock Lock();
public ICanStartOrExit Enter();
}
""";
public interface ICanLockOrEnter
{
public ICanUnlock Lock();
public ICanStartOrExit Enter();
}
""";

private const string CanUnlockSource = """
namespace SuperFluid.Tests.Cars;
Expand All @@ -45,7 +46,7 @@ namespace SuperFluid.Tests.Cars;
public interface ICanStartOrExit
{
public ICanStopOrBuild Start(int speed, string direction);
public ICanStopOrBuild Start<T>(int speed, string direction);
public ICanLockOrEnter Exit();
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ internal record FluidApiMethodDefinition
public List<string> CanTransitionTo { get; init; } = new();

public List<FluidApiArgumentDefinition> Arguments { get; init; } = new();

public List<string> GenericArguments { get; init; } = new();
}
5 changes: 4 additions & 1 deletion src/SuperFluid/Internal/Model/FluidApiMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace SuperFluid.Internal.Model;
[DebuggerDisplay("{Name}")]
internal record FluidApiMethod
{
public FluidApiMethod(string name, string? returnType, IEnumerable<FluidApiMethod> transitions, IEnumerable<FluidApiArgument> args)
public FluidApiMethod(string name, string? returnType, IEnumerable<FluidApiMethod> transitions, IEnumerable<FluidApiArgument> args, IEnumerable<string> genericArgs)
{
Name = name;
ReturnType = returnType;
Arguments = [..args];
CanTransitionTo = [..transitions];
GenericArguments = [..genericArgs];
}

internal string Name { get; init; }
Expand All @@ -19,5 +20,7 @@ public FluidApiMethod(string name, string? returnType, IEnumerable<FluidApiMetho
internal HashSet<FluidApiMethod> CanTransitionTo { get; init; } = new();

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

internal HashSet<string> GenericArguments { get; init; } = new();
}

Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private FluidApiMethod FindOrCreateMethod(FluidApiDefinition definition, FluidAp

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

FluidApiMethod newMethod = new(method.Name, method.ReturnType, Array.Empty<FluidApiMethod>(), args);
FluidApiMethod newMethod = new(method.Name, method.ReturnType, Array.Empty<FluidApiMethod>(), args, method.GenericArguments);
stateDict.Add(method, newMethod);

List<FluidApiMethodDefinition> transitionDefinitions = method.CanTransitionTo.Select(m => definition.Methods.Single(d => d.Name == m)).ToList();
Expand Down
10 changes: 7 additions & 3 deletions src/SuperFluid/Internal/Services/FluidGeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ 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
=> $"""
public {kvp.Key.ReturnType ?? kvp.Value.Name} {kvp.Key.Name}({string.Join(", ", kvp.Key.Arguments.Select(a =>$"{a.Type} {a.Name}"))});
""");
=>
{
string genericArgs = kvp.Key.GenericArguments.Count > 0 ? $"<{string.Join(", ", kvp.Key.GenericArguments.Select(a => $"{a}"))}>" : 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}"))});
""";
});

string source = $$"""
namespace {{model.Namespace}};
Expand Down

0 comments on commit 8fc1304

Please sign in to comment.