Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ProxyInterfaceSourceGenerator/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGenerator;

internal static class Constants
{
internal const string GlobalPrefix = "global::";
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static string GetDefaultValue(this IParameterSymbol ps)
{
defaultValue = ps.Type.IsReferenceType
? ParameterValueNull : // The parameter is a ReferenceType, so use "null".
$"default({ps.Type})"; // The parameter is not a ReferenceType, so use "default(T)".
$"default({Constants.GlobalPrefix}{ps.Type})"; // The parameter is not a ReferenceType, so use "default(T)".
}
}
else
Expand Down
46 changes: 44 additions & 2 deletions src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,53 @@ public static IReadOnlyList<string> GetAttributesAsList(this ISymbol symbol)
{
return symbol
.GetAttributes()
.Where(a => a.AttributeClass.IsPublic() && !ExcludedAttributes.Contains(a.AttributeClass?.ToString(), StringComparer.OrdinalIgnoreCase))
.Select(a => $"[{a}]")
.Where(a => a.AttributeClass.IsPublic() && !ExcludedAttributes.Contains(a.AttributeClass!.ToString(), StringComparer.OrdinalIgnoreCase))
.Select(a =>
{
var sb = new StringBuilder();
sb.Append($"[{a.AttributeClass!.ToFullyQualifiedDisplayString()}");

var args = a.ConstructorArguments.Select(FormatAttributeArgument).ToList();
args.AddRange(a.NamedArguments.Select(kvp => $"{kvp.Key} = {FormatAttributeArgument(kvp.Value)}"));

if (args.Count > 0)
{
sb.Append($"({string.Join(", ", args)})");
}

sb.Append("]");
return sb.ToString();
})
.ToArray();
}

private static string FormatAttributeArgument(TypedConstant arg)
{
if (arg.IsNull)
{
return "null";
}

if (arg.Kind == TypedConstantKind.Type)
{
return $"typeof({Constants.GlobalPrefix}{arg.Value})";
}

if (arg.Kind == TypedConstantKind.Enum)
{
var enumType = arg.Type;
if (enumType is null)
{
return arg.Value?.ToString() ?? string.Empty;
}

var member = enumType.GetMembers().OfType<IFieldSymbol>().FirstOrDefault(f => f.ConstantValue is not null && f.ConstantValue.Equals(arg.Value));
return $"{enumType.ToFullyQualifiedDisplayString()}.{member?.Name ?? arg.Value?.ToString()}";
}

return arg.ToCSharpString();
}

public static string GetAttributesPrefix(this ISymbol symbol)
{
var attributes = symbol.GetAttributesAsList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,17 @@ private void TryAddDirect(string typeSymbolAsString, ProxyData existing)
var found = Context.ReplacedTypes.FirstOrDefault(r => r.Direct && r.ClassType == typeSymbolAsString);
if (found == null)
{
var proxy = $"global::{existing.NamespaceDot}{existing.ShortMetadataName}Proxy"; // global::ProxyInterfaceSourceGeneratorTests.Source.TimeProviderProxy
var proxy = $"{Constants.GlobalPrefix}{existing.NamespaceDot}{existing.ShortMetadataName}Proxy"; // global::ProxyInterfaceSourceGeneratorTests.Source.TimeProviderProxy
Context.ReplacedTypes.Add(new(typeSymbolAsString, existing.FullInterfaceName, string.Empty, string.Empty, proxy, true));
}
}

protected bool TryGetNamedTypeSymbolByFullName(TypeKind kind, string name, IEnumerable<string> usings, [NotNullWhen(true)] out ClassSymbol? classSymbol)
{
classSymbol = default;
const string globalPrefix = "global::";
if (name.StartsWith(globalPrefix, StringComparison.Ordinal))
if (name.StartsWith(Constants.GlobalPrefix, StringComparison.Ordinal))
{
name = name.Substring(globalPrefix.Length);
name = name.Substring(Constants.GlobalPrefix.Length);
}

// The GetTypeByMetadataName method returns null if no type matches the full name or if 2 or more types (in different assemblies) match the full name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private string CreateProxyClassCode(

if (firstExtends is not null)
{
extends = $"global::{firstExtends.NamespaceDot}{firstExtends.ShortMetadataName}Proxy, ";
extends = $"{Constants.GlobalPrefix}{firstExtends.NamespaceDot}{firstExtends.ShortMetadataName}Proxy, ";
@base = " : base(instance)";
@new = "new ";
instanceBaseDefinition = $"public {firstExtends.FullQualifiedTypeName} _Instance{firstExtends.FullQualifiedTypeName.GetLastPart()} {{ get; }}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static ProxyInterfaceGeneratorAttributeArguments Parse(AttributeSyntax? a
}
else
{
throw new ArgumentException("The first argument from the ProxyAttribute should be a Type.");
throw new ArgumentException("The first argument from the ProxyAttribute should be a valid resolvable Type.");
}

var array = attributeSyntax.ArgumentList?.Arguments.ToArray() ?? [];
Expand Down Expand Up @@ -129,6 +129,14 @@ private static bool TryParseAsType(
}

var typeInfo = semanticModel.GetTypeInfo(typeSyntax);

if (typeInfo.Type is IErrorTypeSymbol)
{
// The Roslyn compiler could not resolve the type represented by typeSyntax.
// There was an error in the code being analyzed, the type doesn't exist or cannot be resolved in the current context.
return false;
}

var typeSymbol = typeInfo.Type!;

info = new(typeSymbol.ToFullyQualifiedDisplayString(), typeSymbol.GetFullMetadataName(), isGeneric);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace ProxyInterfaceSourceGenerator.SyntaxReceiver;

internal class ProxySyntaxReceiver : ISyntaxContextReceiver
{
private const string GlobalPrefix = "global::";
private static readonly string[] Modifiers = ["public", "partial"];
public IDictionary<InterfaceDeclarationSyntax, ProxyData> CandidateInterfaces { get; } = new Dictionary<InterfaceDeclarationSyntax, ProxyData>();

Expand Down Expand Up @@ -65,7 +64,7 @@ private static bool TryGet(InterfaceDeclarationSyntax interfaceDeclarationSyntax
var fluentBuilderAttributeArguments = AttributeArgumentListParser.Parse(attributeList.Attributes.FirstOrDefault(), semanticModel);

var metadataName = fluentBuilderAttributeArguments.MetadataName;
var globalNamespace = string.IsNullOrEmpty(ns) ? string.Empty : $"{GlobalPrefix}{ns}";
var globalNamespace = string.IsNullOrEmpty(ns) ? string.Empty : $"{Constants.GlobalPrefix}{ns}";
var namespaceDot = string.IsNullOrEmpty(ns) ? string.Empty : $"{ns}.";

data = new ProxyData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,39 @@ public partial class ClientObjectProxy : global::ProxyInterfaceSourceGeneratorTe

public object Tag { get => _Instance.Tag; set => _Instance.Tag = value; }

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public global::Microsoft.SharePoint.Client.ObjectPath Path { get => _Instance.Path; }

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public string ObjectVersion { get => _Instance.ObjectVersion; set => _Instance.ObjectVersion = value; }

[Microsoft.SharePoint.Client.PseudoRemoteAttribute]
[global::Microsoft.SharePoint.Client.PseudoRemoteAttribute]
public bool? ServerObjectIsNull { get => _Instance.ServerObjectIsNull; }

public global::ProxyInterfaceSourceGeneratorTests.Source.PnP.IClientObject TypedObject { get => MapToInterface(_Instance.TypedObject); }

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public virtual void FromJson(global::Microsoft.SharePoint.Client.JsonReader reader)
{
global::Microsoft.SharePoint.Client.JsonReader reader_ = reader;
_Instance.FromJson(reader_);
}

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public virtual bool CustomFromJson(global::Microsoft.SharePoint.Client.JsonReader reader)
{
global::Microsoft.SharePoint.Client.JsonReader reader_ = reader;
var result__636829107 = _Instance.CustomFromJson(reader_);
return result__636829107;
}

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public void Retrieve()
{
_Instance.Retrieve();
}

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public void Retrieve(params string[] propertyNames)
{
string[] propertyNames_ = propertyNames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public partial class ClientRuntimeContextProxy : global::ProxyInterfaceSourceGen

public int RequestTimeout { get => _Instance.RequestTimeout; set => _Instance.RequestTimeout = value; }

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public global::System.Collections.Generic.Dictionary<string, object> StaticObjects { get => _Instance.StaticObjects; }

public global::System.Version ServerSchemaVersion { get => _Instance.ServerSchemaVersion; }
Expand Down Expand Up @@ -125,14 +125,14 @@ public T CastTo<T>(global::ProxyInterfaceSourceGeneratorTests.Source.PnP.IClient
return result_366781530;
}

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public void AddQuery(global::Microsoft.SharePoint.Client.ClientAction query)
{
global::Microsoft.SharePoint.Client.ClientAction query_ = query;
_Instance.AddQuery(query_);
}

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
public void AddQueryIdAndResultObject(long id, object obj)
{
long id_ = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ public partial class SecurableObjectProxy : global::ProxyInterfaceSourceGenerato

public new global::Microsoft.SharePoint.Client.SecurableObject _Instance { get; }
public global::Microsoft.SharePoint.Client.ClientObject _InstanceClientObject { get; }
[Microsoft.SharePoint.Client.RemoteAttribute]
[global::Microsoft.SharePoint.Client.RemoteAttribute]
public global::ProxyInterfaceSourceGeneratorTests.Source.PnP.ISecurableObject FirstUniqueAncestorSecurableObject { get => MapToInterface(_Instance.FirstUniqueAncestorSecurableObject); }

[Microsoft.SharePoint.Client.RemoteAttribute]
[global::Microsoft.SharePoint.Client.RemoteAttribute]
public bool HasUniqueRoleAssignments { get => _Instance.HasUniqueRoleAssignments; }

[Microsoft.SharePoint.Client.RemoteAttribute]
[global::Microsoft.SharePoint.Client.RemoteAttribute]
public global::Microsoft.SharePoint.Client.RoleAssignmentCollection RoleAssignments { get => _Instance.RoleAssignments; }

[Microsoft.SharePoint.Client.RemoteAttribute]
[global::Microsoft.SharePoint.Client.RemoteAttribute]
public virtual void ResetRoleInheritance()
{
_Instance.ResetRoleInheritance();
}

[Microsoft.SharePoint.Client.RemoteAttribute]
[global::Microsoft.SharePoint.Client.RemoteAttribute]
public virtual void BreakRoleInheritance(bool copyRoleAssignments, bool clearSubscopes)
{
bool copyRoleAssignments_ = copyRoleAssignments;
Expand Down
Loading