-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started working on a new type interface that will make it easier to w…
…ork with symbols/types.
- Loading branch information
1 parent
ba4393b
commit a33189c
Showing
3 changed files
with
121 additions
and
0 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,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Microsoft.CodeAnalysis; | ||
|
||
using schema.binary; | ||
using schema.util.symbols; | ||
|
||
namespace schema.util.types { | ||
public static partial class TypeV2 { | ||
private abstract class BSymbolTypeV2 : ITypeV2 { | ||
public abstract string Name { get; } | ||
|
||
public abstract string FullyQualifiedNamespace { get; } | ||
public abstract IEnumerable<string> NamespaceParts { get; } | ||
|
||
public abstract bool Implements(Type type); | ||
|
||
public abstract int GenericArgCount { get; } | ||
|
||
// Common | ||
private bool Matches_(string name, | ||
string? fullyQualifiedNamespace, | ||
int genericArgCount) | ||
=> this.Name == name && | ||
this.FullyQualifiedNamespace == fullyQualifiedNamespace && | ||
this.GenericArgCount == genericArgCount; | ||
|
||
public bool IsExactly(ITypeV2 other) => this.Matches_( | ||
other.Name, | ||
other.FullyQualifiedNamespace, | ||
other.GenericArgCount); | ||
|
||
public bool IsExactly(Type other) => this.Matches_( | ||
other.Name, | ||
other.Namespace, | ||
other.GenericTypeArguments.Length); | ||
|
||
public bool IsExactly(ISymbol other) => this.Matches_( | ||
other.Name, | ||
other.GetFullyQualifiedNamespace(), | ||
(other as INamedTypeSymbol)?.TypeParameters.Length ?? 0); | ||
|
||
public bool IsExactly<T>() => this.IsExactly(typeof(T)); | ||
public bool Implements<T>() => this.Implements(typeof(T)); | ||
|
||
public bool IsBinarySerializable | ||
=> this.Implements<IBinarySerializable>(); | ||
|
||
public bool IsBinaryDeserializable | ||
=> this.Implements<IBinaryDeserializable>(); | ||
} | ||
} | ||
} |
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,27 @@ | ||
| ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Microsoft.CodeAnalysis; | ||
|
||
namespace schema.util.types { | ||
public interface ITypeV2 { | ||
string Name { get; } | ||
|
||
string? FullyQualifiedNamespace { get; } | ||
IEnumerable<string> NamespaceParts { get; } | ||
|
||
bool IsExactly(ITypeV2 other); | ||
bool IsExactly<T>(); | ||
bool IsExactly(Type other); | ||
bool IsExactly(ISymbol other); | ||
|
||
bool Implements<T>(); | ||
bool Implements(Type type); | ||
|
||
bool IsBinarySerializable { get; } | ||
bool IsBinaryDeserializable { get; } | ||
|
||
int GenericArgCount { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
|
||
using schema.util.enumerables; | ||
using schema.util.symbols; | ||
|
||
namespace schema.util.types { | ||
public static partial class TypeV2 { | ||
public static ITypeV2 FromSymbol(ISymbol symbol) | ||
=> new SymbolTypeV2(symbol); | ||
|
||
private class SymbolTypeV2 : BSymbolTypeV2 { | ||
private readonly ISymbol symbol_; | ||
|
||
public SymbolTypeV2(ISymbol symbol) { | ||
this.symbol_ = symbol; | ||
} | ||
|
||
public override string Name => this.symbol_.Name; | ||
|
||
public override string FullyQualifiedNamespace | ||
=> this.symbol_.GetFullyQualifiedNamespace(); | ||
|
||
public override IEnumerable<string> NamespaceParts | ||
=> this.symbol_.GetContainingNamespaces(); | ||
|
||
public override bool Implements(Type type) | ||
=> this.symbol_.Yield() | ||
.Concat((this.symbol_ as ITypeSymbol)?.AllInterfaces ?? | ||
Enumerable.Empty<ISymbol>()) | ||
.Any(symbol => symbol.IsExactlyType(type)); | ||
|
||
public override int GenericArgCount | ||
=> (this.symbol_ as INamedTypeSymbol)?.TypeParameters.Length ?? 0; | ||
} | ||
} | ||
} |