Skip to content

Commit c6bcc18

Browse files
committed
fix(clientgen): option fix
1 parent f37e051 commit c6bcc18

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

devpkg/clientgen/gen.go

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,46 @@ func (g *clientGen) GenerateType(c gengo.Context, named *types.Named) error {
4747
}
4848

4949
type option struct {
50-
TypeGenPolicy TypeGenPolicy
51-
TrimBashPath string
52-
Include []string
50+
OpenAPISpecURI string
51+
TypeGenPolicy TypeGenPolicy
52+
TrimBasePath string
53+
Include []string
54+
}
55+
56+
func (o *option) ShouldGenerate(op *openapi.OperationObject) bool {
57+
if op == nil {
58+
return false
59+
}
60+
61+
if len(o.Include) == 0 {
62+
return true
63+
}
64+
65+
for _, opID := range o.Include {
66+
if opID == op.OperationId {
67+
return true
68+
}
69+
}
70+
71+
return false
5372
}
5473

5574
func (o *option) Build(tags map[string][]string) {
75+
if r, ok := tags["gengo:client:openapi"]; ok {
76+
if len(r) > 0 {
77+
o.OpenAPISpecURI = r[0]
78+
}
79+
}
80+
5681
if r, ok := tags["gengo:client:typegen-policy"]; ok {
5782
if len(r) > 0 {
5883
o.TypeGenPolicy = TypeGenPolicy(r[0])
5984
}
6085
}
6186

62-
if r, ok := tags["gengo:client:openapi:trim-bash-path"]; ok {
87+
if r, ok := tags["gengo:client:openapi:trim-base-path"]; ok {
6388
if len(r) > 0 {
64-
o.TrimBashPath = r[0]
89+
o.TrimBasePath = r[0]
6590
}
6691
}
6792

@@ -79,46 +104,19 @@ const (
79104
)
80105

81106
func (g *clientGen) generateClient(c gengo.Context, named *types.Named) error {
82-
openapiSpec := ""
83-
tags, _ := c.Doc(named.Obj())
84-
85-
includes := make([]string, 0)
86-
87-
shouldGenerate := func(o *openapi.OperationObject) bool {
88-
if o == nil {
89-
return false
90-
}
91-
92-
if len(includes) == 0 {
93-
return true
94-
}
95-
96-
for i := range includes {
97-
if includes[i] == o.OperationId {
98-
return true
99-
}
100-
}
101-
102-
return false
103-
}
104-
105-
trimBashPath := ""
106-
107-
if r, ok := tags["gengo:client:openapi"]; ok {
108-
if len(r) > 0 {
109-
openapiSpec = r[0]
110-
}
111-
}
112-
113107
o := option{
114108
TypeGenPolicy: TypeGenPolicyGoVendorImported,
115109
}
116110

117-
if openapiSpec == "" {
111+
tags, _ := c.Doc(named.Obj())
112+
113+
o.Build(tags)
114+
115+
if o.OpenAPISpecURI == "" {
118116
return fmt.Errorf("openapi spec is not defined, please use `gengo:client:openapi=http://path/to/openapi/spec`")
119117
}
120118

121-
u, err := url.Parse(openapiSpec)
119+
u, err := url.Parse(o.OpenAPISpecURI)
122120
if err != nil {
123121
return err
124122
}
@@ -138,10 +136,10 @@ func (g *clientGen) generateClient(c gengo.Context, named *types.Named) error {
138136

139137
for p, operations := range g.oas.Paths.KeyValues() {
140138
for method, op := range operations.KeyValues() {
141-
if shouldGenerate(op) {
142-
if o.TrimBashPath != "" {
143-
if strings.HasPrefix(p, trimBashPath) {
144-
p = p[len(trimBashPath):]
139+
if o.ShouldGenerate(op) {
140+
if o.TrimBasePath != "" {
141+
if strings.HasPrefix(p, o.TrimBasePath) {
142+
p = p[len(o.TrimBasePath):]
145143
}
146144
}
147145

0 commit comments

Comments
 (0)