#2 - change to micro-proto/openapi

This commit is contained in:
Gorbunov Kirill Andreevich
2024-12-04 15:36:55 +03:00
parent e207117ba4
commit 8b6c5049b1
6 changed files with 126 additions and 69 deletions

View File

@@ -1,30 +1,29 @@
package swaggerset
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"github.com/getkin/kin-openapi/openapi3"
openapi "go.unistack.org/micro-proto/v4/openapiv3"
)
var errNotFound = errors.New("file descriptor not found")
type SwaggerSet struct {
mu sync.Mutex
files map[string]*openapi3.T
files map[string]*openapi.Document
}
func NewSwaggerSet() *SwaggerSet {
return &SwaggerSet{
mu: sync.Mutex{},
files: make(map[string]*openapi3.T, 0),
files: make(map[string]*openapi.Document, 0),
}
}
func (p *SwaggerSet) GetMessage(addr, svc, mth string, typereq string) (*Message, error) {
if svc == "" || mth == "" || addr == "" || typereq == "" {
func (p *SwaggerSet) GetMessage(addr, svc, mth string) (*Message, error) {
if svc == "" || mth == "" || addr == "" {
return nil, errors.New("addr or service name is empty")
}
@@ -32,10 +31,34 @@ func (p *SwaggerSet) GetMessage(addr, svc, mth string, typereq string) (*Message
doc := p.files[addr+"|"+svc]
p.mu.Unlock()
pathItem := doc.Paths.Value(mth)
reqParam, reqBody, rsp := handleOperation(typereq, pathItem.Get)
var reqParam, reqBody, rsp interface{}
var typeReq string
for _, path := range doc.Paths.GetPath() {
if path.GetName() == mth {
if path.GetValue().GetGet() != nil {
typeReq = http.MethodGet
reqParam, reqBody, rsp = handleOperation(path.GetValue().GetGet(), doc)
}
if path.GetValue().GetPost() != nil {
typeReq = http.MethodPost
reqParam, reqBody, rsp = handleOperation(path.GetValue().GetPost(), doc)
}
if path.GetValue().GetDelete() != nil {
typeReq = http.MethodDelete
reqParam, reqBody, rsp = handleOperation(path.GetValue().GetDelete(), doc)
}
if path.GetValue().GetPatch() != nil {
typeReq = http.MethodPatch
reqParam, reqBody, rsp = handleOperation(path.GetValue().GetPatch(), doc)
}
if path.GetValue().GetPut() != nil {
typeReq = http.MethodPut
reqParam, reqBody, rsp = handleOperation(path.GetValue().GetPut(), doc)
}
}
}
msg := &Message{
Type: typereq,
Type: typeReq,
Request: httpRequest{
Header: reqParam,
Body: reqBody,
@@ -47,18 +70,9 @@ func (p *SwaggerSet) GetMessage(addr, svc, mth string, typereq string) (*Message
}
func (p *SwaggerSet) AddSwaggerset(addr, svc string, data []byte) error {
ctx := context.Background()
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
doc, err := loader.LoadFromData(data)
doc, err := openapi.ParseDocument(data)
if err != nil {
return fmt.Errorf("failed to load data from buf: %w", err)
}
if err = doc.Validate(ctx); err != nil {
return fmt.Errorf("failed to validate data from swagger: %w", err)
return err
}
p.mu.Lock()