Skip to content

Commit 408d440

Browse files
committed
chore: Refactor MDX protobuf generation to use the newer opaque api.
1 parent 1078752 commit 408d440

File tree

6 files changed

+179
-91
lines changed

6 files changed

+179
-91
lines changed

build.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ function clean() {
3434
## generate - Generates all required files, before building
3535
function generate() {
3636
get_protoc
37+
38+
pbFile="internal/mdx/metadata_exchange.pb.go"
39+
# Delete the old MDX pb file
40+
rm "$pbFile"
41+
3742
PATH="${SCRIPT_DIR}/.tools/protoc/bin:$PATH" "${SCRIPT_DIR}/.tools/protoc/bin/protoc" \
3843
--proto_path=. \
3944
--go_out=. \
45+
--go_opt=default_api_level=API_OPAQUE \
4046
internal/mdx/metadata_exchange.proto \
4147
--go_opt=paths=source_relative
4248

4349
# Add the copyright header to the generated protobuf file
44-
pbFile="internal/mdx/metadata_exchange.pb.go"
4550
mv "${pbFile}" "${pbFile}.tmp"
4651
cat > "${pbFile}" <<EOF
4752
// Copyright 2025 Google LLC

dialer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,5 +835,5 @@ func newMDXRequest(ci cloudsql.ConnectionInfo, cfg dialConfig, metadataExchangeD
835835
return nil
836836
}
837837

838-
return &mdx.MetadataExchangeRequest{ClientProtocolType: &cpt}
838+
return mdx.MetadataExchangeRequest_builder{ClientProtocolType: &cpt}.Build()
839839
}

dialer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@ func TestNewMDXRequest(t *testing.T) {
17521752
cfg: dialConfig{
17531753
mdxClientProtocolType: cloudsql.ClientProtocolTCP,
17541754
},
1755-
want: &mdx.MetadataExchangeRequest{ClientProtocolType: &tcp},
1755+
want: mdx.MetadataExchangeRequest_builder{ClientProtocolType: &tcp}.Build(),
17561756
},
17571757
{
17581758
desc: "when client protocol is CLIENT_PROTOCOL_UDS",
@@ -1762,7 +1762,7 @@ func TestNewMDXRequest(t *testing.T) {
17621762
cfg: dialConfig{
17631763
mdxClientProtocolType: cloudsql.ClientProtocolUDS,
17641764
},
1765-
want: &mdx.MetadataExchangeRequest{ClientProtocolType: &uds},
1765+
want: mdx.MetadataExchangeRequest_builder{ClientProtocolType: &uds}.Build(),
17661766
},
17671767
{
17681768
desc: "when client protocol is CLIENT_PROTOCOL_TLS",
@@ -1772,7 +1772,7 @@ func TestNewMDXRequest(t *testing.T) {
17721772
cfg: dialConfig{
17731773
mdxClientProtocolType: cloudsql.ClientProtocolTLS,
17741774
},
1775-
want: &mdx.MetadataExchangeRequest{ClientProtocolType: &tlsRes},
1775+
want: mdx.MetadataExchangeRequest_builder{ClientProtocolType: &tlsRes}.Build(),
17761776
},
17771777
}
17781778

internal/cloudsql/metadataexchange.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,19 @@ func (c *MDXConn) readMDX() (*mdx.MetadataExchangeResponse, error) {
196196

197197
// IsResponseOk returns true if a response was received and ResponseStatusCode == OK.
198198
func (c *MDXConn) IsResponseOk() bool {
199-
return c.res.ResponseStatusCode != nil &&
200-
*c.res.ResponseStatusCode == mdx.MetadataExchangeResponse_OK
199+
return c.res.GetResponseStatusCode() == mdx.MetadataExchangeResponse_OK
201200
}
202201

203202
// GetErrorMessage returns the ErrorMessage in the MDX Response.
204203
func (c *MDXConn) GetErrorMessage() string {
205-
if c.res.ErrorMessage != nil {
206-
return *c.res.ErrorMessage
207-
}
208-
return ""
204+
return c.res.GetErrorMessage()
209205
}
210206

211207
// writeMDX writes the MDX response back to the socket.
212208
func (c *MDXConn) writeMDX(req *mdx.MetadataExchangeRequest) error {
213209
// ctx only used for debug logging
214210
ctx := context.Background()
215-
c.logger.Debugf(ctx, "[%v] Writing MDX request, protocol:%v...", c.cn, req.ClientProtocolType)
211+
c.logger.Debugf(ctx, "[%v] Writing MDX request, protocol:%v...", c.cn, req.GetClientProtocolType())
216212

217213
buf := buffPool.get()
218214
defer buffPool.put(buf)

0 commit comments

Comments
 (0)