Skip to content

Commit e97d754

Browse files
committed
Add DataTable block
1 parent 592f600 commit e97d754

8 files changed

Lines changed: 253 additions & 11 deletions

File tree

Slack.NetStandard.Tests/BlockTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,11 @@ public void Carousel()
245245
{
246246
Utility.AssertSubType<IMessageBlock, Carousel>("Blocks_Carousel.json");
247247
}
248+
249+
[Fact]
250+
public void DataTable()
251+
{
252+
Utility.AssertSubType<IMessageBlock, DataTable>("Blocks_DataTable.json");
253+
}
248254
}
249255
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"type": "data_table",
3+
"caption": "A Fabulous Table",
4+
"rows": [
5+
[
6+
{
7+
"type": "raw_text",
8+
"text": "Name"
9+
},
10+
{
11+
"type": "raw_number",
12+
"value": 1,
13+
"text": "1"
14+
},
15+
{
16+
"type": "raw_number",
17+
"value": 1,
18+
"text": "One"
19+
}
20+
],
21+
[
22+
{
23+
"type": "raw_text",
24+
"text": "Data Refinement Department"
25+
},
26+
{
27+
"type": "raw_text",
28+
"text": "MDR"
29+
},
30+
{
31+
"type": "rich_text",
32+
"elements": [
33+
{
34+
"type": "rich_text_section",
35+
"elements": [
36+
{
37+
"type": "text",
38+
"text": "Blue",
39+
"style": {
40+
"bold": true
41+
}
42+
}
43+
]
44+
}
45+
]
46+
}
47+
],
48+
[
49+
{
50+
"type": "raw_text",
51+
"text": "Art Sourcing Department"
52+
},
53+
{
54+
"type": "raw_text",
55+
"text": "O&D"
56+
},
57+
{
58+
"type": "rich_text",
59+
"elements": [
60+
{
61+
"type": "rich_text_section",
62+
"elements": [
63+
{
64+
"type": "text",
65+
"text": "Green"
66+
},
67+
{
68+
"type": "text",
69+
"text": "review",
70+
"style": {
71+
"italic": true
72+
}
73+
}
74+
]
75+
}
76+
]
77+
}
78+
],
79+
[
80+
{
81+
"type": "raw_text",
82+
"text": "Wellness Department"
83+
},
84+
{
85+
"type": "raw_text",
86+
"text": "Wellness Center"
87+
},
88+
{
89+
"type": "rich_text",
90+
"elements": [
91+
{
92+
"type": "rich_text_section",
93+
"elements": [
94+
{
95+
"type": "text",
96+
"text": "Limited",
97+
"style": {
98+
"bold": true
99+
}
100+
}
101+
]
102+
}
103+
]
104+
}
105+
]
106+
]
107+
}

Slack.NetStandard.Tests/Slack.NetStandard.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
<None Update="Examples\Blocks_Carousel.json">
3030
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3131
</None>
32+
<None Update="Examples\Blocks_DataTable.json">
33+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
34+
</None>
3235
<None Update="Examples\Blocks_Plan.json">
3336
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3437
</None>

Slack.NetStandard/JsonConverters/MessageBlockConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public override IMessageBlock ReadJson(JsonReader reader, Type objectType, IMess
6464
{nameof(Plan).ToLower(), typeof(Plan)},
6565
{nameof(Alert).ToLower(), typeof(Alert)},
6666
{nameof(Card).ToLower(), typeof(Card)},
67-
{nameof(Carousel).ToLower(), typeof(Carousel)}
67+
{nameof(Carousel).ToLower(), typeof(Carousel)},
68+
{DataTable.MessageBlockType, typeof(DataTable) }
6869
};
6970

7071
private IMessageBlock GetComponent(string type)

Slack.NetStandard/JsonConverters/TableRowConverter.cs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,60 @@ public override TableRow ReadJson(JsonReader reader, Type objectType, TableRow e
1414
while(reader.TokenType != JsonToken.EndArray)
1515
{
1616
var jObject = JObject.Load(reader);
17-
if(jObject.Value<string>("type") == "raw_text")
17+
var type = jObject.Value<string>("type");
18+
switch (type)
1819
{
19-
tableRow.Cells.Add(new RawTextCell(jObject.Value<string>("text")));
20-
}
21-
else if(jObject.Value<string>("type") == "rich_text")
22-
{
23-
var richItem = new RichText();
24-
serializer.Populate(jObject.CreateReader(), richItem);
25-
tableRow.Cells.Add(new RichTextCell(richItem));
20+
case "raw_number":
21+
{
22+
var valueToken = jObject["value"];
23+
var text = jObject.Value<string>("text");
24+
if (valueToken != null)
25+
{
26+
switch (valueToken.Type)
27+
{
28+
case JTokenType.Integer:
29+
{
30+
var intVal = valueToken.Value<int>();
31+
if (text != null)
32+
tableRow.Cells.Add(new RawNumberCell(intVal, text));
33+
else
34+
tableRow.Cells.Add(new RawNumberCell(intVal));
35+
break;
36+
}
37+
case JTokenType.Float:
38+
{
39+
var decVal = valueToken.Value<decimal>();
40+
if (text != null)
41+
tableRow.Cells.Add(new RawNumberCell(decVal, text));
42+
else
43+
tableRow.Cells.Add(new RawNumberCell(decVal));
44+
break;
45+
}
46+
default:
47+
{
48+
// value must be numeric; attempt a decimal conversion for other numeric-like tokens
49+
var decVal = valueToken.Value<decimal>();
50+
if (text != null) tableRow.Cells.Add(new RawNumberCell(decVal, text));
51+
else tableRow.Cells.Add(new RawNumberCell(decVal));
52+
break;
53+
}
54+
}
55+
}
56+
57+
break;
58+
}
59+
case "raw_text":
60+
{
61+
tableRow.Cells.Add(new RawTextCell(jObject.Value<string>("text")));
62+
break;
63+
}
64+
case "rich_text":
65+
{
66+
var richItem = new RichText();
67+
serializer.Populate(jObject.CreateReader(), richItem);
68+
tableRow.Cells.Add(new RichTextCell(richItem));
69+
break;
70+
}
2671
}
2772

2873
reader.Read();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Newtonsoft.Json;
4+
5+
namespace Slack.NetStandard.Messages.Blocks
6+
{
7+
public class DataTable : IMessageBlock
8+
{
9+
public const string MessageBlockType = "data_table";
10+
[JsonProperty("type")] public string Type => MessageBlockType;
11+
12+
public DataTable() { }
13+
14+
public DataTable(string caption)
15+
{
16+
Caption = caption;
17+
}
18+
19+
[JsonProperty("block_id", NullValueHandling = NullValueHandling.Ignore)]
20+
public string BlockId { get; set; }
21+
22+
[JsonProperty("caption", NullValueHandling = NullValueHandling.Ignore)]
23+
public string Caption { get; set; }
24+
25+
[JsonProperty("page_size", NullValueHandling = NullValueHandling.Ignore)]
26+
public int? PageSize { get; set; }
27+
28+
[JsonProperty("row_header_column_index", NullValueHandling = NullValueHandling.Ignore)]
29+
public int? RowHeaderColumnIndex { get; set; }
30+
31+
[JsonProperty("rows", NullValueHandling = NullValueHandling.Ignore)]
32+
public List<TableRow> Rows { get; set; } = new List<TableRow>();
33+
34+
public bool ShouldSerializeRows() => Rows?.Any() ?? false;
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using Newtonsoft.Json.Linq;
3+
4+
namespace Slack.NetStandard.Messages.Blocks
5+
{
6+
public class RawNumberCell : ITableRowCell
7+
{
8+
public RawNumberCell(int value)
9+
{
10+
Number = value;
11+
_isInteger = true;
12+
}
13+
14+
public RawNumberCell(int value, string text):this(value)
15+
{
16+
Text = text;
17+
}
18+
19+
public RawNumberCell(decimal value)
20+
{
21+
Number = value;
22+
_isInteger = false;
23+
}
24+
25+
public RawNumberCell(decimal value, string text):this(value)
26+
{
27+
Text = text;
28+
}
29+
30+
private bool _isInteger;
31+
public decimal Number { get; set; }
32+
public string Text { get; set; }
33+
34+
public object GenerateCell()
35+
{
36+
object valueProp = _isInteger ? (object)Convert.ToInt32(Number) : (object)Number;
37+
return new JObject(
38+
new JProperty("type", "raw_number"),
39+
new JProperty("value", valueProp),
40+
new JProperty("text", Text ?? Number.ToString())
41+
);
42+
}
43+
}
44+
}

Slack.NetStandard/Slack.NetStandard.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>11.3.0</Version>
5+
<Version>11.4.0</Version>
66
<Description>.NET Core package that helps with Slack interactions, events and web API</Description>
77
<PackageLicenseFile>LICENSE</PackageLicenseFile>
88
<PackageReleaseNotes>
9-
Add block chunk support to stream methods (thanks once again @TonioOoOo!)
9+
Implement DataTable block
1010
</PackageReleaseNotes>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>

0 commit comments

Comments
 (0)