4
0

jsonpath.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package webhooks
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/theory/jsonpath"
  7. )
  8. type JSONMatcher struct {
  9. payload any
  10. }
  11. func NewJSONMatcher(payload []byte) (*JSONMatcher, error) {
  12. var data any
  13. if err := json.Unmarshal(payload, &data); err != nil {
  14. return nil, err
  15. }
  16. return &JSONMatcher{payload: data}, nil
  17. }
  18. func (m *JSONMatcher) MatchPath(pathExpr string, expectedValue string) (bool, error) {
  19. value, err := m.selectValue(pathExpr)
  20. if err != nil {
  21. return false, err
  22. }
  23. // For string values, compare directly without marshaling
  24. if strValue, ok := value.(string); ok {
  25. return strValue == expectedValue, nil
  26. }
  27. // For non-string values, marshal to JSON for consistent string representation
  28. jsonBytes, err := json.Marshal(value)
  29. if err != nil {
  30. return false, fmt.Errorf("failed to marshal extracted value: %w", err)
  31. }
  32. valueStr := string(jsonBytes)
  33. return valueStr == expectedValue, nil
  34. }
  35. func (m *JSONMatcher) ExtractValue(pathExpr string) (string, error) {
  36. value, err := m.selectValue(pathExpr)
  37. if err != nil {
  38. return "", err
  39. }
  40. // For string values, return directly without marshaling to avoid adding JSON quotes
  41. if strValue, ok := value.(string); ok {
  42. return strValue, nil
  43. }
  44. // For non-string values, marshal to JSON for consistent string representation
  45. jsonBytes, err := json.Marshal(value)
  46. if err != nil {
  47. return "", fmt.Errorf("failed to marshal extracted value: %w", err)
  48. }
  49. return string(jsonBytes), nil
  50. }
  51. // selectValue returns the single value for a path that can only match one node,
  52. // and a list for a path that can match several (wildcards, descendants). A path
  53. // that matches nothing is an error, which callers use to tell that it is absent.
  54. func (m *JSONMatcher) selectValue(pathExpr string) (any, error) {
  55. path, err := jsonpath.Parse(withRootSelector(pathExpr))
  56. if err != nil {
  57. return nil, err
  58. }
  59. nodes := path.Select(m.payload)
  60. if len(nodes) == 0 {
  61. return nil, fmt.Errorf("no value found at JSONPath %q", pathExpr)
  62. }
  63. if path.Query().Singular() != nil {
  64. return nodes[0], nil
  65. }
  66. return []any(nodes), nil
  67. }
  68. // Earlier versions of OliveTin accepted paths without the leading "$", such as
  69. // "repository.name", so keep accepting them.
  70. func withRootSelector(pathExpr string) string {
  71. if strings.HasPrefix(pathExpr, "$") {
  72. return pathExpr
  73. }
  74. return "$." + pathExpr
  75. }
  76. func (m *JSONMatcher) GetPayload() any {
  77. return m.payload
  78. }