trpc.group/trpc-go/trpc-cmdline@v1.0.9/util/apidocs/parameter.go (about)

     1  // Tencent is pleased to support the open source community by making tRPC available.
     2  //
     3  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     4  // All rights reserved.
     5  //
     6  // If you have downloaded a copy of the tRPC source code from Tencent,
     7  // please note that tRPC source code is licensed under the  Apache 2.0 License,
     8  // A copy of the Apache 2.0 License is included in this file.
     9  
    10  package apidocs
    11  
    12  // ParametersStruct defines the structure of input parameters information for methods in Swagger JSON format.
    13  type ParametersStruct struct {
    14  	Name string `json:"name"` // Name of the parameter.
    15  	// Parameter's passing method: in, query, path, header, form.
    16  	In          string        `json:"in"`
    17  	Required    bool          `json:"required"`              // Whether the parameter is required or not.
    18  	Type        string        `json:"type,omitempty"`        // Type of parameter.
    19  	Schema      *SchemaStruct `json:"schema,omitempty"`      // Parameter reference, optional.
    20  	Format      string        `json:"format,omitempty"`      // Parameter format, optional.
    21  	Description string        `json:"description,omitempty"` // Parameter description, optional.
    22  	Enum        []int32       `json:"enum,omitempty"`        // Possible values for the enum.
    23  	// When type = array, it is necessary to indicate the member type, that is, the description of a single field value.
    24  	Items   *PropertyStruct `json:"items,omitempty"`
    25  	Default interface{}     `json:"default,omitempty"` // Default value of the parameter.
    26  }
    27  
    28  // ParameterStructX for v3.
    29  type ParameterStructX struct {
    30  	Name string `json:"name"` // Name of the parameter.
    31  	// Parameter's passing method: in, query, path, header, form.
    32  	In          string       `json:"in"`
    33  	Required    bool         `json:"required"`              // Whether the parameter is required or not.
    34  	Description string       `json:"description,omitempty"` // Parameter description.
    35  	Schema      *ModelStruct `json:"schema,omitempty"`      // Parameter reference.
    36  }
    37  
    38  // GetParametersStructX converts the structure.
    39  func (param ParametersStruct) GetParametersStructX() *ParameterStructX {
    40  	ref := ""
    41  	if param.Schema != nil {
    42  		ref = param.Schema.Ref
    43  	}
    44  	return &ParameterStructX{
    45  		Name:        param.Name,
    46  		In:          param.In,
    47  		Required:    param.Required,
    48  		Description: param.Description,
    49  		Schema: &ModelStruct{
    50  			Type:                 param.Type,
    51  			Title:                param.Name,
    52  			Description:          param.Description,
    53  			AdditionalProperties: nil,
    54  			Ref:                  ref,
    55  			Items:                param.Items,
    56  		},
    57  	}
    58  }
    59  
    60  // GetRequestBody converts the structure.
    61  func (param ParametersStruct) GetRequestBody() *BodyContentStruct {
    62  	return &BodyContentStruct{
    63  		Content: map[string]MediaStruct{
    64  			"application/json": {
    65  				Schema: *param.Schema,
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  // GetProperty converts the structure.
    72  func (param ParametersStruct) GetProperty() *PropertyStruct {
    73  	ref := ""
    74  	if param.Schema != nil {
    75  		ref = param.Schema.Ref
    76  	}
    77  	return &PropertyStruct{
    78  		Title:       param.Name,
    79  		Type:        param.Type,
    80  		Format:      param.Format,
    81  		Ref:         ref,
    82  		Description: param.Description,
    83  		Enum:        param.Enum,
    84  		Items:       param.Items,
    85  	}
    86  }