Power-Fx
Power-Fx copied to clipboard
Very difficult to work with enums and option sets
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<Fruit> os = OptionSet.FromEnum<Fruit>(); FormulaValue fv = os.GetValue(Fruit.Apple); // strongly typed OS<T>.GetValue(T value)