-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2741 from FirelyTeam/feature/try-nullability
Nullability and Try-style function annotations
- Loading branch information
Showing
65 changed files
with
614 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,25 @@ | ||
| ||
/* | ||
/* | ||
* Copyright (c) 2018, Firely ([email protected]) 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-net-sdk/master/LICENSE | ||
*/ | ||
|
||
#nullable enable | ||
|
||
using Hl7.Fhir.Specification; | ||
using Hl7.Fhir.Utility; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using P = Hl7.Fhir.ElementModel.Types; | ||
|
||
namespace Hl7.Fhir.ElementModel | ||
{ | ||
public class ElementNode : DomNode<ElementNode>, ITypedElement, IAnnotated, IAnnotatable, IShortPathGenerator | ||
public class ElementNode : DomNode<ElementNode>, ITypedElement, IAnnotated, IShortPathGenerator | ||
{ | ||
/// <summary> | ||
/// Creates an implementation of ITypedElement that represents a primitive value | ||
|
@@ -43,12 +45,12 @@ public static ITypedElement ForPrimitive(object value) | |
/// <param name="value"></param> | ||
/// <param name="primitiveValue"></param> | ||
/// <returns></returns> | ||
public static bool TryConvertToElementValue(object value, out object primitiveValue) | ||
public static bool TryConvertToElementValue(object? value, [NotNullWhen(true)] out object? primitiveValue) | ||
{ | ||
primitiveValue = conv(); | ||
return primitiveValue is not null; | ||
|
||
object conv() | ||
object? conv() | ||
{ | ||
// NOTE: Keep Any.TryConvertToSystemValue, TypeSpecifier.TryGetNativeType and TypeSpecifier.ForNativeType in sync | ||
switch (value) | ||
|
@@ -94,8 +96,8 @@ public static IEnumerable<ITypedElement> CreateList(params object[] values) => | |
values switch | ||
{ | ||
null => EmptyList, | ||
[var one] => [toTT(one)], | ||
_ => values.Select(toTT).ToList() | ||
[var one] => [toTe(one)!], | ||
_ => values.Select(toTe).ToList()! | ||
}; | ||
|
||
/// <summary> | ||
|
@@ -107,10 +109,10 @@ public static IEnumerable<ITypedElement> CreateList(params object[] values) => | |
public static IEnumerable<ITypedElement> CreateList(IEnumerable<object> values) => values switch | ||
{ | ||
null => EmptyList, | ||
_ => values.Select(toTT).ToList() | ||
_ => values.Select(toTe).ToList()! | ||
}; | ||
|
||
private static ITypedElement toTT(object value) => value switch | ||
private static ITypedElement? toTe(object? value) => value switch | ||
{ | ||
null => null, | ||
ITypedElement element => element, | ||
|
@@ -119,26 +121,26 @@ public static IEnumerable<ITypedElement> CreateList(params object[] values) => | |
|
||
|
||
public static readonly IEnumerable<ITypedElement> EmptyList = []; | ||
public IEnumerable<ITypedElement> Children(string name = null) => ChildrenInternal(name); | ||
public IEnumerable<ITypedElement> Children(string? name = null) => ChildrenInternal(name); | ||
|
||
internal ElementNode(string name, object value, string instanceType, IElementDefinitionSummary definition) | ||
private ElementNode(string name, object? value, string? instanceType, IElementDefinitionSummary? definition) | ||
{ | ||
Name = name ?? throw new ArgumentNullException(nameof(name)); | ||
InstanceType = instanceType; | ||
Value = value; | ||
Definition = definition; | ||
} | ||
|
||
private IReadOnlyCollection<IElementDefinitionSummary> _childDefinitions = null; | ||
private IReadOnlyCollection<IElementDefinitionSummary> _childDefinitions = null!; | ||
|
||
private IReadOnlyCollection<IElementDefinitionSummary> getChildDefinitions(IStructureDefinitionSummaryProvider provider) | ||
{ | ||
LazyInitializer.EnsureInitialized(ref _childDefinitions, () => this.ChildDefinitions(provider)); | ||
|
||
return _childDefinitions!; | ||
return _childDefinitions; | ||
} | ||
|
||
public ElementNode Add(IStructureDefinitionSummaryProvider provider, ElementNode child, string name = null) | ||
public ElementNode Add(IStructureDefinitionSummaryProvider provider, ElementNode child, string? name = null) | ||
{ | ||
if (provider == null) throw new ArgumentNullException(nameof(provider)); | ||
if (child == null) throw new ArgumentNullException(nameof(child)); | ||
|
@@ -147,7 +149,7 @@ public ElementNode Add(IStructureDefinitionSummaryProvider provider, ElementNode | |
return child; | ||
} | ||
|
||
public ElementNode Add(IStructureDefinitionSummaryProvider provider, string name, object value = null, string instanceType = null) | ||
public ElementNode Add(IStructureDefinitionSummaryProvider provider, string name, object? value = null, string? instanceType = null) | ||
{ | ||
if (provider == null) throw new ArgumentNullException(nameof(provider)); | ||
if (name == null) throw new ArgumentNullException(nameof(name)); | ||
|
@@ -182,7 +184,7 @@ public void Replace(IStructureDefinitionSummaryProvider provider, ElementNode ol | |
/// <summary> | ||
/// Will update the child to reflect it being a child of this element, but will not yet add the child at any position within this element | ||
/// </summary> | ||
private void importChild(IStructureDefinitionSummaryProvider provider, ElementNode child, string name, int? position = null) | ||
private void importChild(IStructureDefinitionSummaryProvider provider, ElementNode child, string? name, int? position = null) | ||
{ | ||
child.Name = name ?? child.Name; | ||
if (child.Name == null) throw Error.Argument($"The ElementNode given should have its Name property set or the '{nameof(name)}' parameter should be given."); | ||
|
@@ -195,7 +197,7 @@ private void importChild(IStructureDefinitionSummaryProvider provider, ElementNo | |
// we think it should be - this way you can safely first create a node representing | ||
// an independently created root for a resource of datatype, and then add it to the tree. | ||
var childDefs = getChildDefinitions(provider ?? throw Error.ArgumentNull(nameof(provider))); | ||
var childDef = childDefs.Where(cd => cd.ElementName == child.Name).SingleOrDefault(); | ||
var childDef = childDefs.SingleOrDefault(cd => cd.ElementName == child.Name); | ||
|
||
child.Definition = childDef ?? child.Definition; // if we don't know about the definition, stick with the old one (if any) | ||
|
||
|
@@ -230,7 +232,7 @@ private void importChild(IStructureDefinitionSummaryProvider provider, ElementNo | |
|
||
} | ||
|
||
public static ElementNode Root(IStructureDefinitionSummaryProvider provider, string type, string name = null, object value = null) | ||
public static ElementNode Root(IStructureDefinitionSummaryProvider provider, string type, string? name = null, object? value = null) | ||
{ | ||
if (provider == null) throw Error.ArgumentNull(nameof(provider)); | ||
if (type == null) throw Error.ArgumentNull(nameof(type)); | ||
|
@@ -241,13 +243,13 @@ public static ElementNode Root(IStructureDefinitionSummaryProvider provider, str | |
return new ElementNode(name ?? type, value, type, definition); | ||
} | ||
|
||
public static ElementNode FromElement(ITypedElement node, bool recursive = true, IEnumerable<Type> annotationsToCopy = null) | ||
public static ElementNode FromElement(ITypedElement node, bool recursive = true, IEnumerable<Type>? annotationsToCopy = null) | ||
{ | ||
if (node == null) throw new ArgumentNullException(nameof(node)); | ||
return buildNode(node, recursive, annotationsToCopy, null); | ||
} | ||
|
||
private static ElementNode buildNode(ITypedElement node, bool recursive, IEnumerable<Type> annotationsToCopy, ElementNode parent) | ||
private static ElementNode buildNode(ITypedElement node, bool recursive, IEnumerable<Type>? annotationsToCopy, ElementNode? parent) | ||
{ | ||
var me = new ElementNode(node.Name, node.Value, node.InstanceType, node.Definition) | ||
{ | ||
|
@@ -288,11 +290,11 @@ public ElementNode ShallowCopy() | |
return copy; | ||
} | ||
|
||
public IElementDefinitionSummary Definition { get; private set; } | ||
public IElementDefinitionSummary? Definition { get; private set; } | ||
|
||
public string InstanceType { get; private set; } | ||
public string? InstanceType { get; private set; } | ||
|
||
public object Value { get; set; } | ||
public object? Value { get; set; } | ||
|
||
public IEnumerable<object> Annotations(Type type) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.