Skip to content

Commit 9798236

Browse files
committed
clean
1 parent 5e5e3bd commit 9798236

File tree

6 files changed

+21
-170
lines changed

6 files changed

+21
-170
lines changed

builder/builder.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (b *Builder) ClearCache() {
157157
b.swagger.Components.Schemas = make(openapi3.Schemas)
158158
}
159159

160-
func (b *Builder) GenerateJson(ctx context.Context, callbacksObject shared.ChipiCallbacks) ([]byte, error) {
160+
func (b *Builder) GenerateSwagger(ctx context.Context, callbacksObject shared.ChipiCallbacks) (*openapi3.T, error) {
161161

162162
swagger := *b.swagger
163163
for _, m := range b.methods {
@@ -228,7 +228,15 @@ func (b *Builder) GenerateJson(ctx context.Context, callbacksObject shared.Chipi
228228
swagger.Paths.Set(key, value)
229229
}
230230

231-
swagger = b.injectExtraDocumentation(ctx, callbacksObject.ExtraDocumentation(), swagger)
231+
return &swagger, nil
232+
}
233+
234+
func (b *Builder) GenerateJson(ctx context.Context, callbacksObject shared.ChipiCallbacks) ([]byte, error) {
235+
236+
swagger, err := b.GenerateSwagger(ctx, callbacksObject)
237+
if err != nil {
238+
return nil, err
239+
}
232240

233241
json, err := swagger.MarshalJSON()
234242
if err != nil {

builder/extra_documentation.go

-90
This file was deleted.

example/doc.yaml

-23
This file was deleted.

example/main.go

-37
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ import (
1010
"github.com/getkin/kin-openapi/openapi3"
1111
"github.com/go-chi/chi/v5"
1212
"github.com/go-chi/cors"
13-
"gopkg.in/yaml.v2"
1413

1514
_ "embed"
1615

1716
"github.com/schmurfy/chipi"
18-
"github.com/schmurfy/chipi/shared"
1917
)
2018

2119
//go:embed index.html
@@ -71,21 +69,6 @@ func main() {
7169

7270
router.Get("/doc.json", api.ServeSchema)
7371

74-
router.Get("/doc_injection.json", func(w http.ResponseWriter, r *http.Request) {
75-
data, err := api.GenerateJson(r.Context(), shared.NewChipiCallbacks(&ExtraDocumentationInterface{}))
76-
if err != nil {
77-
http.Error(w, err.Error(), http.StatusInternalServerError)
78-
return
79-
}
80-
w.Header().Add("Content-Type", "application/json")
81-
_, err = w.Write(data)
82-
if err != nil {
83-
http.Error(w, err.Error(), http.StatusInternalServerError)
84-
return
85-
}
86-
})
87-
88-
// router.Get("/doc.json", api.ServeSchema)
8972
router.Get("/doc2.json", func(w http.ResponseWriter, r *http.Request) {
9073
f, err := os.Open("/tmp/doc.json")
9174
if err != nil {
@@ -160,23 +143,3 @@ func main() {
160143
panic(err)
161144
}
162145
}
163-
164-
var _ shared.ExtraDocumentationInterface = (*ExtraDocumentationInterface)(nil)
165-
166-
type ExtraDocumentationInterface struct{}
167-
168-
func (e ExtraDocumentationInterface) ExtraDocumentation() *shared.DocumentationInjection {
169-
file, err := os.ReadFile("doc.yaml")
170-
if err != nil {
171-
panic(err)
172-
}
173-
174-
data := shared.DocumentationInjection{}
175-
176-
err = yaml.Unmarshal([]byte(file), &data)
177-
if err != nil {
178-
panic(err)
179-
}
180-
181-
return &data
182-
}

shared/documentation.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
package shared
22

3-
type ComponentsInjection map[string]map[string]FieldInjection
3+
type SchemasInjection map[string]SchemaInjection
44

55
type DocumentationInjection struct {
66
Operations map[string]OperationInjection `yaml:"operations"`
7-
Components ComponentsInjection `yaml:"components"`
7+
Schemas SchemasInjection `yaml:"schemas"`
88
}
99

10-
type FieldInjection struct {
10+
type SchemaInjection struct {
11+
Properties map[string]PropertyInjection `yaml:"properties"`
12+
Description string `yaml:"description"`
13+
}
14+
15+
type PropertyInjection struct {
1116
Description string `yaml:"description"`
1217
}
1318

1419
type OperationInjection struct {
1520
Description string `yaml:"description"`
1621
Summary string `yaml:"summary"`
17-
Params map[string]ParamInjection `yaml:"params"`
22+
Path map[string]ParamInjection `yaml:"path"`
23+
Query map[string]ParamInjection `yaml:"query"`
24+
Header map[string]ParamInjection `yaml:"header"`
1825
}
1926

2027
type ParamInjection struct {
2128
Description string `yaml:"description"`
2229
Example string `yaml:"example"`
23-
Examples string `yaml:"examples"`
2430
}

shared/filter.go

-13
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,19 @@ type SchemaResolverInterface interface {
8383
type ExtraComponentsAndPathsInterface interface {
8484
ExtraComponentsAndPaths() (openapi3.Schemas, openapi3.Paths)
8585
}
86-
type ExtraDocumentationInterface interface {
87-
ExtraDocumentation() *DocumentationInjection
88-
}
8986

9087
type ChipiCallbacks struct {
9188
FilterRouteInterface
9289
FilterFieldInterface
9390
EnumResolverInterface
9491
SchemaResolverInterface
95-
ExtraDocumentationInterface
9692
i ChipiCallbackInterface
9793
}
9894

9995
var _ FilterRouteInterface = (*ChipiCallbacks)(nil)
10096
var _ FilterFieldInterface = (*ChipiCallbacks)(nil)
10197
var _ EnumResolverInterface = (*ChipiCallbacks)(nil)
10298
var _ SchemaResolverInterface = (*ChipiCallbacks)(nil)
103-
var _ ExtraDocumentationInterface = (*ChipiCallbacks)(nil)
10499

105100
func NewChipiCallbacks(i ChipiCallbackInterface) ChipiCallbacks {
106101
return ChipiCallbacks{i: i}
@@ -145,11 +140,3 @@ func (c *ChipiCallbacks) ExtraComponentsAndPaths() (openapi3.Schemas, openapi3.P
145140
return nil, openapi3.Paths{}
146141
}
147142
}
148-
149-
func (c *ChipiCallbacks) ExtraDocumentation() *DocumentationInjection {
150-
if schemaInterface, hasExtraDoc := c.i.(ExtraDocumentationInterface); c.i != nil && hasExtraDoc {
151-
return schemaInterface.ExtraDocumentation()
152-
} else {
153-
return nil
154-
}
155-
}

0 commit comments

Comments
 (0)