kandi background

fhir | Official source for the HL7 FHIR Specification

Download this library from

kandi X-RAY | fhir Summary

fhir is a HTML library. fhir has no bugs, it has no vulnerabilities and it has low support. However fhir has a Non-SPDX License. You can download it from GitHub.
This library builds and publishes the FHIR specification, based on the contained spreadsheet data in the project. | CI Status (master) | CI Status (R4B) | | :---: | :---: | | | |.

kandi-support Support

  • fhir has a low active ecosystem.
  • It has 307 star(s) with 139 fork(s). There are 59 watchers for this library.
  • It had no major release in the last 12 months.
  • fhir has no issues reported. There are 42 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of fhir is current.

quality kandi Quality

  • fhir has no bugs reported.

securitySecurity

  • fhir has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.

license License

  • fhir has a Non-SPDX License.
  • Non-SPDX licenses can be open source with a non SPDX compliant license, or non open source licenses, and you need to review them closely before use.

buildReuse

  • fhir releases are not available. You will need to build from source code and install.
Top functions reviewed by kandi - BETA

Coming Soon for all Libraries!

Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.

fhir Key Features

Official source for the HL7 FHIR Specification

fhir Examples and Code Snippets

  • Parse string to enum with custom attribute
  • Using Python Requests to POST data to REST API
  • spring cloud gateway only forwards to base path of url
  • How to keep Firely.Terminal from trashing the FHIR package cache?
  • Python loop through ndjson file data
  • MongoDB - Querying inner object that is an array
  • How to update a value in a complex fhirBundle JSON?
  • elementFromPoint not recognising certain elements
  • Get the id of one of the many elements with the same class which reached the top
  • java streams: accumulated collector

Parse string to enum with custom attribute

public static class EnumExtensions
{
    public static T GetValueFromEnumMember<T>(string value) where T: Enum
    {
        var type = typeof(T);
        foreach (var field in type.GetFields())
        {
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(EnumMemberAttribute)) as EnumMemberAttribute;
            if (attribute != null)
            {
                if (attribute.Value == value)
                    return (T)field.GetValue(null);
            }
            else
            {
                if (field.Name == value)
                    return (T)field.GetValue(null);
            }
        }
        throw new ArgumentException($"unknow value: {value}");
    }
}
EnumExtensions.GetValueFromEnumMember<CarePlanActivityStatus>(stringValueToTest)
-----------------------
public static class EnumExtensions
{
    public static T GetValueFromEnumMember<T>(string value) where T: Enum
    {
        var type = typeof(T);
        foreach (var field in type.GetFields())
        {
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(EnumMemberAttribute)) as EnumMemberAttribute;
            if (attribute != null)
            {
                if (attribute.Value == value)
                    return (T)field.GetValue(null);
            }
            else
            {
                if (field.Name == value)
                    return (T)field.GetValue(null);
            }
        }
        throw new ArgumentException($"unknow value: {value}");
    }
}
EnumExtensions.GetValueFromEnumMember<CarePlanActivityStatus>(stringValueToTest)
-----------------------
var hold = typeof(CarePlanActivityStatus).GetFields()
    .Where(a => a.GetCustomAttribute<EnumLiteralAttribute>(false)?.Literal == data);
var hold = typeof(CarePlanActivityStatus).GetFields()
    .FirstOrDefault(a => a.GetCustomAttribute<EnumLiteralAttribute>(false)?.Literal == data)
    ?.GetValue(null);
string data = "cancelled";
var hold = typeof(CarePlanActivityStatus).GetFields()
    .FirstOrDefault(a => a.GetCustomAttributes<EnumLiteralAttribute>(false)
        .Any(x => x.Literal == data))
    ?.GetValue(null);
hold.Dump();
-----------------------
var hold = typeof(CarePlanActivityStatus).GetFields()
    .Where(a => a.GetCustomAttribute<EnumLiteralAttribute>(false)?.Literal == data);
var hold = typeof(CarePlanActivityStatus).GetFields()
    .FirstOrDefault(a => a.GetCustomAttribute<EnumLiteralAttribute>(false)?.Literal == data)
    ?.GetValue(null);
string data = "cancelled";
var hold = typeof(CarePlanActivityStatus).GetFields()
    .FirstOrDefault(a => a.GetCustomAttributes<EnumLiteralAttribute>(false)
        .Any(x => x.Literal == data))
    ?.GetValue(null);
hold.Dump();
-----------------------
var hold = typeof(CarePlanActivityStatus).GetFields()
    .Where(a => a.GetCustomAttribute<EnumLiteralAttribute>(false)?.Literal == data);
var hold = typeof(CarePlanActivityStatus).GetFields()
    .FirstOrDefault(a => a.GetCustomAttribute<EnumLiteralAttribute>(false)?.Literal == data)
    ?.GetValue(null);
string data = "cancelled";
var hold = typeof(CarePlanActivityStatus).GetFields()
    .FirstOrDefault(a => a.GetCustomAttributes<EnumLiteralAttribute>(false)
        .Any(x => x.Literal == data))
    ?.GetValue(null);
hold.Dump();
-----------------------
var enumType = typeof(CarePlanActivityStatus);
FieldInfo field = enumType.GetField(nameof(CarePlanActivityStatus.Cancelled));
var enumLiteralAttribute = field.GetCustomAttribute<EnumLiteralAttribute>(false);
WriteLine(enumLiteralAttribute.Name); // canceled
WriteLine(enumLiteralAttribute.Url); // http://hl7.org/fhir/care-plan-activity-status

Community Discussions

Trending Discussions on fhir
  • Parse string to enum with custom attribute
  • Reverse chaining in FHIR
  • FHIR search for duplicate patients
  • Using Python Requests to POST data to REST API
  • spring cloud gateway only forwards to base path of url
  • What resources are (typically|supposed |required) to be in a /Patient/[ID]/$everything FHIR resource?
  • How to keep Firely.Terminal from trashing the FHIR package cache?
  • Python loop through ndjson file data
  • FHIR publisher fails on a java NullPointerException
  • MongoDB - Querying inner object that is an array
Trending Discussions on fhir

QUESTION

Parse string to enum with custom attribute

Asked 2022-Apr-04 at 09:16

I have an enum as follows. I also have a string in-progress I am trying to parse this string to the appropriate enum. As seen by the following test we want to parse to take a string and return the enum

    [Fact]
    public void TestParseOfEnum()
    {
        var data = "not-started";

        var parsed = EnumExtensions.GetValueFromEnumMember<CarePlan.CarePlanActivityStatus>(data);

        parsed.ShouldBe(CarePlan.CarePlanActivityStatus.NotStarted);
    }

The issue being that enum try parse is checking on the name which means its failing. I need to parse it on this custom attribute.

 CarePlan.CarePlanActivityStatus status
 Enum.TryParse("in-progress", out status)

A try parse will not find this as try parse checks on the name field and not on the custom attribute within this enum. So this would return false and not find my enum

I have been trying to see how I could get a list back of all of the fields within the enum and then test on the literal.

This would work but i have to specify each of the values within the enum in getfield

  var res = typeof(CarePlan.CarePlanActivityStatus)
                 .GetField(nameof(CarePlan.CarePlanActivityStatus.Cancelled))
                 .GetCustomAttribute<EnumLiteralAttribute>(false)
                 .Literal;
            

I tried something like this but Literal doesn't exist at this point apparently so this fails as well.

  var hold = typeof(CarePlan.CarePlanActivityStatus).GetFields().Where(a =>
            a.GetCustomAttributes<EnumLiteralAttribute>(false).Literal.Equals(data));

The enum

The nuget package i am using for the fhir library is Hl7.Fhir.R4

[FhirEnumeration("CarePlanActivityStatus")]
public enum CarePlanActivityStatus
{
  /// <summary>
  /// Care plan activity is planned but no action has yet been taken.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("not-started", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("Not Started")] NotStarted,
  /// <summary>
  /// Appointment or other booking has occurred but activity has not yet begun.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("scheduled", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("Scheduled")] Scheduled,
  /// <summary>
  /// Care plan activity has been started but is not yet complete.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("in-progress", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("In Progress")] InProgress,
  /// <summary>
  /// Care plan activity was started but has temporarily ceased with an expectation of resumption at a future time.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("on-hold", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("On Hold")] OnHold,
  /// <summary>
  /// Care plan activity has been completed (more or less) as planned.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("completed", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("Completed")] Completed,
  /// <summary>
  /// The planned care plan activity has been withdrawn.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("cancelled", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("Cancelled")] Cancelled,
  /// <summary>
  /// The planned care plan activity has been ended prior to completion after the activity was started.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("stopped", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("Stopped")] Stopped,
  /// <summary>
  /// The current state of the care plan activity is not known.  Note: This concept is not to be used for "other" - one of the listed statuses is presumed to apply, but the authoring/source system does not know which one.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("unknown", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("Unknown")] Unknown,
  /// <summary>
  /// Care plan activity was entered in error and voided.
  /// (system: http://hl7.org/fhir/care-plan-activity-status)
  /// </summary>
  [EnumLiteral("entered-in-error", "http://hl7.org/fhir/care-plan-activity-status"), Hl7.Fhir.Utility.Description("Entered in Error")] EnteredInError,
}

// example of everything I have tested with

The nuget package i am using for the fhir library is Hl7.Fhir.R4

public static class EnumExtensions
{
    public static T GetValueFromEnumMember<T>(string value) where T : Enum
    {
            {
                
                
                // Doesnt work as .Literal shows as red not valid.
                var hold = typeof(CarePlan.CarePlanActivityStatus).GetFields().Where(a =>
                      a.GetCustomAttributes<EnumLiteralAttribute>(false).Literal.Equals(data));
                
                
                // doesnt work as its returning the string i need it to return the enum
                var res = typeof(CarePlan.CarePlanActivityStatus)
                    .GetField(nameof(CarePlan.CarePlanActivityStatus.Cancelled))
                    .GetCustomAttribute<EnumLiteralAttribute>(false)
                    .Literal;
                
                
                

// neither of the following two work as they are just looping though and i cant find the enum they find.
                foreach (CarePlan.CarePlanActivityStatus status in (CarePlan.CarePlanActivityStatus[])Enum.GetValues(
                             typeof(CarePlan.CarePlanActivityStatus)))
                {
                }

                foreach (string i in Enum.GetNames(typeof(CarePlan.CarePlanActivityStatus)))
                {
                    // var res = typeof(CarePlan.CarePlanActivityStatus)
                    //     .GetField(nameof(CarePlan.CarePlanActivityStatus[i]))
                    //     .GetCustomAttribute<EnumLiteralAttribute>(false)
                    //     .Literal;
                    //
                    // Console.WriteLine($" {res}" );  
                    //
                    // Console.WriteLine($" {i}" );  
                }
                
            }

Isnt there a way to parse a string to an enum without making a large mess of a if statements to test it. Ideally i need to create a generic method as i have about 10 of these enums i need test.

ANSWER

Answered 2022-Apr-04 at 08:43

You can try with a extension method to read the Custom Atribute from Enums:

public static class EnumExtensions
{
    public static T GetValueFromEnumMember<T>(string value) where T: Enum
    {
        var type = typeof(T);
        foreach (var field in type.GetFields())
        {
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(EnumMemberAttribute)) as EnumMemberAttribute;
            if (attribute != null)
            {
                if (attribute.Value == value)
                    return (T)field.GetValue(null);
            }
            else
            {
                if (field.Name == value)
                    return (T)field.GetValue(null);
            }
        }
        throw new ArgumentException($"unknow value: {value}");
    }
}

and use it like this:

EnumExtensions.GetValueFromEnumMember<CarePlanActivityStatus>(stringValueToTest)

it returns the Enum Value

Source https://stackoverflow.com/questions/71734138

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

Vulnerabilities

No vulnerabilities reported

Install fhir

You can download it from GitHub.

Support

This is the source for the FHIR specification itself. Only the editors of the specification (a small group) need to build this. If that's not you, one of these links should get you going:.