Very difficult to work with enums and option sets

Open
#2,105 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start by tracing the existing OptionSet and enum-marshalling APIs referenced in the issue, including OptionSet, OptionSetValue, and FormulaValue. Compare their current construction and value-selection paths with the proposed generic FromEnum and GetValue entry points. Done means enum-backed option sets can be created and values selected with compile-time checking, with relevant coverage added for the new API.

Written by the indexing model from the issue text.

Description

I have data in my app which contains standard enumerations. I want to expose these as OptionSets that users can interact with via formulas. Right now, to create the option set and a reference to one of the option set values I have to do the following:

var optionSet = new OptionSet("StateCode", DisplayNameUtility.MakeUnique(new Dictionary<string, string>()
{
{ nameof(msdyn_exportcontrolcode_statecode.Active), nameof(msdyn_exportcontrolcode_statecode.Active) },
{ nameof(msdyn_exportcontrolcode_statecode.Inactive), nameof(msdyn_exportcontrolcode_statecode.Inactive) },
}));

Microsoft.PowerFx.Types.OptionSetValue stateOptionSetValue = null;
        if (StateCodeOptionSet.OptionSet.TryGetValue(new Microsoft.PowerFx.Core.Utils.DName(Enum.GetName(typeof(msdyn_exportcontrolcode_statecode), State)), out stateOptionSetValue))
        {
            myFields.Add(new NamedValue("State", stateOptionSetValue));
        }

This isn't very discoverable. Since enum and option set marshalling is extremely common this should be a lot easier and give compile time checking for future changes, as well as let you explicitly pick a value out of the list without having to do TryParse. For example:

enum Fruit { apple , ... }
OptionSet os = OptionSet.FromEnum();
FormulaValue fv = os.GetValue(Fruit.Apple); // strongly typed OS.GetValue(T value)

Dominant language
C#
Stars
3.4k
Forks
358
Avg merge
10h 34m
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/Power-Fx

All issues in microsoft/Power-Fx

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.