synthetics.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package cat
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. )
  7. // SyntheticsHeader represents a decoded Synthetics header.
  8. type SyntheticsHeader struct {
  9. Version int
  10. AccountID int
  11. ResourceID string
  12. JobID string
  13. MonitorID string
  14. }
  15. var (
  16. errInvalidSyntheticsJSON = errors.New("invalid synthetics JSON")
  17. errInvalidSyntheticsVersion = errors.New("version is not a float64")
  18. errInvalidSyntheticsAccountID = errors.New("account ID is not a float64")
  19. errInvalidSyntheticsResourceID = errors.New("synthetics resource ID is not a string")
  20. errInvalidSyntheticsJobID = errors.New("synthetics job ID is not a string")
  21. errInvalidSyntheticsMonitorID = errors.New("synthetics monitor ID is not a string")
  22. )
  23. type errUnexpectedSyntheticsVersion int
  24. func (e errUnexpectedSyntheticsVersion) Error() string {
  25. return fmt.Sprintf("unexpected synthetics header version: %d", e)
  26. }
  27. // UnmarshalJSON unmarshalls a SyntheticsHeader from raw JSON.
  28. func (s *SyntheticsHeader) UnmarshalJSON(data []byte) error {
  29. var ok bool
  30. var v interface{}
  31. if err := json.Unmarshal(data, &v); err != nil {
  32. return err
  33. }
  34. arr, ok := v.([]interface{})
  35. if !ok {
  36. return errInvalidSyntheticsJSON
  37. }
  38. if len(arr) != 5 {
  39. return errUnexpectedArraySize{
  40. label: "unexpected number of application data elements",
  41. expected: 5,
  42. actual: len(arr),
  43. }
  44. }
  45. version, ok := arr[0].(float64)
  46. if !ok {
  47. return errInvalidSyntheticsVersion
  48. }
  49. s.Version = int(version)
  50. if s.Version != 1 {
  51. return errUnexpectedSyntheticsVersion(s.Version)
  52. }
  53. accountID, ok := arr[1].(float64)
  54. if !ok {
  55. return errInvalidSyntheticsAccountID
  56. }
  57. s.AccountID = int(accountID)
  58. if s.ResourceID, ok = arr[2].(string); !ok {
  59. return errInvalidSyntheticsResourceID
  60. }
  61. if s.JobID, ok = arr[3].(string); !ok {
  62. return errInvalidSyntheticsJobID
  63. }
  64. if s.MonitorID, ok = arr[4].(string); !ok {
  65. return errInvalidSyntheticsMonitorID
  66. }
  67. return nil
  68. }