Skip to content

Commit

Permalink
docs: update and fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
TristanSpeakEasy committed Oct 4, 2024
1 parent b5d6cd4 commit 87a46b1
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 86 deletions.
172 changes: 86 additions & 86 deletions arazzo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ import (
)

func main() {
ctx := context.Background()
ctx := context.Background()

data, err := os.ReadFile("arazzo.yaml")
if err != nil {
panic(err)
}
f, err := os.Open("arazzo.yaml")
if err != nil {
panic(err)
}

arazzo, validationErrs, err := arazzo.Unmarshal(ctx, data, "arazzo.yaml")
if err != nil {
panic(err)
}
arazzo, validationErrs, err := arazzo.Unmarshal(ctx, f)
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", arazzo)
fmt.Printf("%+v\n", validationErrs)
fmt.Printf("%+v\n", arazzo)
fmt.Printf("%+v\n", validationErrs)
}
```

Expand All @@ -47,29 +47,30 @@ import (
"fmt"

"github.com/speakeasy-api/openapi/arazzo"
"github.com/speakeasy-api/openapi/pointer"
)

func main() {
ctx := context.Background()

arazzo := &arazzo.Arazzo{
Arazzo: Version,
Info: arazzo.Info{
Title: "My Workflow",
Summary: arazzo.pointer.From("A summary"),
Version: "1.0.0",
},
// ...
}

buf := bytes.NewBuffer([]byte{})

err := arazzo.Marshal(ctx, buf)
if err != nil {
panic(err)
}

fmt.Printf("%s", buf.String())
ctx := context.Background()

arazzo := &arazzo.Arazzo{
Arazzo: arazzo.Version,
Info: arazzo.Info{
Title: "My Workflow",
Summary: pointer.From("A summary"),
Version: "1.0.0",
},
// ...
}

buf := bytes.NewBuffer([]byte{})

err := arazzo.Marshal(ctx, buf)
if err != nil {
panic(err)
}

fmt.Printf("%s", buf.String())
}
```

Expand All @@ -86,28 +87,27 @@ import (
)

func main() {
ctx := context.Background()
ctx := context.Background()

data, err := os.ReadFile("arazzo.yaml")
if err != nil {
panic(err)
}
f, err := os.Open("arazzo.yaml")
if err != nil {
panic(err)
}

arazzo, _, err := arazzo.Unmarshal(ctx, data, "arazzo.yaml")
if err != nil {
panic(err)
}

arazzo.Info.Title = "My updated workflow title"
arazzo, _, err := arazzo.Unmarshal(ctx, f)
if err != nil {
panic(err)
}

buf := bytes.NewBuffer([]byte{})
arazzo.Info.Title = "My updated workflow title"

err := arazzo.Marshal(ctx, buf)
if err != nil {
panic(err)
}
buf := bytes.NewBuffer([]byte{})

fmt.Printf("%s", buf.String())
if err := arazzo.Marshal(ctx, buf); err != nil {
panic(err)
}

fmt.Printf("%s", buf.String())
}
```

Expand All @@ -125,29 +125,29 @@ import (
)

func main() {
ctx := context.Background()

data, err := os.ReadFile("arazzo.yaml")
if err != nil {
panic(err)
}

arazzo, _, err := arazzo.Unmarshal(ctx, data, "arazzo.yaml")
if err != nil {
panic(err)
}

err = arazzo.Walk(ctx, func(ctx context.Context, node, parent arazzo.MatchFunc, arazzo *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)
}
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)
}
}
```

Expand All @@ -165,20 +165,20 @@ import (
)

func main() {
ctx := context.Background()

data, err := os.ReadFile("arazzo.yaml")
if err != nil {
panic(err)
}

arazzo, validationErrs, err := arazzo.Unmarshal(ctx, data, "arazzo.yaml")
if err != nil {
panic(err)
}
for _, err := range validationErrs {
fmt.Printf("%s\n", err.Error())
}
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())
}
}
```
110 changes: 110 additions & 0 deletions arazzo/arazzo_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"github.com/speakeasy-api/openapi/arazzo"
"github.com/speakeasy-api/openapi/pointer"
)

func Example_readAndMutate() {
Expand Down Expand Up @@ -42,3 +43,112 @@ func Example_readAndMutate() {

fmt.Println(buf.String())
}

// The below examples should be copied into the README.md file if every changed TODO: automate this
func Example_reading() {
ctx := context.Background()

f, err := os.Open("arazzo.yaml")
if err != nil {
panic(err)
}

arazzo, validationErrs, err := arazzo.Unmarshal(ctx, f)
if err != nil {
panic(err)
}

fmt.Printf("%+v\n", arazzo)
fmt.Printf("%+v\n", validationErrs)
}

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.0",
},
// ...
}

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())
}
}

0 comments on commit 87a46b1

Please sign in to comment.