mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 20:47:44 +00:00
3.0 and 2.0 do not work, there are multiple versions and anything with a period in it sucks from my point of view, v2 and v3 feel much better from a DX perspective.
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package v3
|
|
|
|
import (
|
|
"fmt"
|
|
low "github.com/pb33f/libopenapi/datamodel/low/v3"
|
|
)
|
|
|
|
type Responses struct {
|
|
Codes map[string]*Response
|
|
Default *Response
|
|
low *low.Responses
|
|
}
|
|
|
|
func NewResponses(response *low.Responses) *Responses {
|
|
r := new(Responses)
|
|
r.low = response
|
|
if !response.Default.IsEmpty() {
|
|
r.Default = NewResponse(response.Default.Value)
|
|
}
|
|
codes := make(map[string]*Response)
|
|
|
|
// struct to hold response and code sent over chan.
|
|
type respRes struct {
|
|
code string
|
|
resp *Response
|
|
}
|
|
|
|
// build each response async for speed
|
|
rChan := make(chan respRes)
|
|
var buildResponse = func(code string, resp *low.Response, c chan respRes) {
|
|
c <- respRes{code: code, resp: NewResponse(resp)}
|
|
}
|
|
for k, v := range response.Codes {
|
|
go buildResponse(k.Value, v.Value, rChan)
|
|
}
|
|
totalCodes := len(response.Codes)
|
|
codesParsed := 0
|
|
for codesParsed < totalCodes {
|
|
select {
|
|
case re := <-rChan:
|
|
codesParsed++
|
|
codes[re.code] = re.resp
|
|
}
|
|
}
|
|
r.Codes = codes
|
|
return r
|
|
}
|
|
|
|
func (r *Responses) FindResponseByCode(code int) *Response {
|
|
return r.Codes[fmt.Sprintf("%d", code)]
|
|
}
|
|
|
|
func (r *Responses) GoLow() *low.Responses {
|
|
return r.low
|
|
}
|