transformTransaction prepares a transaction for API response by ensuring that metadata fields are properly represented as first-class fields. This maintains backward compatibility while keeping responses clean.
(txn *model.Transaction)
| 37 | // that metadata fields are properly represented as first-class fields. |
| 38 | // This maintains backward compatibility while keeping responses clean. |
| 39 | func transformTransaction(txn *model.Transaction) *model.Transaction { |
| 40 | if txn == nil { |
| 41 | return &model.Transaction{} |
| 42 | } |
| 43 | |
| 44 | // Deep copy instead of shallow copy |
| 45 | result := *txn |
| 46 | |
| 47 | // Deep copy the slices to avoid race conditions |
| 48 | if txn.Sources != nil { |
| 49 | result.Sources = make([]model.Distribution, len(txn.Sources)) |
| 50 | copy(result.Sources, txn.Sources) |
| 51 | } |
| 52 | |
| 53 | if txn.Destinations != nil { |
| 54 | result.Destinations = make([]model.Distribution, len(txn.Destinations)) |
| 55 | copy(result.Destinations, txn.Destinations) |
| 56 | } |
| 57 | |
| 58 | // Deep copy the metadata map |
| 59 | if txn.MetaData != nil { |
| 60 | result.MetaData = make(map[string]interface{}) |
| 61 | for k, v := range txn.MetaData { |
| 62 | result.MetaData[k] = v |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Check for inflight flag in metadata and move it to the main field |
| 67 | if result.MetaData != nil { |
| 68 | if inflightVal, exists := result.MetaData["inflight"]; exists { |
| 69 | if inflight, ok := inflightVal.(bool); ok && inflight { |
| 70 | result.Inflight = true |
| 71 | // Remove from metadata to avoid duplication |
| 72 | delete(result.MetaData, "inflight") |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // If metadata is now empty, set it to nil |
| 77 | if len(result.MetaData) == 0 { |
| 78 | result.MetaData = nil |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return &result |
| 83 | } |
| 84 | |
| 85 | func handleRecordTransactionValidationError(c *gin.Context, err error) { |
| 86 | var validationErrors validation.Errors |
no outgoing calls
no test coverage detected