github.com/gogf/gf/v2@v2.7.4/net/goai/goai_operation.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/gogf/gf.
     6  
     7  package goai
     8  
     9  import (
    10  	"github.com/gogf/gf/v2/errors/gerror"
    11  	"github.com/gogf/gf/v2/internal/json"
    12  	"github.com/gogf/gf/v2/util/gconv"
    13  )
    14  
    15  // Operation represents "operation" specified by OpenAPI/Swagger 3.0 standard.
    16  type Operation struct {
    17  	Tags         []string              `json:"tags,omitempty"`
    18  	Summary      string                `json:"summary,omitempty"`
    19  	Description  string                `json:"description,omitempty"`
    20  	OperationID  string                `json:"operationId,omitempty"`
    21  	Parameters   Parameters            `json:"parameters,omitempty"`
    22  	RequestBody  *RequestBodyRef       `json:"requestBody,omitempty"`
    23  	Responses    Responses             `json:"responses"`
    24  	Deprecated   bool                  `json:"deprecated,omitempty"`
    25  	Callbacks    *Callbacks            `json:"callbacks,omitempty"`
    26  	Security     *SecurityRequirements `json:"security,omitempty"`
    27  	Servers      *Servers              `json:"servers,omitempty"`
    28  	ExternalDocs *ExternalDocs         `json:"externalDocs,omitempty"`
    29  	XExtensions  XExtensions           `json:"-"`
    30  }
    31  
    32  func (oai *OpenApiV3) tagMapToOperation(tagMap map[string]string, operation *Operation) error {
    33  	var mergedTagMap = oai.fillMapWithShortTags(tagMap)
    34  	if err := gconv.Struct(mergedTagMap, operation); err != nil {
    35  		return gerror.Wrap(err, `mapping struct tags to Operation failed`)
    36  	}
    37  	oai.tagMapToXExtensions(mergedTagMap, operation.XExtensions)
    38  	return nil
    39  }
    40  
    41  func (o Operation) MarshalJSON() ([]byte, error) {
    42  	var (
    43  		b   []byte
    44  		m   map[string]json.RawMessage
    45  		err error
    46  	)
    47  	type tempOperation Operation // To prevent JSON marshal recursion error.
    48  	if b, err = json.Marshal(tempOperation(o)); err != nil {
    49  		return nil, err
    50  	}
    51  	if err = json.Unmarshal(b, &m); err != nil {
    52  		return nil, err
    53  	}
    54  	for k, v := range o.XExtensions {
    55  		if b, err = json.Marshal(v); err != nil {
    56  			return nil, err
    57  		}
    58  		m[k] = b
    59  	}
    60  	return json.Marshal(m)
    61  }