github.com/wangyougui/gf/v2@v2.6.5/net/goai/goai_response.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package goai 8 9 import ( 10 "github.com/wangyougui/gf/v2/errors/gerror" 11 "github.com/wangyougui/gf/v2/internal/json" 12 "github.com/wangyougui/gf/v2/util/gconv" 13 ) 14 15 // Response is specified by OpenAPI/Swagger 3.0 standard. 16 type Response struct { 17 Description string `json:"description"` 18 Headers Headers `json:"headers,omitempty"` 19 Content Content `json:"content,omitempty"` 20 Links Links `json:"links,omitempty"` 21 XExtensions XExtensions `json:"-"` 22 } 23 24 func (oai *OpenApiV3) tagMapToResponse(tagMap map[string]string, response *Response) error { 25 var mergedTagMap = oai.fillMapWithShortTags(tagMap) 26 if err := gconv.Struct(mergedTagMap, response); err != nil { 27 return gerror.Wrap(err, `mapping struct tags to Response failed`) 28 } 29 oai.tagMapToXExtensions(mergedTagMap, response.XExtensions) 30 return nil 31 } 32 33 func (r Response) MarshalJSON() ([]byte, error) { 34 var ( 35 b []byte 36 m map[string]json.RawMessage 37 err error 38 ) 39 type tempResponse Response // To prevent JSON marshal recursion error. 40 if b, err = json.Marshal(tempResponse(r)); err != nil { 41 return nil, err 42 } 43 if err = json.Unmarshal(b, &m); err != nil { 44 return nil, err 45 } 46 for k, v := range r.XExtensions { 47 if b, err = json.Marshal(v); err != nil { 48 return nil, err 49 } 50 m[k] = b 51 } 52 return json.Marshal(m) 53 }