-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharazzo_examples_test.go
137 lines (107 loc) · 2.65 KB
/
arazzo_examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package arazzo_test
import (
"bytes"
"context"
"fmt"
"os"
"github.com/speakeasy-api/openapi/arazzo"
"github.com/speakeasy-api/openapi/pointer"
)
// The below examples should be copied into the README.md file if every changed TODO: automate this
func Example_readAndMutate() {
ctx := context.Background()
r, err := os.Open("testdata/speakeasybar.arazzo.yaml")
if err != nil {
panic(err)
}
defer r.Close()
// Unmarshal the Arazzo document which will also validate it against the Arazzo Specification
a, validationErrs, err := arazzo.Unmarshal(ctx, r)
if err != nil {
panic(err)
}
// Validation errors are returned separately from any errors that block the document from being unmarshalled
// allowing an invalid document to be mutated and fixed before being marshalled again
for _, err := range validationErrs {
fmt.Println(err.Error())
}
// Mutate the document by just modifying the returned Arazzo object
a.Info.Title = "Speakeasy Bar Workflows"
buf := bytes.NewBuffer([]byte{})
// Marshal the document to a writer
if err := arazzo.Marshal(ctx, a, buf); err != nil {
panic(err)
}
fmt.Println(buf.String())
}
func Example_creating() {
ctx := context.Background()
arazzo := &arazzo.Arazzo{
Arazzo: arazzo.Version,
Info: arazzo.Info{
Title: "My Workflow",
Summary: pointer.From("A summary"),
Version: "1.0.1",
},
// ...
}
buf := bytes.NewBuffer([]byte{})
err := arazzo.Marshal(ctx, buf)
if err != nil {
panic(err)
}
fmt.Printf("%s", buf.String())
}
func Example_mutating() {
ctx := context.Background()
f, err := os.Open("arazzo.yaml")
if err != nil {
panic(err)
}
arazzo, _, err := arazzo.Unmarshal(ctx, f)
if err != nil {
panic(err)
}
arazzo.Info.Title = "My updated workflow title"
buf := bytes.NewBuffer([]byte{})
if err := arazzo.Marshal(ctx, buf); err != nil {
panic(err)
}
fmt.Printf("%s", buf.String())
}
func Example_walking() {
ctx := context.Background()
f, err := os.Open("arazzo.yaml")
if err != nil {
panic(err)
}
a, _, err := arazzo.Unmarshal(ctx, f)
if err != nil {
panic(err)
}
err = arazzo.Walk(ctx, a, func(ctx context.Context, node, parent arazzo.MatchFunc, a *arazzo.Arazzo) error {
return node(arazzo.Matcher{
Workflow: func(workflow *arazzo.Workflow) error {
fmt.Printf("Workflow: %s\n", workflow.WorkflowID)
return nil
},
})
})
if err != nil {
panic(err)
}
}
func Example_validating() {
ctx := context.Background()
f, err := os.Open("arazzo.yaml")
if err != nil {
panic(err)
}
_, validationErrs, err := arazzo.Unmarshal(ctx, f)
if err != nil {
panic(err)
}
for _, err := range validationErrs {
fmt.Printf("%s\n", err.Error())
}
}