-
Notifications
You must be signed in to change notification settings - Fork 33
Feature/2237 add column name #2246
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 2 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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ class Row2 implements JsonSerializable { | |
| private ?string $lastEditBy = null; | ||
| private ?string $lastEditAt = null; | ||
| private ?array $data = []; | ||
| private array $cellMeta = []; | ||
| private array $changedColumnIds = []; // collect column ids that have changed after $loaded = true | ||
|
|
||
| private bool $loaded = false; // set to true if model is loaded, after that changed column ids will be collected | ||
|
|
@@ -133,6 +134,16 @@ public function filterDataByColumns(array $columns): array { | |
| return $this->data; | ||
| } | ||
|
|
||
| /** | ||
| * add response-only metadata for a specific column | ||
| */ | ||
| public function addCellMeta(int $columnId, array $meta): void { | ||
| if (!isset($this->cellMeta[$columnId])) { | ||
| $this->cellMeta[$columnId] = []; | ||
| } | ||
| $this->cellMeta[$columnId] = array_merge($this->cellMeta[$columnId], $meta); | ||
| } | ||
|
|
||
| /** | ||
| * @psalm-return TablesRow | ||
| */ | ||
|
|
@@ -148,6 +159,31 @@ public function jsonSerialize(): array { | |
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * return merged response-only metadata into the data cells. | ||
| */ | ||
|
||
| public function toResponseArray(): array { | ||
| $data = []; | ||
| foreach ($this->data as $cell) { | ||
| $colId = $cell['columnId']; | ||
| $merged = $cell; | ||
| if (isset($this->cellMeta[$colId])) { | ||
| $merged = array_merge($merged, $this->cellMeta[$colId]); | ||
| } | ||
| $data[] = $merged; | ||
| } | ||
|
|
||
| return [ | ||
| 'id' => $this->id, | ||
| 'tableId' => $this->tableId, | ||
| 'createdBy' => $this->createdBy, | ||
| 'createdAt' => $this->createdAt, | ||
| 'lastEditBy' => $this->lastEditBy, | ||
| 'lastEditAt' => $this->lastEditAt, | ||
| 'data' => $data, | ||
| ]; | ||
| } | ||
|
|
||
| public function toPublicRow(?array $previousValues = null): Row { | ||
| return new Row( | ||
| tableId: $this->tableId, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -208,6 +208,28 @@ public function create(?int $tableId, ?int $viewId, RowDataInput|array $data): R | |||||
| throw new InternalError('Cannot create row without table or view in context'); | ||||||
| } | ||||||
|
|
||||||
| $fullRowData = []; | ||||||
| $columnNames = []; | ||||||
|
|
||||||
| foreach ($columns as $c) { | ||||||
|
||||||
| foreach ($columns as $c) { | |
| foreach ($columns as $column) { |
Please don't save bytes where it is not necessary :)
Outdated
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.
same
Outdated
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.
can we generate it in Row2 instead? data is populated there. Having all logic in Row2 would capsulate it there and not leak logic to other classes around.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OCA\Tables\Tests\Unit\Db; | ||
|
|
||
| use OCA\Tables\Db\Row2; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class Row2Test extends TestCase { | ||
| public function testToResponseArrayMergesMetaButJsonSerializeDoesNot(): void { | ||
| $row = new Row2(); | ||
| $row->setTableId(1); | ||
|
|
||
| $row->setData([ | ||
| ['columnId' => 57, 'value' => 'foo'], | ||
| ['columnId' => 58, 'value' => 'bar'], | ||
| ]); | ||
|
|
||
| $json = $row->jsonSerialize(); | ||
| $this->assertArrayNotHasKey('columnName', $json['data'][0]); | ||
|
|
||
| $row->addCellMeta(57, ['columnName' => 'Title 57']); | ||
|
|
||
| $resp = $row->toResponseArray(); | ||
| $this->assertArrayHasKey('columnName', $resp['data'][0]); | ||
| $this->assertSame('Title 57', $resp['data'][0]['columnName']); | ||
|
|
||
| $json2 = $row->jsonSerialize(); | ||
| $this->assertArrayNotHasKey('columnName', $json2['data'][0]); | ||
| } | ||
| } |
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.
Should need to be added to a
VIRTUAL_PROPERTIESconst, otherwise the Mapper will complain. Have a look at https://github.com/nextcloud/tables/blob/main/lib/Db/Column.php#L161C2-L161C114 for comparison.Also, the name is too broad and not specific (same problem with
dataalready…), but right now I am also missing a better suggestion.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.
ill rename it cellMetadata as the array holds metadata for each cell.