Skip to content

Commit

Permalink
Added useful operators to Dynamic classes so they can be constructed …
Browse files Browse the repository at this point in the history
…with Dictionary initializers.
  • Loading branch information
ewoutkramer committed Oct 16, 2024
1 parent 2d1a887 commit 81f2a28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/Hl7.Fhir.Base/Model/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ protected virtual bool TryGetValue(string key, [NotNullWhen(true)] out object? v
public class DynamicDataType : DataType
{
public void Add(string arg1, object arg2) => this.SetValue(arg1, arg2);

public object this[string key]
{
get => this.AsReadOnlyDictionary()[key];
set => SetValue(key, value);
}
}


Expand All @@ -119,4 +125,10 @@ public class DynamicDataType : DataType
public class DynamicResource : Resource
{
public void Add(string arg1, object arg2) => this.SetValue(arg1, arg2);

public object this[string key]
{
get => this.AsReadOnlyDictionary()[key];
set => SetValue(key, value);
}
}
24 changes: 15 additions & 9 deletions src/Hl7.Fhir.Support.Poco.Tests/Model/PocoDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using IRO = System.Collections.Generic.IReadOnlyDictionary<string, object>;

namespace Hl7.Fhir.Tests.Model;

Expand Down Expand Up @@ -37,7 +38,7 @@ public void DynamicResourceAcceptsEverything()
[TestMethod]
public void ResourceAcceptsOverflow()
{
var pat = new Patient();
var pat = new Patient().AsDictionary();

// setting an existing property to an incorrect type should fail.
Assert.ThrowsException<InvalidCastException>(() => pat["name"] = "John");
Expand All @@ -53,25 +54,30 @@ public void ResourceAcceptsOverflow()

pat["name"] = null!;
pat["weight"] = null!;
pat.AsReadOnlyDictionary().Should().BeEmpty();
pat.Should().BeEmpty();
}

[TestMethod]
public void CanReadSpecialProperties()
{
var pat = new Patient()
var patient = new Patient()
{
Text = new Narrative { Div = "<div>hello</div>" },
Active = true,
Meta = new Meta { ElementId = "4" },
};

pat.AddExtension("http://nu.nl", new FhirBoolean(true));
patient.AddExtension("http://nu.nl", new FhirBoolean(true));
var pat = patient.AsReadOnlyDictionary();

pat["active"].Should().BeOfType<FhirBoolean>().Which["value"].Should().Be(true);
pat["text"].Should().BeOfType<Narrative>().Which["div"].Should().BeOfType<XHtml>().Which["value"].Should().Be("<div>hello</div>");
pat["meta"].Should().BeOfType<Meta>().Which["id"].Should().Be("4");
var extension = pat["extension"].Should().BeOfType<List<Extension>>().Subject;
extension.Should().ContainSingle().Which["url"].Should().Be("http://nu.nl");
pat["active"].Should().BeOfType<FhirBoolean>().And
.BeAssignableTo<IRO>().Which["value"].Should().Be(true);
pat["text"].Should().BeOfType<Narrative>().And
.BeAssignableTo<IRO>().Which["div"].Should().BeOfType<XHtml>().And
.BeAssignableTo<IRO>().Which["value"].Should().Be("<div>hello</div>");
pat["meta"].Should().BeOfType<Meta>().And
.BeAssignableTo<IRO>().Which["id"].Should().Be("4");
var extension = pat["extension"].Should().BeOfType<List<Extension>>().Which.Should().ContainSingle().Subject;
extension.Should().BeAssignableTo<IRO>().Which["url"].Should().Be("http://nu.nl");
}
}

0 comments on commit 81f2a28

Please sign in to comment.