|
4 | 4 | _ "embed"
|
5 | 5 | "fmt"
|
6 | 6 | "regexp"
|
| 7 | + "strconv" |
7 | 8 | "strings"
|
8 | 9 |
|
9 | 10 | "gopkg.in/yaml.v3"
|
@@ -40,6 +41,67 @@ func buildResultMessage(key, message string, value interface{}) string {
|
40 | 41 | return builder.String()
|
41 | 42 | }
|
42 | 43 |
|
| 44 | +// Simple JSONPath builder for basic path construction |
| 45 | +type JSONPathBuilder struct { |
| 46 | + segments []string |
| 47 | +} |
| 48 | + |
| 49 | +// GetJSONPathBuilder returns a simple JSONPath builder |
| 50 | +func GetJSONPathBuilder() *JSONPathBuilder { |
| 51 | + return &JSONPathBuilder{ |
| 52 | + segments: make([]string, 0, 10), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Reset clears the builder |
| 57 | +func (b *JSONPathBuilder) Reset() *JSONPathBuilder { |
| 58 | + b.segments = b.segments[:0] |
| 59 | + return b |
| 60 | +} |
| 61 | + |
| 62 | +// Root starts a JSONPath |
| 63 | +func (b *JSONPathBuilder) Root() *JSONPathBuilder { |
| 64 | + b.segments = append(b.segments, "$") |
| 65 | + return b |
| 66 | +} |
| 67 | + |
| 68 | +// Field adds a field to the path |
| 69 | +func (b *JSONPathBuilder) Field(field string) *JSONPathBuilder { |
| 70 | + b.segments = append(b.segments, ".", field) |
| 71 | + return b |
| 72 | +} |
| 73 | + |
| 74 | +// Key adds a key to the path |
| 75 | +func (b *JSONPathBuilder) Key(key string) *JSONPathBuilder { |
| 76 | + b.segments = append(b.segments, "['", key, "']") |
| 77 | + return b |
| 78 | +} |
| 79 | + |
| 80 | +// Index adds an index to the path |
| 81 | +func (b *JSONPathBuilder) Index(index int) *JSONPathBuilder { |
| 82 | + b.segments = append(b.segments, "[", strconv.Itoa(index), "]") |
| 83 | + return b |
| 84 | +} |
| 85 | + |
| 86 | +// Build constructs the JSONPath |
| 87 | +func (b *JSONPathBuilder) Build() string { |
| 88 | + var builder strings.Builder |
| 89 | + for _, segment := range b.segments { |
| 90 | + builder.WriteString(segment) |
| 91 | + } |
| 92 | + return builder.String() |
| 93 | +} |
| 94 | + |
| 95 | +// BuildOperationFieldPath builds a path for operation fields |
| 96 | +func BuildOperationFieldPath(path, method, field string) string { |
| 97 | + return fmt.Sprintf("$.paths['%s'].%s.%s", path, method, field) |
| 98 | +} |
| 99 | + |
| 100 | +// BuildResponsePath builds a path for responses |
| 101 | +func BuildResponsePath(path, method, code string) string { |
| 102 | + return fmt.Sprintf("$.paths['%s'].%s.responses['%s']", path, method, code) |
| 103 | +} |
| 104 | + |
43 | 105 | // buildResultMessageWithDescription efficiently builds a result message with description
|
44 | 106 | func buildResultMessageWithDescription(desc, key, message string, value interface{}) string {
|
45 | 107 | var builder strings.Builder
|
|
0 commit comments