Skip to content

Commit

Permalink
refactor(mysql parser related files): add omitempty,flow to json,yaml…
Browse files Browse the repository at this point in the history
… tags in MYSQL

add omitempty,flow to json,yaml tags in MYSQL

Signed-off-by: Hermione Dadheech <hermionedadheech@gmail.com>
  • Loading branch information
Hermione2408 committed Jan 7, 2024
1 parent 57459da commit 89282a9
Show file tree
Hide file tree
Showing 25 changed files with 228 additions and 214 deletions.
225 changes: 118 additions & 107 deletions pkg/models/mysql.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/proxy/integrations/mysqlparser/authMoreData.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type NextAuthPacket struct {
PluginData byte `yaml:"plugin_data"`
PluginData byte `json:"plugin_data,omitempty" yaml:"plugin_data,omitempty"`
}

func decodeAuthMoreData(data []byte) (*NextAuthPacket, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/authSwitchRequestPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

type AuthSwitchRequestPacket struct {
StatusTag byte `yaml:"status_tag"`
PluginName string `yaml:"plugin_name"`
PluginAuthData string `yaml:"plugin_authdata"`
StatusTag byte `json:"status_tag,omitempty" yaml:"status_tag,omitempty"`
PluginName string `json:"plugin_name,omitempty" yaml:"plugin_name,omitempty"`
PluginAuthData string `json:"plugin_authdata,omitempty" yaml:"plugin_authdata,omitempty"`
}

func decodeAuthSwitchRequest(data []byte) (*AuthSwitchRequestPacket, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type AuthSwitchResponsePacket struct {
AuthResponseData string `yaml:"auth_response_data"`
AuthResponseData string `json:"auth_response_data,omitempty" yaml:"auth_response_data,omitempty"`
}

func decodeAuthSwitchResponse(data []byte) (*AuthSwitchResponsePacket, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/proxy/integrations/mysqlparser/comChangeUserPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type ComChangeUserPacket struct {
User string `yaml:"user"`
Auth string `yaml:"auth"`
Db string `yaml:"db"`
CharacterSet uint8 `yaml:"character_set"`
AuthPlugin string `yaml:"auth_plugin"`
User string `json:"user,omitempty" yaml:"user,omitempty,flow"`
Auth string `json:"auth,omitempty" yaml:"auth,omitempty,flow"`
Db string `json:"db,omitempty" yaml:"db,omitempty,flow"`
CharacterSet uint8 `json:"character_set,omitempty" yaml:"character_set,omitempty,flow"`
AuthPlugin string `json:"auth_plugin,omitempty" yaml:"auth_plugin,omitempty,flow"`
}

func decodeComChangeUser(data []byte) (ComChangeUserPacket, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/comFetchPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type ComStmtFetchPacket struct {
StatementID uint32 `yaml:"statement_id"`
RowCount uint32 `yaml:"row_count"`
Info string `yaml:"info"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty"`
RowCount uint32 `json:"row_count,omitempty" yaml:"row_count,omitempty"`
Info string `json:"info,omitempty" yaml:"info,omitempty"`
}

func decodeComStmtFetch(data []byte) (ComStmtFetchPacket, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/integrations/mysqlparser/comInitDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

type ComInitDbPacket struct {
Status byte
DbName string
Status byte `json:"status,omitempty" yaml:"status,omitempty"`
DbName string `json:"db_name,omitempty" yaml:"db_name,omitempty"`
}

func decodeComInitDb(data []byte) (*ComInitDbPacket, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/proxy/integrations/mysqlparser/comStmtCloseMoreData.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
)

type ComStmtPreparePacket1 struct {
Header []byte
Query string
Header []byte `json:"header,omitempty" yaml:"header,omitempty"`
Query string `json:"query,omitempty" yaml:"query,omitempty"`
}

type ComStmtCloseAndPrepare struct {
StmtClose ComStmtClosePacket
StmtPrepare ComStmtPreparePacket1
StmtClose ComStmtClosePacket `json:"stmt_close,omitempty" yaml:"stmt_close,omitempty"`
StmtPrepare ComStmtPreparePacket1 `json:"stmt_prepare,omitempty" yaml:"stmt_prepare,omitempty"`
}

func decodeComStmtCloseMoreData(data []byte) (*ComStmtCloseAndPrepare, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/integrations/mysqlparser/comStmtClosePacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type ComStmtClosePacket struct {
Status byte
StatementID uint32
Status byte `json:"status,omitempty" yaml:"status,omitempty"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty"`
}

func decodeComStmtClose(data []byte) (*ComStmtClosePacket, error) {
Expand Down
14 changes: 7 additions & 7 deletions pkg/proxy/integrations/mysqlparser/comStmtPrepareOk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

type StmtPrepareOk struct {
Status byte `yaml:"status"`
StatementID uint32 `yaml:"statement_id"`
NumColumns uint16 `yaml:"num_columns"`
NumParams uint16 `yaml:"num_params"`
WarningCount uint16 `yaml:"warning_count"`
ColumnDefs []ColumnDefinition `yaml:"column_definitions"`
ParamDefs []ColumnDefinition `yaml:"param_definitions"`
Status byte `json:"status,omitempty" yaml:"status,omitempty,flow"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty,flow"`
NumColumns uint16 `json:"num_columns,omitempty" yaml:"num_columns,omitempty,flow"`
NumParams uint16 `json:"num_params,omitempty" yaml:"num_params,omitempty,flow"`
WarningCount uint16 `json:"warning_count,omitempty" yaml:"warning_count,omitempty,flow"`
ColumnDefs []ColumnDefinition `json:"column_definitions,omitempty" yaml:"column_definitions,omitempty,flow"`
ParamDefs []ColumnDefinition `json:"param_definitions,omitempty" yaml:"param_definitions,omitempty,flow"`
}

func decodeComStmtPrepareOk(data []byte) (*StmtPrepareOk, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/integrations/mysqlparser/comStmtPreparePacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type ComStmtPreparePacket struct {
Query string
Query string `json:"query,omitempty" yaml:"query,omitempty,flow"`
}

func decodeComStmtPrepare(data []byte) (*ComStmtPreparePacket, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/integrations/mysqlparser/comStmtResetPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type COM_STMT_RESET struct {
StatementID uint32 `yaml:"statement_id"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty,flow"`
}

func decodeComStmtReset(packet []byte) (*COM_STMT_RESET, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

type COM_STMT_SEND_LONG_DATA struct {
StatementID uint32 `yaml:"statement_id"`
ParameterID uint16 `yaml:"parameter_id"`
Data string `yaml:"data"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty,flow"`
ParameterID uint16 `json:"parameter_id,omitempty" yaml:"parameter_id,omitempty,flow"`
Data string `json:"data,omitempty" yaml:"data,omitempty,flow"`
}

func decodeComStmtSendLongData(packet []byte) (COM_STMT_SEND_LONG_DATA, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/encryptPassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type PasswordData struct {
PayloadLength uint32 `yaml:"payload_length"`
SequenceID byte `yaml:"sequence_id"`
Payload []byte `yaml:"payload"`
PayloadLength uint32 `json:"payload_length,omitempty" yaml:"payload_length,omitempty,flow"`
SequenceID byte `json:"sequence_id,omitempty" yaml:"sequence_id,omitempty,flow"`
Payload []byte `json:"payload,omitempty" yaml:"payload,omitempty,flow"`
}

func decodeEncryptPassword(data []byte) (string, *PasswordData, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/proxy/integrations/mysqlparser/eofPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type EOFPacket struct {
Header byte `yaml:"header"`
Warnings uint16 `yaml:"warnings"`
StatusFlags uint16 `yaml:"status_flags"`
Header byte `json:"header,omitempty" yaml:"header,omitempty,flow"`
Warnings uint16 `json:"warnings,omitempty" yaml:"warnings,omitempty,flow"`
StatusFlags uint16 `json:"status_flags,omitempty" yaml:"status_flags,omitempty,flow"`
}

func decodeMYSQLEOF(data []byte) (*EOFPacket, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/proxy/integrations/mysqlparser/errPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

type ERRPacket struct {
Header byte `yaml:"header"`
ErrorCode uint16 `yaml:"error_code"`
SQLStateMarker string `yaml:"sql_state_marker"`
SQLState string `yaml:"sql_state"`
ErrorMessage string `yaml:"error_message"`
Header byte `json:"header,omitempty" yaml:"header,omitempty,flow"`
ErrorCode uint16 `json:"error_code,omitempty" yaml:"error_code,omitempty,flow"`
SQLStateMarker string `json:"sql_state_marker,omitempty" yaml:"sql_state_marker,omitempty,flow"`
SQLState string `json:"sql_state,omitempty" yaml:"sql_state,omitempty,flow"`
ErrorMessage string `json:"error_message,omitempty" yaml:"error_message,omitempty,flow"`
}

func decodeMySQLErr(data []byte) (*ERRPacket, error) {
Expand Down
19 changes: 10 additions & 9 deletions pkg/proxy/integrations/mysqlparser/executePacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import (
)

type ComStmtExecute struct {
StatementID uint32 `yaml:"statement_id"`
Flags byte `yaml:"flags"`
IterationCount uint32 `yaml:"iteration_count"`
NullBitmap string `yaml:"null_bitmap"`
ParamCount uint16 `yaml:"param_count"`
Parameters []BoundParameter `yaml:"parameters"`
StatementID uint32 `json:"statement_id,omitempty" yaml:"statement_id,omitempty,flow"`
Flags byte `json:"flags,omitempty" yaml:"flags,omitempty,flow"`
IterationCount uint32 `json:"iteration_count,omitempty" yaml:"iteration_count,omitempty,flow"`
NullBitmap string `json:"null_bitmap,omitempty" yaml:"null_bitmap,omitempty,flow"`
ParamCount uint16 `json:"param_count,omitempty" yaml:"param_count,omitempty,flow"`
Parameters []BoundParameter `json:"parameters,omitempty" yaml:"parameters,omitempty,flow"`
}

type BoundParameter struct {
Type byte `yaml:"type"`
Unsigned byte `yaml:"unsigned"`
Value []byte `yaml:"value"`
Type byte `json:"type,omitempty" yaml:"type,omitempty,flow"`
Unsigned byte `json:"unsigned,omitempty" yaml:"unsigned,omitempty,flow"`
Value []byte `json:"value,omitempty" yaml:"value,omitempty,flow"`
}

func decodeComStmtExecute(packet []byte) (ComStmtExecute, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type HandshakeResponseOk struct {
PacketIndicator string `yaml:"packet_indicator"`
PluginDetails PluginDetails `yaml:"plugin_details"`
RemainingBytes string `yaml:"remaining_bytes"`
PacketIndicator string `json:"packet_indicator,omitempty" yaml:"packet_indicator,omitempty,flow"`
PluginDetails PluginDetails `json:"plugin_details,omitempty" yaml:"plugin_details,omitempty,flow"`
RemainingBytes string `json:"remaining_bytes,omitempty" yaml:"remaining_bytes,omitempty,flow"`
}

func decodeHandshakeResponseOk(data []byte) (*HandshakeResponseOk, error) {
Expand Down
20 changes: 10 additions & 10 deletions pkg/proxy/integrations/mysqlparser/handshakeResponsePacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const (
)

type HandshakeResponse struct {
CapabilityFlags uint32 `yaml:"capability_flags"`
MaxPacketSize uint32 `yaml:"max_packet_size"`
CharacterSet uint8 `yaml:"character_set"`
Reserved int `yaml:"reserved"`
Username string `yaml:"username"`
AuthData string `yaml:"auth_data"`
Database string `yaml:"database"`
AuthPluginName string `yaml:"auth_plugin_name"`
ConnectAttributes map[string]string `yaml:"connect_attributes"`
ZstdCompressionLevel byte `yaml:"zstdcompressionlevel"`
CapabilityFlags uint32 `json:"capability_flags,omitempty" yaml:"capability_flags,omitempty,flow"`
MaxPacketSize uint32 `json:"max_packet_size,omitempty" yaml:"max_packet_size,omitempty,flow"`
CharacterSet uint8 `json:"character_set,omitempty" yaml:"character_set,omitempty,flow"`
Reserved int `json:"reserved,omitempty" yaml:"reserved,omitempty,flow"`
Username string `json:"username,omitempty" yaml:"username,omitempty,flow"`
AuthData string `json:"auth_data,omitempty" yaml:"auth_data,omitempty,flow"`
Database string `json:"database,omitempty" yaml:"database,omitempty,flow"`
AuthPluginName string `json:"auth_plugin_name,omitempty" yaml:"auth_plugin_name,omitempty,flow"`
ConnectAttributes map[string]string `json:"connect_attributes,omitempty" yaml:"connect_attributes,omitempty,flow"`
ZstdCompressionLevel byte `json:"zstdcompressionlevel,omitempty" yaml:"zstdcompressionlevel,omitempty,flow"`
}

func decodeHandshakeResponse(data []byte) (*HandshakeResponse, error) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/proxy/integrations/mysqlparser/handshakeV10Packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
)

type HandshakeV10Packet struct {
ProtocolVersion uint8 `yaml:"protocol_version"`
ServerVersion string `yaml:"server_version"`
ConnectionID uint32 `yaml:"connection_id"`
AuthPluginData string `yaml:"auth_plugin_data"`
CapabilityFlags uint32 `yaml:"capability_flags"`
CharacterSet uint8 `yaml:"character_set"`
StatusFlags uint16 `yaml:"status_flags"`
AuthPluginName string `yaml:"auth_plugin_name"`
ProtocolVersion uint8 `json:"protocol_version,omitempty" yaml:"protocol_version,omitempty,flow"`
ServerVersion string `json:"server_version,omitempty" yaml:"server_version,omitempty,flow"`
ConnectionID uint32 `json:"connection_id,omitempty" yaml:"connection_id,omitempty,flow"`
AuthPluginData string `json:"auth_plugin_data,omitempty" yaml:"auth_plugin_data,omitempty,flow"`
CapabilityFlags uint32 `json:"capability_flags,omitempty" yaml:"capability_flags,omitempty,flow"`
CharacterSet uint8 `json:"character_set,omitempty" yaml:"character_set,omitempty,flow"`
StatusFlags uint16 `json:"status_flags,omitempty" yaml:"status_flags,omitempty,flow"`
AuthPluginName string `json:"auth_plugin_name,omitempty" yaml:"auth_plugin_name,omitempty,flow"`
}

func decodeMySQLHandshakeV10(data []byte) (*HandshakeV10Packet, error) {
Expand Down
13 changes: 6 additions & 7 deletions pkg/proxy/integrations/mysqlparser/mysqlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ var (

func getfirstSQLMock(configMocks []*models.Mock) (*models.Mock, bool) {
for _, mock := range configMocks {
if mock.Kind == "SQL" && mock.Spec.MySqlResponses[0].Header.PacketType == "MySQLHandshakeV10" {
if len(mock.Spec.MySqlResponses) > 0 && mock.Kind == "SQL" && mock.Spec.MySqlResponses[0].Header.PacketType == "MySQLHandshakeV10" {
return mock, true
}
}
Expand All @@ -470,10 +470,9 @@ func decodeOutgoingMySQL(requestBuffer []byte, clientConn, destConn net.Conn, h
doHandshakeAgain := true
prevRequest := ""
var requestBuffers [][]byte

configMocks, _ := h.GetConfigMocks()
tcsMocks, _ := h.GetTcsMocks()
for {
configMocks, _ := h.GetConfigMocks()
tcsMocks, _ := h.GetTcsMocks()

//logger.Debug("Config and TCS Mocks", zap.Any("configMocks", configMocks), zap.Any("tcsMocks", tcsMocks))
if firstLoop || doHandshakeAgain {
Expand Down Expand Up @@ -507,7 +506,7 @@ func decodeOutgoingMySQL(requestBuffer []byte, clientConn, destConn net.Conn, h
if len(configMocks[matchedIndex].Spec.MySqlResponses) == 0 {
configMocks = (append(configMocks[:matchedIndex], configMocks[matchedIndex+1:]...))
}
h.SetConfigMocks(configMocks)
//h.SetConfigMocks(configMocks)
firstLoop = false
doHandshakeAgain = false
logger.Debug("BINARY PACKET SENT HANDSHAKE", zap.ByteString("binaryPacketKey", binaryPacket))
Expand Down Expand Up @@ -656,7 +655,7 @@ func matchRequestWithMock(mysqlRequest models.MySQLRequest, configMocks, tcsMock
if len(configMocks[matchedIndex].Spec.MySqlResponses) == 0 {
configMocks = append(configMocks[:matchedIndex], configMocks[matchedIndex+1:]...)
}
h.SetConfigMocks(configMocks)
//h.SetConfigMocks(configMocks)
} else {
realIndex := matchedIndex - len(configMocks)
if realIndex < 0 || realIndex >= len(tcsMocks) {
Expand All @@ -668,7 +667,7 @@ func matchRequestWithMock(mysqlRequest models.MySQLRequest, configMocks, tcsMock
if len(tcsMocks[realIndex].Spec.MySqlResponses) == 0 {
tcsMocks = append(tcsMocks[:realIndex], tcsMocks[realIndex+1:]...)
}
h.SetTcsMocks(tcsMocks)
//h.SetTcsMocks(tcsMocks)
}

return bestMatch, matchedIndex, mockType, nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/proxy/integrations/mysqlparser/okPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

type OKPacket struct {
AffectedRows uint64 `json:"affected_rows,omitempty" yaml:"affected_rows"`
LastInsertID uint64 `json:"last_insert_id,omitempty" yaml:"last_insert_id"`
StatusFlags uint16 `json:"status_flags,omitempty" yaml:"status_flags"`
Warnings uint16 `json:"warnings,omitempty" yaml:"warnings"`
Info string `json:"info,omitempty" yaml:"info"`
AffectedRows uint64 `json:"affected_rows,omitempty" yaml:"affected_rows,omitempty,flow"`
LastInsertID uint64 `json:"last_insert_id,omitempty" yaml:"last_insert_id,omitempty,flow"`
StatusFlags uint16 `json:"status_flags,omitempty" yaml:"status_flags,omitempty,flow"`
Warnings uint16 `json:"warnings,omitempty" yaml:"warnings,omitempty,flow"`
Info string `json:"info,omitempty" yaml:"info,omitempty,flow"`
}

func decodeMySQLOK(data []byte) (*OKPacket, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/proxy/integrations/mysqlparser/queryPacket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package mysqlparser
import "fmt"

type QueryPacket struct {
Command byte `yaml:"command"`
Query string `yaml:"query"`
Command byte `json:"command,omitempty" yaml:"command,omitempty,flow"`
Query string `json:"query,omitempty" yaml:"query,omitempty,flow"`
}

func decodeMySQLQuery(data []byte) (*QueryPacket, error) {
Expand Down
Loading

0 comments on commit 89282a9

Please sign in to comment.