Skip to content

Commit 0ada181

Browse files
authored
chore: Refactor MDX protobuf generation to use the newer opaque api. (#1072)
This updates protobuf code generator to generate the newer style opaque api. This is safer to use and more in line with other generated Google API stubs. This does not change the API structure in any way.
1 parent 1078752 commit 0ada181

File tree

7 files changed

+188
-98
lines changed

7 files changed

+188
-98
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)

internal/cloudsql/metadataexchange_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func newFakeConn(bytesToRead []byte) *fakeConn {
6565
}
6666

6767
func newMDXResponseBytes(data []byte) (*mdx.MetadataExchangeResponse, []byte) {
68-
res := &mdx.MetadataExchangeResponse{
68+
res := mdx.MetadataExchangeResponse_builder{
6969
ResponseStatusCode: ptr(mdx.MetadataExchangeResponse_OK),
70-
}
70+
}.Build()
7171
resBytes, err := proto.Marshal(res)
7272
if err != nil {
7373
panic(err)
@@ -82,7 +82,9 @@ func newMDXResponseBytes(data []byte) (*mdx.MetadataExchangeResponse, []byte) {
8282
}
8383

8484
func newMDXRequestBytes(data []byte) (*mdx.MetadataExchangeRequest, []byte) {
85-
req := &mdx.MetadataExchangeRequest{UserAgent: proto.String("hello")}
85+
req := mdx.MetadataExchangeRequest_builder{
86+
UserAgent: proto.String("hello"),
87+
}.Build()
8688
resBytes, err := proto.Marshal(req)
8789
if err != nil {
8890
panic(err)
@@ -195,9 +197,9 @@ func TestMDXConn_Request_Response_WriteRead(t *testing.T) {
195197
if !mdxConn.HasMDXResponse() {
196198
t.Fatalf("expected no MDX response, got response")
197199
}
198-
if *mdxConn.GetMDXResponse().ResponseStatusCode != *res.ResponseStatusCode {
200+
if mdxConn.GetMDXResponse().GetResponseStatusCode() != res.GetResponseStatusCode() {
199201
t.Fatalf("expected status code %v, got %v",
200-
res.ResponseStatusCode, mdxConn.GetMDXResponse().ResponseStatusCode)
202+
res.GetResponseStatusCode(), mdxConn.GetMDXResponse().GetResponseStatusCode())
201203
}
202204
}
203205

@@ -216,9 +218,9 @@ func TestMDXConn_Request_Response_ReadWrite(t *testing.T) {
216218
if !mdxConn.HasMDXResponse() {
217219
t.Fatalf("expected no MDX response, got response")
218220
}
219-
if *mdxConn.GetMDXResponse().ResponseStatusCode != *res.ResponseStatusCode {
221+
if mdxConn.GetMDXResponse().GetResponseStatusCode() != res.GetResponseStatusCode() {
220222
t.Fatalf("expected status code %v, got %v",
221-
res.ResponseStatusCode, mdxConn.GetMDXResponse().ResponseStatusCode)
223+
res.GetResponseStatusCode(), mdxConn.GetMDXResponse().GetResponseStatusCode())
222224
}
223225
}
224226

0 commit comments

Comments
 (0)