-
Notifications
You must be signed in to change notification settings - Fork 232
feat(model): add table Block Kit block #1611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
038976e
3a52834
cdf0bf7
2f5a86e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.slack.api.model.block; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| /** | ||
| * A {@code raw_number} table cell, holding a numeric value. The optional {@code text} | ||
| * field carries the display representation of the value (for example a formatted string). | ||
| * | ||
| * @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a> | ||
| */ | ||
| @Data | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class RawNumberTableCell implements TableCell { | ||
| public static final String TYPE = "raw_number"; | ||
| private final String type = TYPE; | ||
| private Double value; | ||
| private String text; | ||
| } | ||
|
Comment on lines
+18
to
+23
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 note: I find |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.slack.api.model.block; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| /** | ||
| * A {@code raw_text} table cell, holding unformatted plain text. | ||
| * | ||
| * @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a> | ||
| */ | ||
| @Data | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class RawTextTableCell implements TableCell { | ||
| public static final String TYPE = "raw_text"; | ||
| private final String type = TYPE; | ||
| private String text; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.slack.api.model.block; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Displays tabular data as a grid of rows and cells. | ||
| * | ||
| * <p>Each row is a list of {@link TableCell cells}; a cell may be a | ||
| * {@link RawTextTableCell} ({@code raw_text}), {@link RawNumberTableCell} | ||
| * ({@code raw_number}), or a {@link RichTextBlock} ({@code rich_text}). A table | ||
| * supports up to 100 rows and up to 20 cells per row, with optional per-column | ||
| * behavior configured via {@link TableColumnSetting column settings}.</p> | ||
| * | ||
| * @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a> | ||
| */ | ||
| @Data | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class TableBlock implements LayoutBlock { | ||
| public static final String TYPE = "table"; | ||
| private final String type = TYPE; | ||
|
|
||
| /** | ||
| * The rows of the table. Each row is a list of cells. Maximum 100 rows, with up to | ||
| * 20 cells per row. | ||
| */ | ||
| @Builder.Default | ||
| private List<List<TableCell>> rows = new ArrayList<>(); | ||
|
|
||
| /** | ||
| * Per-column behavior configuration (alignment, wrapping). Maximum 20 items. | ||
| */ | ||
| private List<TableColumnSetting> columnSettings; | ||
|
|
||
| private String blockId; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.slack.api.model.block; | ||
|
|
||
| /** | ||
| * A single cell within a {@link TableBlock} row. A cell can be one of: | ||
| * | ||
| * <ul> | ||
| * <li>{@link RawTextTableCell} ({@code raw_text})</li> | ||
| * <li>{@link RawNumberTableCell} ({@code raw_number})</li> | ||
| * <li>{@link com.slack.api.model.block.RichTextBlock RichTextBlock} ({@code rich_text})</li> | ||
| * </ul> | ||
| * | ||
| * @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a> | ||
| */ | ||
| public interface TableCell { | ||
|
|
||
| String getType(); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.slack.api.model.block; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| /** | ||
| * Describes the behavior of a single column in a {@link TableBlock}. Up to 20 column | ||
| * settings can be supplied, one per column. | ||
| * | ||
| * @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a> | ||
| */ | ||
| @Data | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class TableColumnSetting { | ||
| /** | ||
| * Horizontal text alignment for the column. One of {@code left}, {@code center}, | ||
| * or {@code right}. Defaults to {@code left}. | ||
| */ | ||
| private String align; | ||
| /** | ||
| * Whether text in the column wraps onto multiple lines. Defaults to {@code false}. | ||
| */ | ||
| private Boolean isWrapped; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package com.slack.api.util.json; | ||
|
|
||
| import com.google.gson.*; | ||
| import com.slack.api.model.block.RawNumberTableCell; | ||
| import com.slack.api.model.block.RawTextTableCell; | ||
| import com.slack.api.model.block.RichTextBlock; | ||
| import com.slack.api.model.block.TableCell; | ||
|
|
||
| import java.lang.reflect.Type; | ||
|
|
||
| /** | ||
| * Factory for deserializing the cells of a | ||
| * {@link com.slack.api.model.block.TableBlock table block}. | ||
| * | ||
| * @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a> | ||
| */ | ||
| public class GsonTableCellFactory implements JsonDeserializer<TableCell>, JsonSerializer<TableCell> { | ||
|
|
||
| private final boolean failOnUnknownProperties; | ||
|
|
||
| public GsonTableCellFactory() { | ||
| this(false); | ||
| } | ||
|
|
||
| public GsonTableCellFactory(boolean failOnUnknownProperties) { | ||
| this.failOnUnknownProperties = failOnUnknownProperties; | ||
| } | ||
|
|
||
| @Override | ||
| public TableCell deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
| throws JsonParseException { | ||
| final JsonObject jsonObject = json.getAsJsonObject(); | ||
| final JsonPrimitive prim = (JsonPrimitive) jsonObject.get("type"); | ||
| final String typeName = prim.getAsString(); | ||
| final Class<? extends TableCell> clazz = getTableCellClassInstance(typeName); | ||
| return context.deserialize(jsonObject, clazz); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonElement serialize(TableCell src, Type typeOfSrc, JsonSerializationContext context) { | ||
| return context.serialize(src); | ||
| } | ||
|
|
||
| private Class<? extends TableCell> getTableCellClassInstance(String typeName) { | ||
| switch (typeName) { | ||
| case RawTextTableCell.TYPE: | ||
| return RawTextTableCell.class; | ||
| case RawNumberTableCell.TYPE: | ||
| return RawNumberTableCell.class; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧮 question: Without testing on a personal sandbox I forget if this is stable? It's documented so no blocker here either! |
||
| case RichTextBlock.TYPE: | ||
| return RichTextBlock.class; | ||
| default: | ||
| throw new JsonParseException("Unknown table cell type: " + typeName); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👁️🗨️ question: Is this pattern more standard than perhaps reversing the order for more "direct" imports?
🪬 note: I might prefer the latter but not a blocker!