github.com/GuanceCloud/cliutils@v1.1.21/point/enc.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package point
     7  
     8  import "strings"
     9  
    10  type Encoding int
    11  
    12  const (
    13  	encProtobuf      = "protobuf"
    14  	encProtobufAlias = "v2"
    15  	encJSON          = "json"
    16  	encPBJSON        = "pbjson"
    17  
    18  	encLineprotocolAlias = "v1"
    19  	encLineprotocol      = "line-protocol"
    20  
    21  	contentTypeJSON      = "application/json"
    22  	contentTypeProtobuf  = "application/protobuf; proto=com.guance.Point"
    23  	contentTypePBJSON    = "application/pbjson; proto=com.guance.Point"
    24  	contentTypeLineproto = "application/line-protocol"
    25  )
    26  
    27  const (
    28  	LineProtocol Encoding = iota // encoding in InfluxDB line-protocol
    29  	Protobuf                     // encoding in protobuf
    30  	JSON                         // encoding int simple JSON
    31  	PBJSON                       // encoding in protobuf structured JSON(with better field-type labeled)
    32  )
    33  
    34  // EncodingStr convert encoding-string in configure file to
    35  // encoding enum.
    36  //
    37  // Here v1/v2 are alias for lineprotocol and protobuf, this makes
    38  // people easy to switch between lineprotocol and protobuf. For
    39  // json, you should not configure json encoding in production
    40  // environments(json do not classify int and float).
    41  func EncodingStr(s string) Encoding {
    42  	switch strings.ToLower(s) {
    43  	case encProtobuf, encProtobufAlias:
    44  		return Protobuf
    45  	case encJSON:
    46  		return JSON
    47  	case encPBJSON:
    48  		return PBJSON
    49  	case encLineprotocol,
    50  		encLineprotocolAlias:
    51  		return LineProtocol
    52  	default:
    53  		return LineProtocol
    54  	}
    55  }
    56  
    57  // HTTPContentType detect HTTP body content encoding according to header Content-Type.
    58  func HTTPContentType(ct string) Encoding {
    59  	switch ct {
    60  	case contentTypeJSON:
    61  		return JSON
    62  	case contentTypePBJSON:
    63  		return PBJSON
    64  	case contentTypeProtobuf:
    65  		return Protobuf
    66  	case contentTypeLineproto:
    67  		return LineProtocol
    68  	default: // default use line-protocol to be compatible with lagacy code
    69  		return LineProtocol
    70  	}
    71  }
    72  
    73  // HTTPContentType get correct HTTP Content-Type value on different body encoding.
    74  func (e Encoding) HTTPContentType() string {
    75  	switch e {
    76  	case JSON:
    77  		return contentTypeJSON
    78  	case PBJSON:
    79  		return contentTypePBJSON
    80  	case Protobuf:
    81  		return contentTypeProtobuf
    82  	case LineProtocol:
    83  		return contentTypeLineproto
    84  	default: // default use line-protocol to be compatible with lagacy code
    85  		return contentTypeLineproto
    86  	}
    87  }
    88  
    89  func (e Encoding) String() string {
    90  	switch e {
    91  	case JSON:
    92  		return encJSON
    93  	case PBJSON:
    94  		return encPBJSON
    95  	case Protobuf:
    96  		return encProtobuf
    97  	case LineProtocol:
    98  		return encLineprotocol
    99  	default: // default use line-protocol to be compatible with lagacy code
   100  		return encLineprotocol
   101  	}
   102  }