|
| 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