-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparams.go
More file actions
104 lines (95 loc) · 2.98 KB
/
params.go
File metadata and controls
104 lines (95 loc) · 2.98 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
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
package generator
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
// Params configure a generator run.
type Params struct {
SpecPath string
OutputDir string
ResourceDir string
BasePackage string
}
// normalize fills missing configuration with defaults and resolves absolute
// paths, ensuring downstream rendering code can operate deterministically.
func (p *Params) normalize() error {
if p.BasePackage == "" {
p.BasePackage = "com.sumup.sdk"
}
if p.OutputDir == "" {
p.OutputDir = filepath.Join("..", "src", "main", "java")
}
var err error
if p.SpecPath == "" {
p.SpecPath = filepath.Join("..", "openapi.json")
}
if p.SpecPath, err = filepath.Abs(p.SpecPath); err != nil {
return fmt.Errorf("resolve spec path: %w", err)
}
if p.OutputDir, err = filepath.Abs(p.OutputDir); err != nil {
return fmt.Errorf("resolve output directory: %w", err)
}
if p.ResourceDir == "" {
p.ResourceDir = filepath.Join(filepath.Dir(p.OutputDir), "resources")
}
if p.ResourceDir, err = filepath.Abs(p.ResourceDir); err != nil {
return fmt.Errorf("resolve resource directory: %w", err)
}
return nil
}
// validate ensures the OpenAPI specification exists and the target directory is
// writable (creating it if needed).
func (p *Params) validate() error {
if _, err := os.Stat(p.SpecPath); err != nil {
return fmt.Errorf("spec not found: %w", err)
}
fi, err := os.Stat(p.OutputDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(p.OutputDir, 0o755); err != nil {
return fmt.Errorf("create output directory: %w", err)
}
} else {
return fmt.Errorf("output directory: %w", err)
}
} else if !fi.IsDir() {
return fmt.Errorf("output directory %s is not a directory", p.OutputDir)
}
fi, err = os.Stat(p.ResourceDir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(p.ResourceDir, 0o755); err != nil {
return fmt.Errorf("create resource directory: %w", err)
}
} else {
return fmt.Errorf("resource directory: %w", err)
}
} else if !fi.IsDir() {
return fmt.Errorf("resource directory %s is not a directory", p.ResourceDir)
}
return nil
}
// basePackagePath translates the base Java package into a filesystem path
// relative to the configured output directory.
func (p Params) basePackagePath() string {
return filepath.Join(strings.Split(p.BasePackage, ".")...)
}
// clientPackage returns the Java package path where API clients live.
func (p Params) clientPackage() string {
return p.BasePackage + ".clients"
}
// clientPackagePath returns the filesystem path that backs the client package.
func (p Params) clientPackagePath() string {
return filepath.Join(p.basePackagePath(), "clients")
}
// modelPackage returns the Java package for generated shared models.
func (p Params) modelPackage() string {
return p.BasePackage + ".models"
}
// modelPackagePath returns the filesystem path to the models package.
func (p Params) modelPackagePath() string {
return filepath.Join(p.basePackagePath(), "models")
}