Skip to content

Commit abe4128

Browse files
committed
Moves POCO and handles null ref.
1 parent 2fcab0f commit abe4128

File tree

11 files changed

+252
-25
lines changed

11 files changed

+252
-25
lines changed

src/libraries/Microsoft.PowerFx.Connectors/Internal/Wrappers/Extensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
57
using System.Text.Json;
68
using Microsoft.OpenApi.Any;
79
using Microsoft.OpenApi.Interfaces;
10+
using Microsoft.PowerFx.Core.Functions.Delegation;
811

912
namespace Microsoft.PowerFx.Connectors
1013
{
@@ -27,5 +30,15 @@ public static IOpenApiExtension ToIOpenApiExtension(this JsonElement je)
2730
_ => throw new NotImplementedException()
2831
};
2932
}
33+
34+
public static IEnumerable<string> ToStr(this IEnumerable<DelegationOperator> ops)
35+
{
36+
if (ops == null)
37+
{
38+
return null;
39+
}
40+
41+
return ops.Select(op => op.ToString().ToLowerInvariant());
42+
}
3043
}
3144
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Text.Json.Serialization;
9+
using Microsoft.PowerFx.Core.Functions.Delegation;
10+
11+
namespace Microsoft.PowerFx.Connectors
12+
{
13+
public class CapabilitiesPoco
14+
{
15+
[JsonPropertyName("sortRestrictions")]
16+
public Sort SortRestrictions { get; set; }
17+
18+
[JsonPropertyName("filterRestrictions")]
19+
public Filter FilterRestrictions { get; set; }
20+
21+
[JsonPropertyName("isOnlyServerPagable")]
22+
public bool IsOnlyServerPagable { get; set; }
23+
24+
// "top", "skiptoken"
25+
[JsonPropertyName("serverPagingOptions")]
26+
public string[] ServerPagingOptions { get; set; }
27+
28+
// and,or,eq, etc...
29+
// DelegationOperator
30+
[JsonPropertyName("filterFunctionSupport")]
31+
public string[] FilterFunctionSupport { get; set; }
32+
33+
public CapabilitiesPoco SetOps(IEnumerable<DelegationOperator> ops)
34+
{
35+
this.FilterFunctionSupport = ops.ToStr().ToArray();
36+
return this;
37+
}
38+
39+
public IEnumerable<DelegationOperator> FilterFunctionSupportOps() => Utilities.ToDelegationOp(FilterFunctionSupport);
40+
41+
// 3
42+
[JsonPropertyName("odataVersion")]
43+
public int OdataVersion { get; set; }
44+
}
45+
46+
public class Filter
47+
{
48+
[JsonPropertyName("filterable")]
49+
public bool Filterable { get; set; }
50+
51+
[JsonPropertyName("nonFilterableProperties")]
52+
public string[] NonFilterableProperties { get; set; }
53+
}
54+
55+
public class Sort
56+
{
57+
[JsonPropertyName("sortable")]
58+
public bool Sortable { get; set; }
59+
60+
[JsonPropertyName("unsortableProperties")]
61+
public string[] UnsortableProperties { get; set; }
62+
}
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Text.Json.Serialization;
9+
using Microsoft.PowerFx.Core.Functions.Delegation;
10+
11+
namespace Microsoft.PowerFx.Connectors
12+
{
13+
public class ColumnCapabilitiesPoco
14+
{
15+
[JsonPropertyName("filterFunctions")]
16+
public string[] FilterFunctions { get; set; }
17+
18+
// Strong-typing
19+
public IEnumerable<DelegationOperator> FilterFunctionOps() =>
20+
Utilities.ToDelegationOp(FilterFunctions);
21+
22+
public static ColumnCapabilitiesPoco New(IEnumerable<DelegationOperator> ops)
23+
{
24+
return new ColumnCapabilitiesPoco
25+
{
26+
FilterFunctions = ops.ToStr().ToArray()
27+
};
28+
}
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using System.Text.Json.Serialization;
8+
9+
namespace Microsoft.PowerFx.Connectors
10+
{
11+
public class GetTablePoco
12+
{
13+
[JsonPropertyName("name")]
14+
public string Name { get; set; }
15+
16+
[JsonPropertyName("x-ms-permission")]
17+
public string Permissions { get; set; } // read-write
18+
19+
// $$$ Fill in rest of stuff here...
20+
[JsonPropertyName("x-ms-capabilities")]
21+
public CapabilitiesPoco Capabilities { get; set; }
22+
23+
[JsonPropertyName("schema")]
24+
public TableSchemaPoco Schema { get; set; }
25+
}
26+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using System.Text.Json.Serialization;
8+
9+
namespace Microsoft.PowerFx.Connectors
10+
{
11+
public class TableSchemaPoco
12+
{
13+
[JsonPropertyName("type")]
14+
public string Type { get; set; } = "array";
15+
16+
[JsonPropertyName("items")]
17+
public ItemsPoco Items { get; set; }
18+
}
19+
20+
public class ItemsPoco
21+
{
22+
[JsonPropertyName("type")]
23+
public string Type { get; set; } = "object";
24+
25+
// need required flag.
26+
[JsonPropertyName("properties")]
27+
public Dictionary<string, ColumnInfoPoco> Properties { get; set; }
28+
}
29+
30+
public class ColumnInfoPoco
31+
{
32+
[JsonPropertyName("title")]
33+
public string Title { get; set; }
34+
35+
[JsonPropertyName("description")]
36+
public string Description { get; set; }
37+
38+
[JsonPropertyName("type")]
39+
public string Type { get; set; } // $$$ "integer", "string",
40+
41+
// What sorting capabilities?
42+
// "asc,desc"
43+
[JsonPropertyName("x-ms-sort")]
44+
public string Sort { get; set; }
45+
46+
// What filter capabilities?
47+
[JsonPropertyName("x-ms-capabilities")]
48+
public ColumnCapabilitiesPoco Capabilities { get; set; }
49+
}
50+
}

src/libraries/Microsoft.PowerFx.Connectors/Public/ConnectorType.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,14 @@ internal ConnectorType(JsonElement schema, string tableName, SymbolTable optionS
273273
DisplayName = displayName;
274274
FieldMetadata = fieldMetadata;
275275

276-
foreach (ConnectorType field in Fields.Where(f => f.Capabilities != null))
276+
if (serviceCapabilities != null)
277277
{
278-
serviceCapabilities.AddColumnCapability(field.Name, field.Capabilities);
278+
foreach (ConnectorType field in Fields.Where(f => f.Capabilities != null))
279+
{
280+
serviceCapabilities.AddColumnCapability(field.Name, field.Capabilities);
281+
}
279282
}
280-
283+
281284
FormulaType = new CdpRecordType(this, resolver, ServiceCapabilities.ToDelegationInfo(serviceCapabilities, name, isTableReadOnly, this, datasetName), FieldMetadata);
282285
}
283286

src/libraries/Microsoft.PowerFx.Connectors/Tabular/CdpTableResolver.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ public async Task<ConnectorType> ResolveTableAsync(string logicalName, Cancellat
5959
}
6060

6161
string dataset = _doubleEncoding ? CdpServiceBase.DoubleEncode(_tabularTable.DatasetName) : CdpServiceBase.SingleEncode(_tabularTable.DatasetName);
62+
63+
// URI to fetch table metadata.
6264
string uri = (_uriPrefix ?? string.Empty) + (UseV2(_uriPrefix) ? "/v2" : string.Empty) + $"/$metadata.json/datasets/{dataset}/tables/{CdpServiceBase.DoubleEncode(logicalName)}?api-version=2015-09-01";
6365

66+
// $$$ we need to return POCO
6467
string text = await CdpServiceBase.GetObject(_httpClient, $"Get table metadata", uri, null, cancellationToken, Logger).ConfigureAwait(false);
6568

6669
if (string.IsNullOrWhiteSpace(text))

src/libraries/Microsoft.PowerFx.Connectors/Tabular/Services/CdpServiceBase.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ protected internal static async Task<T> GetObject<T>(HttpClient httpClient, stri
2525

2626
string result = await GetObject(httpClient, message, uri, content, cancellationToken, logger, $"{callingMethod}<{typeof(T).Name}>").ConfigureAwait(false);
2727
T res = string.IsNullOrWhiteSpace(result) ? null : JsonSerializer.Deserialize<T>(result);
28-
29-
if (res is ISupportsPostProcessing postProcessing)
30-
{
31-
postProcessing.PostProcess();
32-
}
33-
3428
return res;
3529
}
3630

src/libraries/Microsoft.PowerFx.Connectors/Tabular/Services/CdpTable.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class CdpTable : CdpService
4141

4242
internal ConnectorType TabularTableDescriptor;
4343

44-
internal IReadOnlyCollection<RawTable> Tables;
44+
internal IEnumerable<RawTable> Tables;
4545

4646
private string _uriPrefix;
4747

@@ -56,7 +56,7 @@ public class CdpTable : CdpService
5656

5757
public override ConnectorSettings ConnectorSettings => _connectorSettings;
5858

59-
internal CdpTable(string dataset, string table, IReadOnlyCollection<RawTable> tables, ConnectorSettings connectorSettings, CDPMetadataItem fieldMetadata = null)
59+
internal CdpTable(string dataset, string table, IEnumerable<RawTable> tables, ConnectorSettings connectorSettings, CDPMetadataItem fieldMetadata = null)
6060
{
6161
DatasetName = dataset ?? throw new ArgumentNullException(nameof(dataset));
6262
TableName = table ?? throw new ArgumentNullException(nameof(table));
@@ -65,7 +65,7 @@ internal CdpTable(string dataset, string table, IReadOnlyCollection<RawTable> ta
6565
_fieldMetadata = fieldMetadata;
6666
}
6767

68-
internal CdpTable(string dataset, string table, DatasetMetadata datasetMetadata, IReadOnlyCollection<RawTable> tables, ConnectorSettings connectorSettings, CDPMetadataItem fieldMetadata)
68+
internal CdpTable(string dataset, string table, DatasetMetadata datasetMetadata, IEnumerable<RawTable> tables, ConnectorSettings connectorSettings, CDPMetadataItem fieldMetadata)
6969
: this(dataset, table, tables, connectorSettings, fieldMetadata)
7070
{
7171
DatasetMetadata = datasetMetadata;

src/libraries/Microsoft.PowerFx.Connectors/Tabular/Services/InternalObjects.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,32 @@
99
namespace Microsoft.PowerFx.Connectors
1010
{
1111
// Used by ConnectorDataSource.GetTablesAsync
12-
internal class GetTables : ISupportsPostProcessing
12+
public class GetTables
1313
{
1414
[JsonPropertyName("@metadata")]
15-
public List<CDPMetadataItem> Metadata { get; set; }
15+
internal List<CDPMetadataItem> Metadata { get; set; }
1616

17-
[JsonPropertyName("value")]
18-
public List<RawTable> Value { get; set; }
17+
private IEnumerable<RawTable> _value;
1918

20-
public void PostProcess()
19+
[JsonPropertyName("value")]
20+
public IEnumerable<RawTable> Value
2121
{
22-
Value = Value.Select(rt => new RawTable() { Name = rt.Name, DisplayName = rt.DisplayName.Split('.').Last().Replace("[", string.Empty).Replace("]", string.Empty) }).ToList();
22+
get => _value;
23+
set =>
24+
_value = value?
25+
.Select(rt => new RawTable
26+
{
27+
Name = rt.Name,
28+
DisplayName = rt.DisplayName
29+
.Split('.')
30+
.Last()
31+
.Replace("[", string.Empty)
32+
.Replace("]", string.Empty)
33+
})
34+
.ToList();
2335
}
2436
}
2537

26-
internal interface ISupportsPostProcessing
27-
{
28-
void PostProcess();
29-
}
30-
3138
internal class CDPMetadataItem
3239
{
3340
[JsonPropertyName("name")]
@@ -68,12 +75,17 @@ public class CDPSensitivityLabelInfo
6875
public bool IsParent { get; set; }
6976
}
7077

71-
internal class RawTable
78+
public class RawTable
7279
{
73-
// Logical Name
80+
/// <summary>
81+
/// Logical name of the table.
82+
/// </summary>
7483
[JsonPropertyName("Name")]
7584
public string Name { get; set; }
7685

86+
/// <summary>
87+
/// Display name of the table.
88+
/// </summary>
7789
[JsonPropertyName("DisplayName")]
7890
public string DisplayName { get; set; }
7991

0 commit comments

Comments
 (0)