Skip to content

Commit

Permalink
properly unmarshal response
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Oct 30, 2024
1 parent 49bb69d commit bc34bd0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 7 additions & 7 deletions controllers/jobs/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ func GetMostRecentTask(job *store.Job, r *http.Request) (*Task, error) {
return nil, err
}

var tasks []Task
err = json.Unmarshal(tasksBody, &tasks)
var tasksStruct TasksResponse
err = json.Unmarshal(tasksBody, &tasksStruct)
if err != nil {
fmt.Println(tasksBody)
return nil, err
}

if len(tasks) == 0 {
if len(tasksStruct.Data) == 0 {
return nil, fmt.Errorf("error getting tasks: not found")
}

return &tasks[0], nil
return &tasksStruct.Data[0], nil
}

func GetTaskByUPID(upid string, r *http.Request) (*Task, error) {
Expand Down Expand Up @@ -93,12 +93,12 @@ func GetTaskByUPID(upid string, r *http.Request) (*Task, error) {
return nil, err
}

var task Task
err = json.Unmarshal(tasksBody, &task)
var taskStruct TaskResponse
err = json.Unmarshal(tasksBody, &taskStruct)
if err != nil {
fmt.Println(tasksBody)
return nil, err
}

return &task, nil
return &taskStruct.Data, nil
}
10 changes: 10 additions & 0 deletions controllers/jobs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ type JobRunResponse struct {
Success bool `json:"success"`
}

type TasksResponse struct {
Data []Task `json:"data"`
Total int `json:"total"`
}

type TaskResponse struct {
Data Task `json:"data"`
Total int `json:"total"`
}

type Task struct {
Node string `json:"node"`
PID int `json:"pid"`
Expand Down

0 comments on commit bc34bd0

Please sign in to comment.