github.com/wangyougui/gf/v2@v2.6.5/net/goai/goai_parameter.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  // Parameter is specified by OpenAPI/Swagger 3.0 standard.
    16  // See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#parameterObject
    17  type Parameter struct {
    18  	Name            string      `json:"name,omitempty"`
    19  	In              string      `json:"in,omitempty"`
    20  	Description     string      `json:"description,omitempty"`
    21  	Style           string      `json:"style,omitempty"`
    22  	Explode         *bool       `json:"explode,omitempty"`
    23  	AllowEmptyValue bool        `json:"allowEmptyValue,omitempty"`
    24  	AllowReserved   bool        `json:"allowReserved,omitempty"`
    25  	Deprecated      bool        `json:"deprecated,omitempty"`
    26  	Required        bool        `json:"required,omitempty"`
    27  	Schema          *SchemaRef  `json:"schema,omitempty"`
    28  	Example         interface{} `json:"example,omitempty"`
    29  	Examples        *Examples   `json:"examples,omitempty"`
    30  	Content         *Content    `json:"content,omitempty"`
    31  	XExtensions     XExtensions `json:"-"`
    32  }
    33  
    34  func (oai *OpenApiV3) tagMapToParameter(tagMap map[string]string, parameter *Parameter) error {
    35  	var mergedTagMap = oai.fillMapWithShortTags(tagMap)
    36  	if err := gconv.Struct(mergedTagMap, parameter); err != nil {
    37  		return gerror.Wrap(err, `mapping struct tags to Parameter failed`)
    38  	}
    39  	oai.tagMapToXExtensions(mergedTagMap, parameter.XExtensions)
    40  	return nil
    41  }
    42  
    43  func (p Parameter) MarshalJSON() ([]byte, error) {
    44  	var (
    45  		b   []byte
    46  		m   map[string]json.RawMessage
    47  		err error
    48  	)
    49  	type tempParameter Parameter // To prevent JSON marshal recursion error.
    50  	if b, err = json.Marshal(tempParameter(p)); err != nil {
    51  		return nil, err
    52  	}
    53  	if err = json.Unmarshal(b, &m); err != nil {
    54  		return nil, err
    55  	}
    56  	for k, v := range p.XExtensions {
    57  		if b, err = json.Marshal(v); err != nil {
    58  			return nil, err
    59  		}
    60  		m[k] = b
    61  	}
    62  	return json.Marshal(m)
    63  }