microsoft / microsoft/Power-Fx
Type coersion issues
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 3.4k
- Forks
- 358
- Avg merge
- 10h 34m
- Merged PRs (30d)
- 3
Description
I'm trying to use Power Fx to calculate values that I can then use in an SDK app to update Dataverse records. I want to check that a formula will produce values of the expected type for each attribute, and use the built-in type coersion to do conversions where necessary.
I'm getting the expected type by first getting the record type of the target entity, then getting the type of the field within it, e.g.:
var connection = SingleOrgPolicy.New(org);
var recordType = connection.GetRecordType("contact");
var attributeType = recordType.GetFieldType("gendercode");
I can also get the type that will be produced by the formula, e.g.:
var checkResult = scope.Check("'Gender (Contacts)'.Female");
To check if the types are compatible I'm using:
if (!checkResult.ReturnType.CanPotentiallyCoerceTo(attributeType))
{
throw new ...
}
In most cases this works well, but it seems to throw a StackOverflowException for optionset types like this. I can work around this by changing this check to:
if (!checkResult.ReturnType.Equals(attributeType) && !checkResult.ReturnType.CanPotentiallyCoerceTo(attributeType))
{
throw new ...
}
but it would be good if CanPotentiallyCoerceTo worked consistently.
Once I've actually evaluated the formula and got a value I then need to actually do the coersion. In most cases I can do:
var eval = checkResult.GetEvaluator();
var result = eval.Eval(runtimeConfig);
if (!result.TryCoerceTo(attributeType, out var convertedValue))
{
throw new ...
}
However, this again doesn't work for optionset values, or for lookups. I can work around this by adding various special cases for different result types but it would be cleaner if this was wrapped up within the TryCoerceTo method.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the CanPotentiallyCoerceTo and TryCoerceTo entry points described in the issue, reproducing the failures with optionset values and lookups. Trace the type comparisons and conversions involved; done means compatibility checks avoid StackOverflowException and TryCoerceTo handles those result types consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100