Refactor response preparation for flat transactions (#5187)

* Refactor response preparation for flat transactions

This will allow to share code across endpoints

changelog_begin
changelog_end

* Address https://github.com/digital-asset/daml/pull/5187#discussion_r397996322
This commit is contained in:
Stefano Baghino 2020-03-25 18:53:25 +01:00 committed by GitHub
parent ef6c09b1bd
commit 1a229ebb7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 15 deletions

View File

@ -18,7 +18,8 @@ import com.digitalasset.ledger.api.v1.transaction.{
}
import com.digitalasset.ledger.api.v1.transaction_service.{
GetFlatTransactionResponse,
GetTransactionResponse
GetTransactionResponse,
GetTransactionsResponse,
}
import com.digitalasset.ledger.api.v1.value.Identifier
import com.digitalasset.platform.ApiOffset
@ -59,29 +60,34 @@ private[events] trait EventsTable {
private def instantToTimestamp(t: Instant): Timestamp =
Timestamp(seconds = t.getEpochSecond, nanos = t.getNano)
def toFlatTransaction(events: List[Entry[Event]]): Option[GetFlatTransactionResponse] = {
private def flatTransaction[R](makeResponse: ApiTransaction => R)(
events: Seq[Entry[Event]],
): Option[R] =
events.headOption.flatMap { first =>
val flatEvents =
TransactionConversion.removeTransient(events.iterator.map(_.event).toVector)
if (flatEvents.nonEmpty || first.commandId.nonEmpty)
Some(
GetFlatTransactionResponse(
transaction = Some(
ApiTransaction(
transactionId = first.transactionId,
commandId = first.commandId,
effectiveAt = Some(instantToTimestamp(first.ledgerEffectiveTime)),
workflowId = first.workflowId,
offset = ApiOffset.toApiString(first.eventOffset),
events = flatEvents,
)
makeResponse(
ApiTransaction(
transactionId = first.transactionId,
commandId = first.commandId,
effectiveAt = Some(instantToTimestamp(first.ledgerEffectiveTime)),
workflowId = first.workflowId,
offset = ApiOffset.toApiString(first.eventOffset),
events = flatEvents,
)
)
)
else None
}
}
def toGetTransactionsResponse(events: Vector[Entry[Event]]): Seq[GetTransactionsResponse] =
flatTransaction(tx => GetTransactionsResponse(Seq(tx)))(events).toList
def toGetFlatTransactionResponse(
events: List[Entry[Event]]): Option[GetFlatTransactionResponse] =
flatTransaction(tx => GetFlatTransactionResponse(Some(tx)))(events)
def toTransactionTree(events: List[Entry[TreeEvent]]): Option[GetTransactionResponse] =
events.headOption.map(

View File

@ -32,8 +32,9 @@ private[dao] object TransactionsReader {
) { implicit connection =>
query.as(EventsTable.flatEventParser.*)
}
.map(EventsTable.Entry.toFlatTransaction)(executionContext)
.map(EventsTable.Entry.toGetFlatTransactionResponse)(executionContext)
}
override def lookupTransactionTreeById(
transactionId: TransactionId,
requestingParties: Set[Party],