Skip to content

Commit

Permalink
Merge pull request #3064 from FirelyTeam/2937-combine-canonicals
Browse files Browse the repository at this point in the history
2937 Copied evertything over from the validator that was not already there.
  • Loading branch information
andrzejskowronski authored Feb 25, 2025
2 parents 980346d + 029115f commit 930fb98
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 52 deletions.
14 changes: 14 additions & 0 deletions src/Hl7.Fhir.Base/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,13 @@
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Canonical.CanonicalUriForFhirCoreType(System.String)</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Date.op_Equality(Hl7.Fhir.Model.Date,Hl7.Fhir.Model.Date)</Target>
Expand Down Expand Up @@ -2108,6 +2115,13 @@
<Right>lib/netstandard2.1/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Canonical.CanonicalUriForFhirCoreType(System.String)</Target>
<Left>lib/netstandard2.1/Hl7.Fhir.Base.dll</Left>
<Right>lib/netstandard2.1/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Date.op_Equality(Hl7.Fhir.Model.Date,Hl7.Fhir.Model.Date)</Target>
Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Base/Introspection/ModelInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private void extractBackbonesFromClasses(IEnumerable<Type> classTypes)

#region IModelInfo

public Canonical? CanonicalUriForFhirCoreType(string typeName) => Canonical.CanonicalUriForFhirCoreType(typeName);
public Canonical? CanonicalUriForFhirCoreType(string typeName) => Canonical.ForCoreType(typeName);

public Canonical? CanonicalUriForFhirCoreType(Type type) => GetFhirTypeNameForType(type) is { } name ? CanonicalUriForFhirCoreType(name) : null;

Expand Down
93 changes: 53 additions & 40 deletions src/Hl7.Fhir.Base/Model/Canonical.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

using P=Hl7.Fhir.ElementModel.Types;
using P = Hl7.Fhir.ElementModel.Types;
using Hl7.Fhir.Utility;
using Hl7.Fhir.Rest;
using System;

#nullable enable
Expand All @@ -37,6 +38,11 @@ namespace Hl7.Fhir.Model;

public partial class Canonical
{
/// <summary>
/// The base uri for FHIR core profiles, which is "http://hl7.org/fhir/StructureDefinition/".
/// </summary>
public static readonly Uri FHIR_CORE_PROFILE_BASE_URI = new(ResourceIdentity.CORE_BASE_URL);

/// <summary>
/// Constructs a Canonical based on a given <see cref="Uri"/>.
/// </summary>
Expand All @@ -46,20 +52,19 @@ public Canonical(Uri uri) : this(uri.OriginalString)
// nothing
}

/// <summary>
/// Constructs a canonical from its components.
/// </summary>
public Canonical(string? uri, string? version, string? fragment = null)
{
if ((uri is not null) && uri.IndexOfAny(['|', '#']) != -1)
throw Error.Argument(nameof(uri), "cannot contain version/fragment data");

if ((version is not null) && version.IndexOfAny(['|', '#']) != -1)
throw Error.Argument(nameof(version), "cannot contain version/fragment data");
/// <summary>
/// Constructs a canonical from its components.
/// </summary>
public Canonical(string? uri, string? version, string? fragment = null)
{
if ((uri is not null) && uri.IndexOfAny(['|', '#']) != -1)
throw Error.Argument(nameof(uri), "cannot contain version/fragment data");

if ((fragment is not null) && fragment.IndexOfAny(['|', '#']) != -1)
throw Error.Argument(nameof(fragment), "already contains version/fragment data");
if ((version is not null) && version.IndexOfAny(['|', '#']) != -1)
throw Error.Argument(nameof(version), "cannot contain version/fragment data");

if ((fragment is not null) && fragment.IndexOfAny(['|', '#']) != -1)
throw Error.Argument(nameof(fragment), "already contains version/fragment data");

Value = uri +
(version is not null ? "|" + version : null) +
Expand All @@ -80,7 +85,7 @@ public void Deconstruct(out string? uri, out string? version, out string? fragme
/// Converts a string to a canonical.
/// </summary>
/// <param name="value"></param>
public static implicit operator Canonical(string value) => new(value);
public static implicit operator Canonical(string? value) => new(value);

/// <summary>
/// Converts a canonical to a string.
Expand All @@ -93,9 +98,17 @@ public void Deconstruct(out string? uri, out string? version, out string? fragme
/// </summary>
public static bool IsValidValue(string value) => FhirUri.IsValidValue(value);

public static readonly Uri FHIR_CORE_PROFILE_BASE_URI = new(@"http://hl7.org/fhir/StructureDefinition/");
public static Canonical CanonicalUriForFhirCoreType(string typename) => new(FHIR_CORE_PROFILE_BASE_URI + typename);

/// <summary>
/// Constructs a Canonical to represent a FHIR core type given by name.
/// </summary>
/// <remarks>If the typename is an absolute url, this function assumes the
/// typename is already fully qualified and will return a canonical with that value.
/// </remarks>
public static Canonical ForCoreType(string typename)
{
var typeNameUri = new Canonical(typename);
return typeNameUri.IsAbsolute ? typeNameUri : ResourceIdentity.Core(typename).OriginalString;
}

/// <summary>
/// The version string of the canonical (if present).
Expand Down Expand Up @@ -145,8 +158,8 @@ private static (string? url, string? version, string? fragment) splitCanonical(s
if (url.EndsWith(separator.ToString())) url = url[..^1];
var position = url.LastIndexOf(separator);

return position == -1 ?
(url, null)
return position == -1
? (url, null)
: (url[..position], url[(position + 1)..]);
}
}
Expand All @@ -157,8 +170,8 @@ private static (string? url, string? version, string? fragment) splitCanonical(s
/// <exception cref="InvalidOperationException">The value of this canonical is null,
/// which is not valid for System strings.</exception>
public P.String ToSystemString() => (P.String?)TryConvertToSystemTypeInternal() ??
throw new InvalidOperationException("Value is null.");
throw new InvalidOperationException("Value is null.");

protected internal override P.Any? TryConvertToSystemTypeInternal() =>
Value is not null ? new P.String(Value) : null;
Value is not null ? new P.String(Value) : null;
}
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Conformance/Model/ElementDefinitionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ public static IReadOnlyList<string> DistinctTypeCodes(this List<ElementDefinitio
/// or otherwise the core profile url for the specified type code.
/// </summary>
public static string? GetTypeProfile(this ElementDefinition.TypeRefComponent elemType) =>
elemType?.Profile.SafeSingleOrDefault() ?? (elemType?.Code is not null ? Canonical.CanonicalUriForFhirCoreType(elemType.Code).Value : null);
elemType?.Profile.SafeSingleOrDefault() ?? (elemType?.Code is not null ? Canonical.ForCoreType(elemType.Code).Value : null);

/// <summary>
/// Returns the profiles on the given <see cref="ElementDefinition.TypeRefComponent"/> if specified,
/// or otherwise the core profile url for the specified type code.
/// </summary>
public static IEnumerable<string>? GetTypeProfiles(this ElementDefinition.TypeRefComponent elemType) =>
elemType?.Profile.Any() == true ? elemType.Profile : (elemType?.Code is not null ? new[] { Canonical.CanonicalUriForFhirCoreType(elemType.Code).Value } : null);
elemType?.Profile.Any() == true ? elemType.Profile : (elemType?.Code is not null ? new[] { Canonical.ForCoreType(elemType.Code).Value } : null);

/// <summary>
/// Determines if the specified element definition represents a <see cref="ResourceReference"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ private void fixInvalidSliceNameOnRootElement(ElementDefinition elem, StructureD
Debug.Assert(elem.IsRootElement());
if (!string.IsNullOrEmpty(elem.SliceName))
{
if (sd.Url != Canonical.CanonicalUriForFhirCoreType(FhirTypeNames.SIMPLEQUANTITY_NAME))
if (sd.Url != Canonical.ForCoreType(FhirTypeNames.SIMPLEQUANTITY_NAME))
{
addIssueInvalidSliceNameOnRootElement(elem, sd);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static async Tasks.Task<StructureDefinition> FindStructureDefinitionAsync
[Obsolete("Using synchronous resolvers is not recommended anymore, use FindStructureDefinitionForCoreTypeAsync() instead.")]
public static StructureDefinition FindStructureDefinitionForCoreType(this IResourceResolver resolver, string typename)
{
var url = Uri.IsWellFormedUriString(typename, UriKind.Absolute) ? typename : Canonical.CanonicalUriForFhirCoreType(typename).Value;
var url = Uri.IsWellFormedUriString(typename, UriKind.Absolute) ? typename : Canonical.ForCoreType(typename).Value;
return resolver.FindStructureDefinition(url);
}

Expand All @@ -75,7 +75,7 @@ public static StructureDefinition FindStructureDefinitionForCoreType(this IResou
public static async Tasks.Task<StructureDefinition> FindStructureDefinitionForCoreTypeAsync(this IAsyncResourceResolver resolver, string typename)
{
var url = Uri.IsWellFormedUriString(typename, UriKind.Absolute) ? typename :
Canonical.CanonicalUriForFhirCoreType(typename).Value;
Canonical.ForCoreType(typename).Value;
return await resolver.FindStructureDefinitionAsync(url).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,18 @@ public static class StructureDefinitionSummaryProperties
public static readonly string BaseDefinitionKey = "StructureDefinition.baseDefinition";
public static readonly string DerivationKey = "StructureDefinition.derivation";

public static readonly string FmmExtensionUrl = Canonical.CanonicalUriForFhirCoreType("structuredefinition-fmm").Value;
public static readonly string FmmExtensionUrl = Canonical.ForCoreType("structuredefinition-fmm").Value;
public static readonly string MaturityLevelKey = "StructureDefinition.maturityLevel";

public static readonly string WgExtensionUrl = Canonical.CanonicalUriForFhirCoreType("structuredefinition-wg").Value;
public static readonly string WgExtensionUrl = Canonical.ForCoreType("structuredefinition-wg").Value;
public static readonly string WorkingGroupKey = "StructureDefinition.workingGroup";

// [WMR 20181213] R4 - NEW
public static readonly string StandardsStatusExtensionUrl = Canonical.CanonicalUriForFhirCoreType("structuredefinition-standards-status").Value;
public static readonly string StandardsStatusExtensionUrl = Canonical.ForCoreType("structuredefinition-standards-status").Value;
public static readonly string StandardsStatusKey = "StructureDefinition.standardsStatus";

// [WMR 20181213] R4 - NEW
public static readonly string NormativeVersionExtensionUrl = Canonical.CanonicalUriForFhirCoreType("structuredefinition-normative-version").Value;
public static readonly string NormativeVersionExtensionUrl = Canonical.ForCoreType("structuredefinition-normative-version").Value;
public static readonly string NormativeVersionKey = "StructureDefinition.normativeVersion";

public static readonly string RootDefinitionKey = "StructureDefinition.rootDefinition";
Expand Down
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Shims.Base/Model/StructureDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ public partial class DifferentialComponent : IElementList;
public bool HasSnapshot => Snapshot is not null && Snapshot.Element.Any();

[NotMapped]
public bool IsCoreDefinition => Type == Id && Url == Canonical.CanonicalUriForFhirCoreType(Type);
public bool IsCoreDefinition => Type == Id && Url == Canonical.ForCoreType(Type);
}
2 changes: 1 addition & 1 deletion src/Hl7.Fhir.Shims.STU3AndUp/Model/ModelInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static bool IsInstanceTypeFor(FHIRAllTypes superclass, FHIRAllTypes subcl
}

/// <inheritdoc cref="IModelInfo.CanonicalUriForFhirCoreType(string)"/>
public static Canonical CanonicalUriForFhirCoreType(string typename) => Canonical.CanonicalUriForFhirCoreType(typename);
public static Canonical CanonicalUriForFhirCoreType(string typename) => Canonical.ForCoreType(typename);

/// <inheritdoc cref="IModelInfo.CanonicalUriForFhirCoreType(Type)"/>
public static Canonical? CanonicalUriForFhirCoreType(Type type) => ModelInspector.CanonicalUriForFhirCoreType(type);
Expand Down

0 comments on commit 930fb98

Please sign in to comment.