Skip to content

Commit b905b5e

Browse files
authored
Remove redundant copy in ParseToProtobuf, require Go 1.20+ (#139)
1 parent d64beeb commit b905b5e

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ jobs:
1313
matrix:
1414
go: ['1.20', '1.21']
1515
os: ['ubuntu-latest', 'windows-latest']
16-
include:
17-
- go: 1.16 # Oldest Go version still tested (Windows requires 1.20+ due to https://github.com/golang/go/issues/57455)
18-
os: ubuntu-latest
1916
runs-on: ${{ matrix.os }}
2017
steps:
2118
- name: Set up Go 1.x

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module github.com/pganalyze/pg_query_go/v6
22

3-
go 1.14
3+
go 1.20
44

55
require (
66
github.com/google/go-cmp v0.5.5
77
google.golang.org/protobuf v1.31.0
88
)
9+
10+
require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect

parser/parser.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func ScanToProtobuf(input string) (result []byte, err error) {
101101
}
102102

103103
// ParseToProtobuf - Parses the given SQL statement into a parse tree (Protobuf format)
104-
func ParseToProtobuf(input string) (result []byte, err error) {
104+
func ParseToProtobuf(input string) ([]byte, error) {
105105
inputC := C.CString(input)
106106
defer C.free(unsafe.Pointer(inputC))
107107

@@ -110,13 +110,10 @@ func ParseToProtobuf(input string) (result []byte, err error) {
110110
defer C.pg_query_free_protobuf_parse_result(resultC)
111111

112112
if resultC.error != nil {
113-
err = newPgQueryError(resultC.error)
114-
return
113+
return nil, newPgQueryError(resultC.error)
115114
}
116115

117-
result = []byte(C.GoStringN(resultC.parse_tree.data, C.int(resultC.parse_tree.len)))
118-
119-
return
116+
return toBytes(C.GoStringN(resultC.parse_tree.data, C.int(resultC.parse_tree.len))), nil
120117
}
121118

122119
// DeparseFromProtobuf - Deparses the given Protobuf format parse tree into a SQL statement

parser/string_to_bytes.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build go1.20
2+
// +build go1.20
3+
4+
package parser
5+
6+
import "unsafe"
7+
8+
func toBytes(s string) (b []byte) {
9+
if s == "" {
10+
return nil
11+
}
12+
13+
return unsafe.Slice(unsafe.StringData(s), len(s))
14+
}

0 commit comments

Comments
 (0)