Skip to content

Commit abeefe7

Browse files
mitikovmr.slow
andauthored
Easier number to Expression (#72)
* Illustrate simple expression requires 2 type to be specified two times * Add implicit casts for numbers * Document how it behaves for null entries * Update README.md * Update README.md Fix typo Co-authored-by: mr.slow <no-reply@github.com>
1 parent e5b879f commit abeefe7

3 files changed

Lines changed: 74 additions & 3 deletions

File tree

docs/expressions/README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,44 @@ var layer = new AzureMapsControl.Components.Layers.PolygonExtrusionLayer
3232
};
3333
```
3434

35+
#### Checking if property exists
36+
37+
A necessity to check if [property `has` value](https://docs.microsoft.com/en-us/azure/azure-maps/data-driven-style-expressions-web-sdk#data-expressions) to decide which value to pick during clustering (or data display):
38+
39+
```
40+
var expression = Expression.HasProperty("score");
41+
```
42+
43+
#### Property value getter
44+
45+
Similarly `get` [property value](https://docs.microsoft.com/en-us/azure/azure-maps/data-driven-style-expressions-web-sdk#data-expressions) can be fetched:
46+
47+
```
48+
var leafProp = Expression.GetProperty("leafValue");
49+
```
50+
51+
#### Double is treated as `ExpressionOrNumber`
52+
53+
A portion of APIs require an expression to be evaluated to number, like bubble radius. Since `ExpressionOrNumber` has `implicit` cast from `double?`, you can simply specify number:
54+
55+
```
56+
ExpressionOrNumber smallRadius = 10;
57+
```
58+
59+
60+
#### Converting `Expression` to `ExpressionOrNumber`
61+
62+
When `expression` evaluates to number, but is represented as general-purpose `Expression` type, call `.ToNumber() to apply `[to-number` cast expression](https://docs.microsoft.com/en-us/azure/azure-maps/data-driven-style-expressions-web-sdk#data-expressions):
63+
64+
```
65+
Expression alreadyNumber = new ExpressionOrNumber(5); // expression variable is a number for sure
66+
ExpressionOrNumber expression = alreadyNumber.ToNumber();
67+
```
68+
69+
#### More examples
70+
71+
More API samples can be found in [Expressions Unit Test block](https://github.com/arnaudleclerc/AzureMapsControl.Components/tree/develop/tests/AzureMapsControl.Components.Tests/Atlas/Expression.cs).
72+
3573
### Using expressions
3674

3775
The following example would fill the layer with a color based on the `DENSITY` property of the layer source :
@@ -83,4 +121,4 @@ var layer = new AzureMapsControl.Components.Layers.PolygonExtrusionLayer
83121
FillColor = new Components.Atlas.ExpressionOrString(System.Text.Json.JsonDocument.Parse(fillColorExpressionJsonString))
84122
}
85123
};
86-
```
124+
```

src/AzureMapsControl.Components/Atlas/Expression.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ private struct ClusterProperties
9494
/// </summary>
9595
public static readonly string PointCount = "point_count";
9696
}
97+
98+
public static implicit operator Expression(double? value) => new ExpressionOrNumber(value);
9799
}
98100

99101
/// <summary>
@@ -123,6 +125,8 @@ public ExpressionOrNumber(JsonDocument json) : base(json) { }
123125
/// </summary>
124126
/// <param name="value">Value which will be used instead of the expression</param>
125127
public ExpressionOrNumber(double? value) => Value = value;
128+
129+
public static implicit operator ExpressionOrNumber(double? value) => new(value);
126130
}
127131

128132
/// <summary>

tests/AzureMapsControl.Components.Tests/Atlas/Expression.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,35 @@ public void ToNumber_WhenNotYetNumber_Wraps()
7373

7474
public class ExpressionOrNumberTests
7575
{
76+
[Fact]
77+
public void Number_WhenSupplied_IsExpressionWithoutBoilerplate()
78+
{
79+
ExpressionOrNumber smallRadius = 10;
80+
Assert.IsType<ExpressionOrNumber>(smallRadius);
81+
}
82+
83+
[Fact]
84+
public void Cast_ForNull_IsNull()
85+
{
86+
ExpressionOrNumber expression = null;
87+
Assert.Null(expression);
88+
}
89+
90+
[Fact]
91+
public void Cast_ForNullableDouble_IsExpression()
92+
{
93+
double? value = null;
94+
ExpressionOrNumber expression = value;
95+
Assert.NotNull(expression);
96+
}
97+
98+
[Fact]
99+
public void Number_WhenSupplied_CanBeExpressionWithoutBoilerplate()
100+
{
101+
var radius = Expression.Conditional(Expression.IsCluster, 10, 5);
102+
Assert.IsType<Expression>(radius);
103+
}
104+
76105
[Fact]
77106
public void Type_Is_DebugFriendly()
78107
{
@@ -116,8 +145,8 @@ public void Should_WriteExpressions()
116145
[Fact]
117146
public void Should_WriteNumberValue()
118147
{
119-
var value = 1;
120-
var expression = new ExpressionOrNumber(value);
148+
double value = 1;
149+
ExpressionOrNumber expression = value;
121150

122151
TestAndAssertWrite(expression, value.ToString());
123152
}

0 commit comments

Comments
 (0)