Skip to content

Commit 297f241

Browse files
fix: improved typing for RequestBody.Payload
1 parent ee8de9f commit 297f241

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

arazzo/requestbody.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
type RequestBody struct {
1717
// ContentType is the content type of the request body
1818
ContentType *string
19-
// Payload is a static value or value containing expressions that will be used to populate the request body
20-
Payload Value
19+
// Payload is a static value, an expression or a value containing inline expressions that will be used to populate the request body
20+
Payload ValueOrExpression
2121
// Replacements is a list of expressions that will be used to populate the request body in addition to any in the Payload field
2222
Replacements []*PayloadReplacement
2323
// Extensions is a list of extensions to apply to the request body object

yml/walk.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package yml
2+
3+
import (
4+
"context"
5+
6+
"github.com/speakeasy-api/openapi/errors"
7+
"gopkg.in/yaml.v3"
8+
)
9+
10+
const (
11+
// ErrTerminate is a sentinel error that can be returned from a Walk function to terminate the walk.
12+
ErrTerminate = errors.Error("terminate")
13+
)
14+
15+
// VisitFunc represents a function that will be called for each node in the node structure.
16+
// The functions receives the current node, any parent nodes, and the root node.
17+
type VisitFunc func(ctx context.Context, node, parent *yaml.Node, root *yaml.Node) error
18+
19+
// Walk will walk the yaml node structure and call the provided VisitFunc for each node in the document.
20+
// TODO should key/index be passed for nodes that are children of maps/sequences?
21+
func Walk(ctx context.Context, node *yaml.Node, visit VisitFunc) error {
22+
err := walkNode(ctx, node, nil, node, visit)
23+
if err != nil {
24+
if errors.Is(err, ErrTerminate) {
25+
return nil
26+
}
27+
return err
28+
}
29+
30+
return nil
31+
}
32+
33+
func walkNode(ctx context.Context, node *yaml.Node, parent *yaml.Node, root *yaml.Node, visit VisitFunc) error {
34+
if node == nil {
35+
return nil
36+
}
37+
38+
if err := visit(ctx, node, parent, root); err != nil {
39+
return err
40+
}
41+
42+
switch node.Kind {
43+
case yaml.DocumentNode:
44+
return walkDocumentNode(ctx, node, root, visit)
45+
case yaml.MappingNode:
46+
return walkMappingNode(ctx, node, root, visit)
47+
case yaml.SequenceNode:
48+
return walkSequenceNode(ctx, node, root, visit)
49+
case yaml.AliasNode:
50+
return walkAliasNode(ctx, node, root, visit)
51+
}
52+
53+
return nil
54+
}
55+
56+
func walkDocumentNode(ctx context.Context, node *yaml.Node, root *yaml.Node, visit VisitFunc) error {
57+
for i := 0; i < len(node.Content); i++ {
58+
if err := walkNode(ctx, node.Content[i], node, root, visit); err != nil {
59+
return err
60+
}
61+
}
62+
63+
return nil
64+
}
65+
66+
func walkMappingNode(ctx context.Context, node *yaml.Node, root *yaml.Node, visit VisitFunc) error {
67+
for i := 0; i < len(node.Content); i += 2 {
68+
key := node.Content[i]
69+
value := node.Content[i+1]
70+
71+
if err := walkNode(ctx, key, node, root, visit); err != nil {
72+
return err
73+
}
74+
75+
if err := walkNode(ctx, value, node, root, visit); err != nil {
76+
return err
77+
}
78+
}
79+
80+
return nil
81+
}
82+
83+
func walkSequenceNode(ctx context.Context, node *yaml.Node, root *yaml.Node, visit VisitFunc) error {
84+
for i := 0; i < len(node.Content); i++ {
85+
if err := walkNode(ctx, node.Content[i], node, root, visit); err != nil {
86+
return err
87+
}
88+
}
89+
90+
return nil
91+
}
92+
93+
func walkAliasNode(ctx context.Context, node *yaml.Node, root *yaml.Node, visit VisitFunc) error {
94+
return walkNode(ctx, node.Alias, node, root, visit)
95+
}

0 commit comments

Comments
 (0)