Skip to content

Commit

Permalink
Merge pull request #505 from FirelyTeam/375-fix-integration-test---ca…
Browse files Browse the repository at this point in the history
…nnot-check-equivalence-for-type-keyvaluepair2

375 fix integration test   cannot check equivalence for type keyvaluepair2
  • Loading branch information
baseTwo authored Sep 3, 2024
2 parents fa6f22d + 65c190d commit 6c68c9f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cql/Cql.Comparers/CqlComparers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public CqlComparers()
var cqlComparer = (ICqlComparer)Activator.CreateInstance(genericType, @this)!;
return cqlComparer;
});
ComparerFactories.TryAdd(typeof(KeyValuePair<,>), (type, @this) =>
{
var genericArguments = type.GetGenericArguments();
var genericType = typeof(KeyValuePairComparer<,>).MakeGenericType(genericArguments);
var cqlComparer = (ICqlComparer)Activator.CreateInstance(genericType)!;
return cqlComparer;
});
}


Expand Down
66 changes: 66 additions & 0 deletions Cql/Cql.Comparers/KeyValuePairComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024, NCQA and contributors
* See the file CONTRIBUTORS for details.
*
* This file is licensed under the BSD 3-Clause license
* available at https://raw.githubusercontent.com/FirelyTeam/firely-cql-sdk/main/LICENSE
*/

using System;
using System.Collections.Generic;
using Hl7.Cql.Abstractions;

namespace Hl7.Cql.Comparers;

internal class KeyValuePairComparer<TKey,TValue> : ICqlComparer, ICqlComparer<KeyValuePair<TKey, TValue>>
{
/// <inheritdoc />
public int? Compare(object? x, object? y, string? precision = null)
{
if (x is KeyValuePair<TKey, TValue> tx)
{
if (y is KeyValuePair<TKey, TValue> ty)
return Compare(tx, ty, precision);
}
return -1;
}

/// <inheritdoc />
public int? Compare(
KeyValuePair<TKey, TValue> x,
KeyValuePair<TKey, TValue> y,
string? precision = null) =>
Comparer<TKey>.Default.Compare(x.Key, y.Key) switch
{
0 => Comparer<TValue>.Default.Compare(x.Value, y.Value),
var i => i
};

/// <inheritdoc />
public bool? Equals(object? x, object? y, string? precision = null) =>
Compare(x, y, precision) == 0;

/// <inheritdoc />
public bool? Equals(
KeyValuePair<TKey, TValue> x,
KeyValuePair<TKey, TValue> y,
string? precision = null) =>
Comparer<TKey>.Default.Compare(x.Key, y.Key) == 0
&& Comparer<TValue>.Default.Compare(x.Value, y.Value) == 0;

/// <inheritdoc />
public bool Equivalent(object? x, object? y, string? precision = null) =>
Compare(x, y, precision) == 0;

/// <inheritdoc />
public bool Equivalent(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey, TValue> y, string? precision = null) => Compare(x, y, precision) == 0;

/// <inheritdoc />
public int GetHashCode(KeyValuePair<TKey, TValue> x) => x.GetHashCode();

/// <inheritdoc />
public int GetHashCode(object x) =>
x is KeyValuePair<TKey, TValue> t
? GetHashCode(t)
: throw new InvalidCastException();
}
2 changes: 1 addition & 1 deletion submodules/Firely.Cql.Sdk.Integration.Runner

0 comments on commit 6c68c9f

Please sign in to comment.