Skip to content

Commit

Permalink
Allow constructing values from any source sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Dec 27, 2023
1 parent 499ae29 commit 86468dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Bonsai.Sgen/Bonsai.Sgen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<VersionPrefix>0.1.0</VersionPrefix>
<VersionPrefix>0.2.0</VersionPrefix>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
30 changes: 30 additions & 0 deletions Bonsai.Sgen/CSharpClassTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,37 @@ public string Render()
{{
{propertyAssignments}
}})"))));

var genericTypeParameter = new CodeTypeParameter("TSource");
var genericSourceParameter = new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(IObservable<>))
{
TypeArguments = { new CodeTypeReference(genericTypeParameter) }
}, "source");
var genericProcessMethod = new CodeMemberMethod
{
Name = "Process",
Attributes = MemberAttributes.Public | MemberAttributes.Final,
TypeParameters = { genericTypeParameter },
Parameters = { genericSourceParameter },
ReturnType = new CodeTypeReference(typeof(IObservable<>))
{
TypeArguments = { new CodeTypeReference(Model.ClassName) }
}
};
genericProcessMethod.Statements.Add(new CodeMethodReturnStatement(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression("System.Reactive.Linq.Observable"),
"Select"),
new CodeVariableReferenceExpression(genericSourceParameter.Name),
new CodeSnippetExpression(
@$"_ => new {Model.ClassName}
{{
{propertyAssignments}
}}"))));

type.Members.Add(processMethod);
type.Members.Add(genericProcessMethod);
type.CustomAttributes.Add(new CodeAttributeDeclaration(
new CodeTypeReference("Bonsai.CombinatorAttribute")));
type.CustomAttributes.Add(new CodeAttributeDeclaration(
Expand Down

0 comments on commit 86468dd

Please sign in to comment.