-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
76 lines (64 loc) · 1.51 KB
/
run.go
File metadata and controls
76 lines (64 loc) · 1.51 KB
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
package generator
import (
"context"
"fmt"
"log/slog"
"os"
"github.com/pb33f/libopenapi"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
)
// Run executes the custom generator using the provided parameters.
func Run(ctx context.Context, params Params) error {
if ctx == nil {
ctx = context.Background()
}
if err := params.normalize(); err != nil {
return err
}
if err := params.validate(); err != nil {
return err
}
doc, err := loadDocument(params.SpecPath)
if err != nil {
return err
}
model, err := buildModel(doc, params)
if err != nil {
return err
}
apiVersion := ""
if doc.Info != nil {
apiVersion = doc.Info.Version
}
slog.Info("Generating SDK", "spec", params.SpecPath)
if err := renderClients(model, params); err != nil {
return err
}
if err := renderModels(model, params); err != nil {
return err
}
if err := renderSumUpClient(model, params); err != nil {
return err
}
if err := renderApiVersionResource(apiVersion, params); err != nil {
return err
}
return nil
}
// loadDocument reads and parses the OpenAPI specification into the pbo33f
// high-level model.
func loadDocument(specPath string) (*v3.Document, error) {
data, err := os.ReadFile(specPath)
if err != nil {
return nil, fmt.Errorf("read spec: %w", err)
}
doc, err := libopenapi.NewDocument(data)
if err != nil {
return nil, fmt.Errorf("parse spec: %w", err)
}
model, err := doc.BuildV3Model()
if err != nil {
return nil, fmt.Errorf("build spec model: %w", err)
}
return &model.Model, nil
}