Skip to content

Commit 9fef1d3

Browse files
committed
helper functions.
1 parent c42e96a commit 9fef1d3

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

model/utils.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
_ "embed"
55
"fmt"
66
"regexp"
7+
"strconv"
78
"strings"
89

910
"gopkg.in/yaml.v3"
@@ -40,6 +41,67 @@ func buildResultMessage(key, message string, value interface{}) string {
4041
return builder.String()
4142
}
4243

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+
43105
// buildResultMessageWithDescription efficiently builds a result message with description
44106
func buildResultMessageWithDescription(desc, key, message string, value interface{}) string {
45107
var builder strings.Builder

0 commit comments

Comments
 (0)