github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/config/sink_protocol.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package config 15 16 import ( 17 "strings" 18 19 cerror "github.com/pingcap/tiflow/pkg/errors" 20 ) 21 22 const ( 23 // ProtocolKey specifies the key of the protocol in the SinkURI. 24 ProtocolKey = "protocol" 25 ) 26 27 // Protocol is the protocol of the message. 28 type Protocol int 29 30 // Enum types of the Protocol. 31 const ( 32 ProtocolUnknown Protocol = iota 33 ProtocolDefault 34 ProtocolCanal 35 ProtocolAvro 36 ProtocolMaxwell 37 ProtocolCanalJSON 38 ProtocolCraft 39 ProtocolOpen 40 ProtocolCsv 41 ProtocolDebezium 42 ProtocolSimple 43 ) 44 45 // IsBatchEncode returns whether the protocol is a batch encoder. 46 func (p Protocol) IsBatchEncode() bool { 47 return p == ProtocolOpen || p == ProtocolCanal || p == ProtocolMaxwell || p == ProtocolCraft 48 } 49 50 // ParseSinkProtocolFromString converts the protocol from string to Protocol enum type. 51 func ParseSinkProtocolFromString(protocol string) (Protocol, error) { 52 switch strings.ToLower(protocol) { 53 case "default": 54 return ProtocolOpen, nil 55 case "canal": 56 return ProtocolCanal, nil 57 case "avro": 58 return ProtocolAvro, nil 59 case "flat-avro": 60 return ProtocolAvro, nil 61 case "maxwell": 62 return ProtocolMaxwell, nil 63 case "canal-json": 64 return ProtocolCanalJSON, nil 65 case "craft": 66 return ProtocolCraft, nil 67 case "open-protocol": 68 return ProtocolOpen, nil 69 case "csv": 70 return ProtocolCsv, nil 71 case "debezium": 72 return ProtocolDebezium, nil 73 case "simple": 74 return ProtocolSimple, nil 75 default: 76 return ProtocolUnknown, cerror.ErrSinkUnknownProtocol.GenWithStackByArgs(protocol) 77 } 78 } 79 80 // String converts the Protocol enum type string to string. 81 func (p Protocol) String() string { 82 switch p { 83 case ProtocolDefault: 84 return "default" 85 case ProtocolCanal: 86 return "canal" 87 case ProtocolAvro: 88 return "avro" 89 case ProtocolMaxwell: 90 return "maxwell" 91 case ProtocolCanalJSON: 92 return "canal-json" 93 case ProtocolCraft: 94 return "craft" 95 case ProtocolOpen: 96 return "open-protocol" 97 case ProtocolCsv: 98 return "csv" 99 case ProtocolDebezium: 100 return "debezium" 101 case ProtocolSimple: 102 return "simple" 103 default: 104 panic("unreachable") 105 } 106 }