From d4d83cccab09859d07575507c1e9ea81e2841d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 2 Jun 2020 09:40:56 +0200 Subject: [PATCH 1/4] Simplify textRows.readRow Refactor textRows.readRow to improve readability: * simplified flow: * reduced maximum indent level * return error early * remove 3 'continue' * remove one type cast * remove one obsolete comment about readLengthEncodedString * -6 lines of code --- packets.go | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/packets.go b/packets.go index 8e2f5e76f..8c0778bef 100644 --- a/packets.go +++ b/packets.go @@ -761,40 +761,34 @@ func (rows *textRows) readRow(dest []driver.Value) error { } // RowSet Packet - var n int - var isNull bool pos := 0 for i := range dest { - // Read bytes and convert to string - dest[i], isNull, n, err = readLengthEncodedString(data[pos:]) + b, isNull, n, err := readLengthEncodedString(data[pos:]) + if err != nil { + return err + } pos += n - if err == nil { - if !isNull { - if !mc.parseTime { + switch { + case isNull: + dest[i] = nil + case mc.parseTime: + switch rows.rs.columns[i].fieldType { + + case fieldTypeTimestamp, fieldTypeDateTime, + fieldTypeDate, fieldTypeNewDate: + + t, err := parseDateTime(b, mc.cfg.Loc) + if err == nil { + dest[i] = t continue - } else { - switch rows.rs.columns[i].fieldType { - case fieldTypeTimestamp, fieldTypeDateTime, - fieldTypeDate, fieldTypeNewDate: - dest[i], err = parseDateTime( - dest[i].([]byte), - mc.cfg.Loc, - ) - if err == nil { - continue - } - default: - continue - } } - - } else { - dest[i] = nil - continue + // If parseDateTime failed, leave as []byte } + fallthrough + default: + dest[i] = b } - return err // err != nil } return nil From 317d8045364d7a1a2a3e788d0093d8e81ba18791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 2 Jun 2020 12:05:12 +0200 Subject: [PATCH 2/4] textRows.readRow: refactor for more early return style --- packets.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/packets.go b/packets.go index 8c0778bef..ccf1cc603 100644 --- a/packets.go +++ b/packets.go @@ -769,25 +769,29 @@ func (rows *textRows) readRow(dest []driver.Value) error { return err } pos += n - switch { - case isNull: + + if isNull { dest[i] = nil - case mc.parseTime: - switch rows.rs.columns[i].fieldType { + continue + } + if !mc.parseTime { + dest[i] = b // type: []byte + continue + } + switch rows.rs.columns[i].fieldType { - case fieldTypeTimestamp, fieldTypeDateTime, - fieldTypeDate, fieldTypeNewDate: + case fieldTypeTimestamp, fieldTypeDateTime, + fieldTypeDate, fieldTypeNewDate: - t, err := parseDateTime(b, mc.cfg.Loc) - if err == nil { - dest[i] = t - continue - } - // If parseDateTime failed, leave as []byte + t, err := parseDateTime(b, mc.cfg.Loc) + if err == nil { + dest[i] = t // type: time.Time + continue } + // If parseDateTime failed, leave as []byte fallthrough default: - dest[i] = b + dest[i] = b // type: []byte } } From a4c78474e8e9e4763cdb4e3f0f1de0d7086c8e76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 2 Jun 2020 13:18:38 +0200 Subject: [PATCH 3/4] textRows.readRow: simplify switch block --- packets.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packets.go b/packets.go index ccf1cc603..9c2934523 100644 --- a/packets.go +++ b/packets.go @@ -789,10 +789,9 @@ func (rows *textRows) readRow(dest []driver.Value) error { continue } // If parseDateTime failed, leave as []byte - fallthrough - default: - dest[i] = b // type: []byte } + + dest[i] = b // type: []byte } return nil From 5ea471c7a4ed84ffa5b2fc1e4ed891b38df9593c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 8 Jun 2020 15:56:01 +0200 Subject: [PATCH 4/4] packets.go: add empty line for readability Co-authored-by: Inada Naoki --- packets.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packets.go b/packets.go index 9c2934523..a3a8fffef 100644 --- a/packets.go +++ b/packets.go @@ -778,8 +778,8 @@ func (rows *textRows) readRow(dest []driver.Value) error { dest[i] = b // type: []byte continue } - switch rows.rs.columns[i].fieldType { + switch rows.rs.columns[i].fieldType { case fieldTypeTimestamp, fieldTypeDateTime, fieldTypeDate, fieldTypeNewDate: