-
Notifications
You must be signed in to change notification settings - Fork 62
Add Table and DataTable block support #455
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
Changes from all commits
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,97 @@ | ||
| package com.hubspot.slack.client.models.blocks; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.hubspot.immutables.style.HubSpotStyle; | ||
| import com.hubspot.slack.client.models.blocks.table.DataTableCell; | ||
| import com.hubspot.slack.client.models.blocks.table.RawTextTableCell; | ||
| import java.util.Optional; | ||
| import org.immutables.value.Value; | ||
| import org.immutables.value.Value.Check; | ||
| import org.immutables.value.Value.Immutable; | ||
|
|
||
| @Immutable | ||
| @HubSpotStyle | ||
| @JsonNaming(SnakeCaseStrategy.class) | ||
| @JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
| public interface DataTableIF extends Block { | ||
| String TYPE = "data_table"; | ||
|
|
||
| @Override | ||
| @Value.Derived | ||
| default String getType() { | ||
| return TYPE; | ||
| } | ||
|
|
||
| @Value.Parameter(order = 1) | ||
| String getCaption(); | ||
|
|
||
| @Value.Parameter(order = 2) | ||
| ImmutableList<ImmutableList<DataTableCell>> getRows(); | ||
|
|
||
| @JsonProperty("page_size") | ||
| Optional<Integer> getPageSize(); | ||
|
|
||
| @JsonProperty("row_header_column_index") | ||
| Optional<Integer> getRowHeaderColumnIndex(); | ||
|
|
||
| @Check | ||
| default void check() { | ||
| Preconditions.checkState( | ||
| getRows().size() >= BlockElementLengthLimits.MIN_DATA_TABLE_ROWS.getLimit(), | ||
| "A data_table block must have at least %s rows (1 header + 1 body)", | ||
| BlockElementLengthLimits.MIN_DATA_TABLE_ROWS.getLimit() | ||
| ); | ||
| Preconditions.checkState( | ||
| getRows().size() <= BlockElementLengthLimits.MAX_TABLE_ROWS.getLimit(), | ||
| "A data_table block cannot have more than %s rows", | ||
| BlockElementLengthLimits.MAX_TABLE_ROWS.getLimit() | ||
| ); | ||
| getRows() | ||
| .forEach(row -> | ||
| Preconditions.checkState( | ||
| row.size() <= BlockElementLengthLimits.MAX_TABLE_COLUMNS.getLimit(), | ||
| "A data_table row cannot have more than %s columns", | ||
| BlockElementLengthLimits.MAX_TABLE_COLUMNS.getLimit() | ||
| ) | ||
| ); | ||
| int columnCount = getRows().get(0).size(); | ||
| getRows() | ||
| .forEach(row -> | ||
| Preconditions.checkState( | ||
| row.size() == columnCount, | ||
| "All rows in a data_table block must have the same number of columns" | ||
| ) | ||
| ); | ||
| getRows() | ||
| .get(0) | ||
| .forEach(cell -> | ||
| Preconditions.checkState( | ||
| cell instanceof RawTextTableCell, | ||
| "Header row cells must be of type raw_text" | ||
| ) | ||
| ); | ||
| getPageSize() | ||
| .ifPresent(pageSize -> | ||
| Preconditions.checkState( | ||
| pageSize >= 1 && | ||
| pageSize <= BlockElementLengthLimits.MAX_DATA_TABLE_PAGE_SIZE.getLimit(), | ||
| "page_size must be between 1 and %s", | ||
| BlockElementLengthLimits.MAX_DATA_TABLE_PAGE_SIZE.getLimit() | ||
| ) | ||
| ); | ||
| getBlockId() | ||
| .ifPresent(blockId -> | ||
| Preconditions.checkState( | ||
| blockId.length() <= | ||
| BlockElementLengthLimits.MAX_TABLE_BLOCK_ID_LENGTH.getLimit(), | ||
| "block_id cannot exceed %s characters", | ||
| BlockElementLengthLimits.MAX_TABLE_BLOCK_ID_LENGTH.getLimit() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.hubspot.slack.client.models.blocks; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.hubspot.immutables.style.HubSpotStyle; | ||
| import com.hubspot.slack.client.models.blocks.table.TableCell; | ||
| import com.hubspot.slack.client.models.blocks.table.TableColumnSetting; | ||
| import org.immutables.value.Value; | ||
| import org.immutables.value.Value.Check; | ||
| import org.immutables.value.Value.Immutable; | ||
|
|
||
| @Immutable | ||
| @HubSpotStyle | ||
| @JsonNaming(SnakeCaseStrategy.class) | ||
| public interface TableIF extends Block { | ||
| String TYPE = "table"; | ||
|
|
||
| @Override | ||
| @Value.Derived | ||
| default String getType() { | ||
| return TYPE; | ||
| } | ||
|
|
||
| @Value.Parameter | ||
| ImmutableList<ImmutableList<TableCell>> getRows(); | ||
|
|
||
| ImmutableList<TableColumnSetting> getColumnSettings(); | ||
|
|
||
| @Check | ||
| default void check() { | ||
| Preconditions.checkState( | ||
| getRows().size() <= BlockElementLengthLimits.MAX_TABLE_ROWS.getLimit(), | ||
| "A table block cannot have more than %s rows", | ||
| BlockElementLengthLimits.MAX_TABLE_ROWS.getLimit() | ||
| ); | ||
| getRows() | ||
| .forEach(row -> | ||
| Preconditions.checkState( | ||
| row.size() <= BlockElementLengthLimits.MAX_TABLE_COLUMNS.getLimit(), | ||
| "A table row cannot have more than %s cells", | ||
| BlockElementLengthLimits.MAX_TABLE_COLUMNS.getLimit() | ||
| ) | ||
| ); | ||
| Preconditions.checkState( | ||
| getColumnSettings().size() <= BlockElementLengthLimits.MAX_TABLE_COLUMNS.getLimit(), | ||
| "A table block cannot have more than %s column settings", | ||
| BlockElementLengthLimits.MAX_TABLE_COLUMNS.getLimit() | ||
| ); | ||
| getBlockId() | ||
| .ifPresent(blockId -> | ||
| Preconditions.checkState( | ||
| blockId.length() <= | ||
| BlockElementLengthLimits.MAX_TABLE_BLOCK_ID_LENGTH.getLimit(), | ||
| "block_id cannot exceed %s characters", | ||
| BlockElementLengthLimits.MAX_TABLE_BLOCK_ID_LENGTH.getLimit() | ||
| ) | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
| import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
|
||
| @JsonTypeInfo( | ||
| use = JsonTypeInfo.Id.NAME, | ||
| property = "type", | ||
| defaultImpl = UnknownTableCell.class | ||
| ) | ||
| @JsonSubTypes( | ||
| { | ||
| @JsonSubTypes.Type(value = RawTextTableCell.class, name = RawTextTableCell.TYPE), | ||
| @JsonSubTypes.Type(value = RawNumberTableCell.class, name = RawNumberTableCell.TYPE), | ||
| @JsonSubTypes.Type(value = RichTextTableCell.class, name = RichTextTableCell.TYPE), | ||
| } | ||
| ) | ||
| @JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
| public interface DataTableCell extends TableCell {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import com.hubspot.immutables.style.HubSpotStyle; | ||
| import org.immutables.value.Value; | ||
| import org.immutables.value.Value.Immutable; | ||
|
|
||
| @Immutable | ||
| @HubSpotStyle | ||
| @JsonNaming(SnakeCaseStrategy.class) | ||
| public interface RawNumberTableCellIF extends DataTableCell { | ||
| String TYPE = "raw_number"; | ||
|
|
||
| @Override | ||
| @Value.Derived | ||
| default String getType() { | ||
| return TYPE; | ||
| } | ||
|
|
||
| @Value.Parameter(order = 1) | ||
| double getValue(); | ||
|
|
||
| @Value.Parameter(order = 2) | ||
| String getText(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import com.hubspot.immutables.style.HubSpotStyle; | ||
| import org.immutables.value.Value; | ||
| import org.immutables.value.Value.Immutable; | ||
|
|
||
| @Immutable | ||
| @HubSpotStyle | ||
| @JsonNaming(SnakeCaseStrategy.class) | ||
| public interface RawTextTableCellIF extends DataTableCell { | ||
| String TYPE = "raw_text"; | ||
|
|
||
| @Override | ||
| @Value.Derived | ||
| default String getType() { | ||
| return TYPE; | ||
| } | ||
|
|
||
| @Value.Parameter | ||
| String getText(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.hubspot.immutables.style.HubSpotStyle; | ||
| import com.hubspot.slack.client.models.blocks.elements.richtextelements.RichTextObject; | ||
| import org.immutables.value.Value; | ||
| import org.immutables.value.Value.Immutable; | ||
|
|
||
| @Immutable | ||
| @HubSpotStyle | ||
| @JsonNaming(SnakeCaseStrategy.class) | ||
| public interface RichTextTableCellIF extends DataTableCell { | ||
| String TYPE = "rich_text"; | ||
|
|
||
| @Override | ||
| @Value.Derived | ||
| default String getType() { | ||
| return TYPE; | ||
| } | ||
|
|
||
| @JsonProperty("elements") | ||
| ImmutableList<RichTextObject> getElements(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
| import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
|
|
||
| @JsonTypeInfo( | ||
| use = JsonTypeInfo.Id.NAME, | ||
| property = "type", | ||
| defaultImpl = UnknownTableCell.class | ||
| ) | ||
| @JsonSubTypes( | ||
| { | ||
| @JsonSubTypes.Type(value = RawTextTableCell.class, name = RawTextTableCell.TYPE), | ||
| @JsonSubTypes.Type(value = RichTextTableCell.class, name = RichTextTableCell.TYPE), | ||
|
Contributor
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. Could you please check if we shouldn't include RawNumberTableCellIF here as well
Contributor
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. In documentation it says that Tables supports RawNumberTableCell, but in practice / what's actually implemented it doesn't support it. Not sure where the right place to raise that issue is to Slack, but I've omitted RawNumberTableCell here because of that. |
||
| } | ||
| ) | ||
| @JsonInclude(JsonInclude.Include.NON_EMPTY) | ||
| public interface TableCell { | ||
| String getType(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
|
|
||
| public enum TableColumnAlign { | ||
| LEFT("left"), | ||
| CENTER("center"), | ||
| RIGHT("right"); | ||
|
|
||
| private final String value; | ||
|
|
||
| TableColumnAlign(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @JsonValue | ||
| public String getValue() { | ||
| return value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonInclude; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import com.hubspot.immutables.style.HubSpotStyle; | ||
| import java.util.Optional; | ||
| import org.immutables.value.Value.Immutable; | ||
|
|
||
| @Immutable | ||
| @HubSpotStyle | ||
| @JsonNaming(SnakeCaseStrategy.class) | ||
| @JsonInclude(JsonInclude.Include.NON_ABSENT) | ||
| public interface TableColumnSettingIF { | ||
| Optional<TableColumnAlign> getAlign(); | ||
|
|
||
| @JsonProperty("is_wrapped") | ||
| Optional<Boolean> isWrapped(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.hubspot.slack.client.models.blocks.table; | ||
|
|
||
| public class UnknownTableCell implements DataTableCell { | ||
|
|
||
| public static final String TYPE = "unknown"; | ||
|
|
||
| protected UnknownTableCell() {} | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return TYPE; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.