Skip to content

Commit

Permalink
Implement IEquatable on static model elements
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed Jan 9, 2023
1 parent 6665934 commit bb9243d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,15 @@ private static bool TryGetRouteHandlerPattern(IInvocationOperation invocation, o

private static bool TryGetRouteHandlerMethod(IInvocationOperation invocation, out IMethodSymbol method)
{
IArgumentOperation? argumentOperation = null;
method = null;
foreach (var argument in invocation.Arguments)
{
if (argument.Parameter?.Ordinal == _routeHandlerArgumentOrdinal)
{
argumentOperation = argument;
method = ResolveMethodFromOperation(argument);
return true;
}
}

if (argumentOperation is not null)
{
method = ResolveMethodFromOperation(argumentOperation);
return true;
}

method = null;
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

namespace Microsoft.AspNetCore.Http.Generators.StaticRouteHandlerModel;
Expand All @@ -15,38 +16,92 @@ internal enum RequestParameterSource
BodyOrService
}

internal sealed class RequestParameter
internal sealed class RequestParameter : IEquatable<RequestParameter>
{
public string Name { get; }
public string Type { get; }
public RequestParameterSource Source { get; set; }
public bool IsOptional { get; set; }
public object? DefaultValue { get; set; }

public override bool Equals(object? obj)
=> obj is RequestParameter requestParameter && Equals(requestParameter);

public bool Equals(RequestParameter other)
=> Name.Equals(other.Name, StringComparison.Ordinal) &&
Type.Equals(other.Type, StringComparison.Ordinal) &&
Source == other.Source &&
IsOptional == other.IsOptional &&
DefaultValue.Equals(DefaultValue);

public override int GetHashCode()
=> (Name, Type, Source, IsOptional, DefaultValue).GetHashCode();
}

internal sealed class EndpointRoute
internal sealed class EndpointRoute : IEquatable<EndpointRoute>
{
public string RoutePattern { get; set; }

public List<string> RouteParameters { get; set; }

public override bool Equals(object? obj)
=> obj is EndpointRoute route && Equals(route);

public bool Equals(EndpointRoute other)
=> RoutePattern.Equals(other.RoutePattern, StringComparison.Ordinal) &&
RouteParameters.Equals(other.RouteParameters);

public override int GetHashCode()
=> (RoutePattern, RouteParameters).GetHashCode();
}

internal sealed class EndpointResponse
internal sealed class EndpointResponse : IEquatable<EndpointResponse>
{
public string ResponseType { get; set; }
public string ContentType { get; set; }
public override bool Equals(object? obj)
=> obj is EndpointResponse endpointResponse && Equals(endpointResponse);

public bool Equals(EndpointResponse other)
=> ResponseType == other.ResponseType
&& ContentType == other.ContentType;

public override int GetHashCode()
=> (ResponseType, ContentType).GetHashCode();
}

internal sealed class EndpointRequest
internal sealed class EndpointRequest : IEquatable<EndpointRequest>
{
public List<RequestParameter> RequestParameters { get; set; }

public override bool Equals(object? obj)
=> obj is EndpointRequest endpointRequest && Equals(endpointRequest);

public bool Equals(EndpointRequest other)
=> RequestParameters == other.RequestParameters;

This comment has been minimized.

Copy link
@Youssef1313

Youssef1313 Jan 9, 2023

Member

This will compare unequal. I guess we need to compare the elements, and calculate GetHashCode per the elements.


public override int GetHashCode()
=> RequestParameters.GetHashCode();
}

internal sealed class Endpoint
internal sealed class Endpoint : IEquatable<Endpoint>
{
public string HttpMethod { get; set; }
public EndpointRoute Route { get; set; }
public EndpointRequest Request { get; set; }
public EndpointResponse Response { get; set; }
public (string, int) Location { get; set; }

public override bool Equals(object? obj)
=> obj is Endpoint endpoint && Equals(endpoint);

public bool Equals(Endpoint other)
=> HttpMethod == other.HttpMethod &&
Route.Equals(other.Route) &&
Request.Equals(other.Request) &&
Response.Equals(other.Response) &&
Location.Equals(other.Location);

public override int GetHashCode()
=> (HttpMethod, Route, Request, Response, Location).GetHashCode();
}

1 comment on commit bb9243d

@davidfowl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orrr use records?

Please sign in to comment.