fix(receiver): return Payload as JSON object instead of string
All checks were successful
ci / build (push) Successful in 1m49s
All checks were successful
ci / build (push) Successful in 1m49s
Changed the API response to embed Payload as a JSON object using json.RawMessage instead of returning it as a JSON-encoded string inside the JSON response. Added MetricResponse type with ToResponse converter method. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
0af8c28bc2
commit
eb01c1c842
3 changed files with 35 additions and 3 deletions
|
|
@ -74,8 +74,14 @@ func (h *Handler) handleGetByWorkflowJob(w http.ResponseWriter, r *http.Request)
|
|||
return
|
||||
}
|
||||
|
||||
// Convert to response type with Payload as JSON object
|
||||
response := make([]MetricResponse, len(metrics))
|
||||
for i, m := range metrics {
|
||||
response[i] = m.ToResponse()
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(metrics)
|
||||
_ = json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (h *Handler) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -125,7 +125,7 @@ func TestHandler_GetByWorkflowJob(t *testing.T) {
|
|||
t.Errorf("status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
var metrics []Metric
|
||||
var metrics []MetricResponse
|
||||
if err := json.NewDecoder(rec.Body).Decode(&metrics); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
|
@ -149,7 +149,7 @@ func TestHandler_GetByWorkflowJob_NotFound(t *testing.T) {
|
|||
t.Errorf("status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
var metrics []Metric
|
||||
var metrics []MetricResponse
|
||||
if err := json.NewDecoder(rec.Body).Decode(&metrics); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,32 @@ type Metric struct {
|
|||
Payload string `gorm:"type:text;not null"` // JSON-encoded RunSummary
|
||||
}
|
||||
|
||||
// MetricResponse is the API response type with Payload as embedded JSON object
|
||||
type MetricResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Organization string `json:"organization"`
|
||||
Repository string `json:"repository"`
|
||||
Workflow string `json:"workflow"`
|
||||
Job string `json:"job"`
|
||||
RunID string `json:"run_id"`
|
||||
ReceivedAt time.Time `json:"received_at"`
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
}
|
||||
|
||||
// ToResponse converts a Metric to a MetricResponse with Payload as JSON object
|
||||
func (m *Metric) ToResponse() MetricResponse {
|
||||
return MetricResponse{
|
||||
ID: m.ID,
|
||||
Organization: m.Organization,
|
||||
Repository: m.Repository,
|
||||
Workflow: m.Workflow,
|
||||
Job: m.Job,
|
||||
RunID: m.RunID,
|
||||
ReceivedAt: m.ReceivedAt,
|
||||
Payload: json.RawMessage(m.Payload),
|
||||
}
|
||||
}
|
||||
|
||||
// Store handles SQLite storage for metrics using GORM
|
||||
type Store struct {
|
||||
db *gorm.DB
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue