Wasm plugins' isolated Postgres role is deliberately denied on core tables, which left no sanctioned path to data_tables/data_table_rows. Adds the datatables.* capability family (get_table_by_key, get_row, list_rows, create_row, update_row_data, find_row_by_field) following the datasources pattern: interface package, ABI proto messages, guest stub, CoreServices wiring, golden round-trip coverage. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
103 lines
3.3 KiB
Go
103 lines
3.3 KiB
Go
package caps
|
|
|
|
import (
|
|
"context"
|
|
|
|
abiv1 "git.dev.alexdunmow.com/block/pluginsdk/abi/v1"
|
|
"git.dev.alexdunmow.com/block/pluginsdk/datatables"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// datatablesStub implements datatables.DataTables over datatables.* capability
|
|
// calls. Row payloads cross as JSON bytes.
|
|
type datatablesStub struct{ base }
|
|
|
|
var _ datatables.DataTables = (*datatablesStub)(nil)
|
|
|
|
func (s *datatablesStub) GetTableByKey(ctx context.Context, tableKey string) (*datatables.Table, error) {
|
|
resp := &abiv1.DatatablesGetTableByKeyResponse{}
|
|
req := &abiv1.DatatablesGetTableByKeyRequest{TableKey: tableKey}
|
|
if err := s.invoke(ctx, "get_table_by_key", req, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
return dataTableFromProto(resp.GetTable()), nil
|
|
}
|
|
|
|
func (s *datatablesStub) GetRow(ctx context.Context, rowID uuid.UUID) (*datatables.Row, error) {
|
|
resp := &abiv1.DatatablesGetRowResponse{}
|
|
req := &abiv1.DatatablesGetRowRequest{RowId: rowID.String()}
|
|
if err := s.invoke(ctx, "get_row", req, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
return dataTableRowFromProto(resp.GetRow()), nil
|
|
}
|
|
|
|
func (s *datatablesStub) ListRows(ctx context.Context, tableID uuid.UUID, limit, offset int32) ([]datatables.Row, error) {
|
|
resp := &abiv1.DatatablesListRowsResponse{}
|
|
req := &abiv1.DatatablesListRowsRequest{TableId: tableID.String(), Limit: limit, Offset: offset}
|
|
if err := s.invoke(ctx, "list_rows", req, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
rows := make([]datatables.Row, 0, len(resp.GetRows()))
|
|
for _, r := range resp.GetRows() {
|
|
if row := dataTableRowFromProto(r); row != nil {
|
|
rows = append(rows, *row)
|
|
}
|
|
}
|
|
return rows, nil
|
|
}
|
|
|
|
func (s *datatablesStub) CreateRow(ctx context.Context, tableID uuid.UUID, dataJSON []byte) (*datatables.Row, error) {
|
|
resp := &abiv1.DatatablesCreateRowResponse{}
|
|
req := &abiv1.DatatablesCreateRowRequest{TableId: tableID.String(), DataJson: dataJSON}
|
|
if err := s.invoke(ctx, "create_row", req, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
return dataTableRowFromProto(resp.GetRow()), nil
|
|
}
|
|
|
|
func (s *datatablesStub) UpdateRowData(ctx context.Context, rowID uuid.UUID, dataJSON []byte) (*datatables.Row, error) {
|
|
resp := &abiv1.DatatablesUpdateRowDataResponse{}
|
|
req := &abiv1.DatatablesUpdateRowDataRequest{RowId: rowID.String(), DataJson: dataJSON}
|
|
if err := s.invoke(ctx, "update_row_data", req, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
return dataTableRowFromProto(resp.GetRow()), nil
|
|
}
|
|
|
|
func (s *datatablesStub) FindRowByField(ctx context.Context, tableID uuid.UUID, field, value string) (*datatables.Row, error) {
|
|
resp := &abiv1.DatatablesFindRowByFieldResponse{}
|
|
req := &abiv1.DatatablesFindRowByFieldRequest{TableId: tableID.String(), Field: field, Value: value}
|
|
if err := s.invoke(ctx, "find_row_by_field", req, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
return dataTableRowFromProto(resp.GetRow()), nil
|
|
}
|
|
|
|
func dataTableFromProto(t *abiv1.DataTableInfo) *datatables.Table {
|
|
if t == nil {
|
|
return nil
|
|
}
|
|
id, _ := uuid.Parse(t.GetId())
|
|
return &datatables.Table{
|
|
ID: id,
|
|
TableKey: t.GetTableKey(),
|
|
Name: t.GetName(),
|
|
RowCount: t.GetRowCount(),
|
|
}
|
|
}
|
|
|
|
func dataTableRowFromProto(r *abiv1.DataTableRowInfo) *datatables.Row {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
id, _ := uuid.Parse(r.GetId())
|
|
tableID, _ := uuid.Parse(r.GetTableId())
|
|
return &datatables.Row{
|
|
ID: id,
|
|
TableID: tableID,
|
|
DataJSON: r.GetDataJson(),
|
|
SortOrder: r.GetSortOrder(),
|
|
}
|
|
}
|