-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from bonsai-rx/type-matching
Generate type matching operator for derived types
- Loading branch information
Showing
3 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using NJsonSchema.CodeGeneration; | ||
|
||
namespace Bonsai.Sgen | ||
{ | ||
internal class CSharpClassCodeArtifact : CodeArtifact | ||
{ | ||
public CSharpClassCodeArtifact(CSharpClassTemplateModel model, ITemplate template) : base( | ||
model.ClassName, | ||
model.BaseClassName, | ||
CodeArtifactType.Class, | ||
CodeArtifactLanguage.CSharp, | ||
CodeArtifactCategory.Contract, | ||
template) | ||
{ | ||
Model = model; | ||
} | ||
|
||
public CSharpClassTemplateModel Model { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.CodeDom; | ||
using System.CodeDom.Compiler; | ||
using System.ComponentModel; | ||
using System.Xml.Serialization; | ||
|
||
namespace Bonsai.Sgen | ||
{ | ||
internal class CSharpTypeMatchTemplate : CSharpCodeDomTemplate | ||
{ | ||
public CSharpTypeMatchTemplate( | ||
CSharpClassCodeArtifact modelType, | ||
CodeDomProvider provider, | ||
CodeGeneratorOptions options, | ||
CSharpCodeDomGeneratorSettings settings) | ||
: base(provider, options, settings) | ||
{ | ||
ModelType = modelType; | ||
} | ||
|
||
public CSharpClassCodeArtifact ModelType { get; } | ||
|
||
public override string TypeName => $"Match{ModelType.TypeName}"; | ||
|
||
public override void BuildType(CodeTypeDeclaration type) | ||
{ | ||
type.BaseTypes.Add(new CodeTypeReference("Bonsai.Expressions.SingleArgumentExpressionBuilder")); | ||
type.CustomAttributes.Add(new CodeAttributeDeclaration( | ||
new CodeTypeReference(typeof(DefaultPropertyAttribute)), | ||
new CodeAttributeArgument(new CodePrimitiveExpression("Type")))); | ||
type.CustomAttributes.Add(new CodeAttributeDeclaration( | ||
new CodeTypeReference("Bonsai.WorkflowElementCategoryAttribute"), | ||
new CodeAttributeArgument(new CodeFieldReferenceExpression( | ||
new CodeTypeReferenceExpression("Bonsai.ElementCategory"), | ||
"Combinator")))); | ||
foreach (var modelType in ModelType.Model.DerivedClasses) | ||
{ | ||
type.CustomAttributes.Add(new CodeAttributeDeclaration( | ||
new CodeTypeReference(typeof(XmlIncludeAttribute)), | ||
new CodeAttributeArgument(new CodeTypeOfExpression( | ||
new CodeTypeReference( | ||
"Bonsai.Expressions.TypeMapping", | ||
new CodeTypeReference(modelType.ClassName)))))); | ||
} | ||
|
||
type.Members.Add(new CodeSnippetTypeMember( | ||
@$" public Bonsai.Expressions.TypeMapping Type {{ get; set; }} | ||
public override System.Linq.Expressions.Expression Build(System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> arguments) | ||
{{ | ||
var typeMapping = Type; | ||
var returnType = typeMapping != null ? typeMapping.GetType().GetGenericArguments()[0] : typeof({ModelType.TypeName}); | ||
return System.Linq.Expressions.Expression.Call( | ||
typeof({TypeName}), | ||
""Process"", | ||
new System.Type[] {{ returnType }}, | ||
System.Linq.Enumerable.Single(arguments)); | ||
}} | ||
")); | ||
var sourceTypeReference = new CodeTypeReference(ModelType.TypeName); | ||
var genericTypeParameter = new CodeTypeParameter("TResult") { Constraints = { sourceTypeReference } }; | ||
var sourceParameter = new CodeParameterDeclarationExpression( | ||
new CodeTypeReference(typeof(IObservable<>)) { TypeArguments = { sourceTypeReference } }, "source"); | ||
type.Members.Add(new CodeMemberMethod | ||
{ | ||
Name = "Process", | ||
Attributes = MemberAttributes.Private | MemberAttributes.Static, | ||
TypeParameters = { genericTypeParameter }, | ||
Parameters = { sourceParameter }, | ||
ReturnType = new CodeTypeReference(typeof(IObservable<>)) | ||
{ | ||
TypeArguments = { new CodeTypeReference(genericTypeParameter) } | ||
}, | ||
Statements = | ||
{ | ||
new CodeExpressionStatement(new CodeSnippetExpression( | ||
@$"return System.Reactive.Linq.Observable.Create<{genericTypeParameter.Name}>(observer => | ||
{{ | ||
var sourceObserver = System.Reactive.Observer.Create<{ModelType.TypeName}>( | ||
value => | ||
{{ | ||
var match = value as {genericTypeParameter.Name}; | ||
if (match != null) observer.OnNext(match); | ||
}}, | ||
observer.OnError, | ||
observer.OnCompleted); | ||
return System.ObservableExtensions.SubscribeSafe(source, sourceObserver); | ||
}})")) | ||
} | ||
}); | ||
} | ||
} | ||
} |