Skip to content

Commit

Permalink
Started working on a new type interface that will make it easier to w…
Browse files Browse the repository at this point in the history
…ork with symbols/types.
  • Loading branch information
MeltyPlayer committed Aug 5, 2023
1 parent ba4393b commit a33189c
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Schema/src/util/types/BSymbolTypeV2.cs
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>();
}
}
}
27 changes: 27 additions & 0 deletions Schema/src/util/types/ITypeV2.cs
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; }
}
}
40 changes: 40 additions & 0 deletions Schema/src/util/types/SymbolTypeV2.cs
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;
}
}
}

0 comments on commit a33189c

Please sign in to comment.