UnmarshalJSON implements json.Unmarshaler.
(data []byte)
| 514 | |
| 515 | // UnmarshalJSON implements json.Unmarshaler. |
| 516 | func (s *PrometheusData) UnmarshalJSON(data []byte) error { |
| 517 | var queryData struct { |
| 518 | ResultType string `json:"resultType"` |
| 519 | Stats *PrometheusResponseStats `json:"stats,omitempty"` |
| 520 | } |
| 521 | |
| 522 | if err := json.Unmarshal(data, &queryData); err != nil { |
| 523 | return err |
| 524 | } |
| 525 | s.ResultType = queryData.ResultType |
| 526 | s.Stats = queryData.Stats |
| 527 | switch s.ResultType { |
| 528 | case model.ValVector.String(): |
| 529 | var result struct { |
| 530 | Samples []Sample `json:"result"` |
| 531 | } |
| 532 | if err := json.Unmarshal(data, &result); err != nil { |
| 533 | return err |
| 534 | } |
| 535 | s.Result = PrometheusQueryResult{ |
| 536 | Result: &PrometheusQueryResult_Vector{Vector: &Vector{ |
| 537 | Samples: result.Samples, |
| 538 | }}, |
| 539 | } |
| 540 | case model.ValMatrix.String(): |
| 541 | var result struct { |
| 542 | SampleStreams []SampleStream `json:"result"` |
| 543 | } |
| 544 | if err := json.Unmarshal(data, &result); err != nil { |
| 545 | return err |
| 546 | } |
| 547 | s.Result = PrometheusQueryResult{ |
| 548 | Result: &PrometheusQueryResult_Matrix{Matrix: &Matrix{ |
| 549 | SampleStreams: result.SampleStreams, |
| 550 | }}, |
| 551 | } |
| 552 | default: |
| 553 | s.Result = PrometheusQueryResult{ |
| 554 | Result: &PrometheusQueryResult_RawBytes{data}, |
| 555 | } |
| 556 | } |
| 557 | return nil |
| 558 | } |
| 559 | |
| 560 | // MarshalJSON implements json.Marshaler. |
| 561 | func (s *PrometheusData) MarshalJSON() ([]byte, error) { |