Skip to content

Commit

Permalink
Allowing MethodCall to be aware of multiple variables of the same typ…
Browse files Browse the repository at this point in the history
…e within a Tuple. Bumps to 3.5.1
  • Loading branch information
jeremydmiller committed Mar 21, 2024
1 parent b7ee2ff commit 4671bd9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<VersionPrefix>3.5.0</VersionPrefix>
<VersionPrefix>3.5.1</VersionPrefix>
<LangVersion>10.0</LangVersion>
<Authors>Jeremy D. Miller;Babu Annamalai;Oskar Dudycz;Joona-Pekka Kokko</Authors>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
Expand Down
19 changes: 19 additions & 0 deletions src/CodegenTests/Codegen/MethodCallTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ public void try_to_assign_variable_within_tuple()
tuple.Inners[1].Inner.ShouldBeSameAs(ball);
tuple.Inners[1].Action.ShouldBe(ReturnAction.Assign);
}

[Fact]
public void tuple_of_same_type()
{
var methodCall = MethodCall.For<MethodCallTarget>(x => x.TupleOfSame());
methodCall.Creates.Count().ShouldBe(2);
methodCall.Creates.ElementAt(0).Usage.ShouldBe("martenOp");
methodCall.Creates.ElementAt(1).Usage.ShouldBe("martenOp2");
}
}

public class Ball
Expand Down Expand Up @@ -434,4 +443,14 @@ public Task<string> GetString()
{
return Task.FromResult("foo");
}

public (IMartenOp, IMartenOp) TupleOfSame()
{
return (default, default);
}
}

public interface IMartenOp
{

}
16 changes: 14 additions & 2 deletions src/JasperFx.CodeGeneration/Frames/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public MethodCall(Type handlerType, MethodInfo method) : base(method.IsAsync())

if (ReturnType.IsValueTuple())
{
var values = ReturnType.GetGenericArguments().Select(x => new Variable(x, this)).ToArray();
var values = buildTupleCreateVariables().ToArray();

ReturnVariable = new ValueTypeReturnVariable(ReturnType, values);
}
Expand Down Expand Up @@ -87,7 +87,19 @@ public MethodCall(Type handlerType, MethodInfo method) : base(method.IsAsync())
}
}
}


private IEnumerable<Variable> buildTupleCreateVariables()
{
foreach (var type in ReturnType.GetGenericArguments())

Check warning on line 93 in src/JasperFx.CodeGeneration/Frames/MethodCall.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 93 in src/JasperFx.CodeGeneration/Frames/MethodCall.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
var count = Creates.Count(x => x.VariableType == type);
var suffix = count > 0 ? (count + 1).ToString() : string.Empty;
var created = new Variable(type, Variable.DefaultArgName(type) + suffix, this);

yield return created;
}
}

/// <summary>
/// Does this MethodCall create a new variable of the object type? This includes the return variable, destructuring
/// tuples, or out parameters
Expand Down

0 comments on commit 4671bd9

Please sign in to comment.