trpc.group/trpc-go/trpc-cmdline@v1.0.9/util/apidocs/x/types.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 x provides common utilities for documentation operations.
    11  package x
    12  
    13  import protobuf "google.golang.org/protobuf/types/descriptorpb"
    14  
    15  // FieldDescriptorProtoTypeFormats is used to convert field type definitions
    16  // from the protobuf library to the openapi data format map.
    17  var FieldDescriptorProtoTypeFormats = map[protobuf.FieldDescriptorProto_Type]string{
    18  	protobuf.FieldDescriptorProto_TYPE_DOUBLE:  "double",
    19  	protobuf.FieldDescriptorProto_TYPE_FLOAT:   "float",
    20  	protobuf.FieldDescriptorProto_TYPE_INT64:   "int64",
    21  	protobuf.FieldDescriptorProto_TYPE_UINT64:  "uint64",
    22  	protobuf.FieldDescriptorProto_TYPE_INT32:   "int32",
    23  	protobuf.FieldDescriptorProto_TYPE_FIXED64: "fixed64",
    24  	protobuf.FieldDescriptorProto_TYPE_FIXED32: "fixed32",
    25  	// Refer to https://swagger.io/specification, boolean type doesn't need to set format.
    26  	protobuf.FieldDescriptorProto_TYPE_BOOL: "",
    27  	// Refer to https://swagger.io/specification, string type doesn't need to set format.
    28  	protobuf.FieldDescriptorProto_TYPE_STRING:   "",
    29  	protobuf.FieldDescriptorProto_TYPE_GROUP:    "group",
    30  	protobuf.FieldDescriptorProto_TYPE_MESSAGE:  "message",
    31  	protobuf.FieldDescriptorProto_TYPE_BYTES:    "bytes",
    32  	protobuf.FieldDescriptorProto_TYPE_UINT32:   "uint32",
    33  	protobuf.FieldDescriptorProto_TYPE_ENUM:     "int32",
    34  	protobuf.FieldDescriptorProto_TYPE_SFIXED32: "sfixed32",
    35  	protobuf.FieldDescriptorProto_TYPE_SFIXED64: "sfixed64",
    36  	protobuf.FieldDescriptorProto_TYPE_SINT32:   "sint32",
    37  	protobuf.FieldDescriptorProto_TYPE_SINT64:   "sint64",
    38  }
    39  
    40  // FieldDescriptorProtoTypes is used to convert field types defined in protobuf library to openapi data type map table.
    41  // Since trpc uses jsonpb as the default encoding and decoding method,
    42  // according to the proto specification, int64, uint64, and fixed64 will be serialized as strings.
    43  var FieldDescriptorProtoTypes = map[protobuf.FieldDescriptorProto_Type]string{
    44  	protobuf.FieldDescriptorProto_TYPE_BOOL:     "boolean",
    45  	protobuf.FieldDescriptorProto_TYPE_DOUBLE:   "number",
    46  	protobuf.FieldDescriptorProto_TYPE_FLOAT:    "number",
    47  	protobuf.FieldDescriptorProto_TYPE_INT64:    "string",
    48  	protobuf.FieldDescriptorProto_TYPE_UINT64:   "string",
    49  	protobuf.FieldDescriptorProto_TYPE_INT32:    "integer",
    50  	protobuf.FieldDescriptorProto_TYPE_FIXED64:  "string",
    51  	protobuf.FieldDescriptorProto_TYPE_FIXED32:  "integer",
    52  	protobuf.FieldDescriptorProto_TYPE_UINT32:   "integer",
    53  	protobuf.FieldDescriptorProto_TYPE_SFIXED32: "integer",
    54  	protobuf.FieldDescriptorProto_TYPE_SFIXED64: "integer",
    55  	protobuf.FieldDescriptorProto_TYPE_SINT32:   "integer",
    56  	protobuf.FieldDescriptorProto_TYPE_SINT64:   "integer",
    57  	protobuf.FieldDescriptorProto_TYPE_ENUM:     "integer",
    58  	protobuf.FieldDescriptorProto_TYPE_STRING:   "string",
    59  	protobuf.FieldDescriptorProto_TYPE_BYTES:    "string",
    60  	protobuf.FieldDescriptorProto_TYPE_MESSAGE:  "object",
    61  }
    62  
    63  // GetFormatStr returns the specific format of a field based on its protobuf type.
    64  func GetFormatStr(t protobuf.FieldDescriptorProto_Type) string {
    65  	if val, ok := FieldDescriptorProtoTypeFormats[t]; ok {
    66  		return val
    67  	}
    68  
    69  	return "string"
    70  }
    71  
    72  // GetTypeStr returns the specific field type according to the protobuf type.
    73  func GetTypeStr(t protobuf.FieldDescriptorProto_Type) string {
    74  	if val, ok := FieldDescriptorProtoTypes[t]; ok {
    75  		return val
    76  	}
    77  
    78  	return "string"
    79  }