package swaggerset import ( "context" "errors" "fmt" "sync" "github.com/getkin/kin-openapi/openapi3" ) var errNotFound = errors.New("file descriptor not found") type SwaggerSet struct { mu sync.Mutex files map[string]*openapi3.T } func NewSwaggerSet() *SwaggerSet { return &SwaggerSet{ mu: sync.Mutex{}, files: make(map[string]*openapi3.T, 0), } } func (p *SwaggerSet) GetMessage(addr, svc, mth, typereq string) (*messages, error) { if svc == "" || mth == "" || addr == "" || typereq == "" { return nil, errors.New("addr or service name is empty") } messages := newMessages() p.mu.Lock() doc := p.files[addr+"|"+svc] p.mu.Unlock() pathItem := doc.Paths.Value(mth) if pathItem.Get != nil { reqParam, reqBody, rsp := handleOperation("GET", pathItem.Get) messages.Msgs = append(messages.Msgs, message{ Type: "GET", RequestParam: reqParam, RequestBody: reqBody, Response: rsp, }) } if pathItem.Post != nil { reqParam, reqBody, rsp := handleOperation("POST", pathItem.Post) messages.Msgs = append(messages.Msgs, message{ Type: "POST", RequestParam: reqParam, RequestBody: reqBody, Response: rsp, }) } return messages, nil } func (p *SwaggerSet) AddSwaggerset(addr, svc string, data []byte) error { ctx := context.Background() loader := openapi3.NewLoader() loader.IsExternalRefsAllowed = true doc, err := loader.LoadFromData(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) } p.mu.Lock() p.files[addr+"|"+svc] = doc p.mu.Unlock() return nil }