Skip to content

Commit 398529d

Browse files
[release-18.0] vtctldclient: Apply (Shard | Keyspace| Table) Routing Rules commands don't work (vitessio#16096) (vitessio#16123)
Signed-off-by: Rohit Nayak <[email protected]> Co-authored-by: Rohit Nayak <[email protected]>
1 parent a83eeee commit 398529d

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

go/cmd/vtctldclient/command/routing_rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func commandApplyRoutingRules(cmd *cobra.Command, args []string) error {
8282
}
8383

8484
rr := &vschemapb.RoutingRules{}
85-
if err := json2.Unmarshal(rulesBytes, &rr); err != nil {
85+
if err := json2.UnmarshalPB(rulesBytes, rr); err != nil {
8686
return err
8787
}
8888

go/cmd/vtctldclient/command/shard_routing_rules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func commandApplyShardRoutingRules(cmd *cobra.Command, args []string) error {
8787
}
8888

8989
srr := &vschemapb.ShardRoutingRules{}
90-
if err := json2.Unmarshal(rulesBytes, &srr); err != nil {
90+
if err := json2.UnmarshalPB(rulesBytes, srr); err != nil {
9191
return err
9292
}
9393
// Round-trip so when we display the result it's readable.

go/json2/unmarshal.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ var carriageReturn = []byte("\n")
3333
// efficient and should not be used for high QPS operations.
3434
func Unmarshal(data []byte, v any) error {
3535
if pb, ok := v.(proto.Message); ok {
36-
opts := protojson.UnmarshalOptions{DiscardUnknown: true}
37-
return annotate(data, opts.Unmarshal(data, pb))
36+
return UnmarshalPB(data, pb)
3837
}
3938
return annotate(data, json.Unmarshal(data, v))
4039
}
@@ -53,3 +52,9 @@ func annotate(data []byte, err error) error {
5352

5453
return fmt.Errorf("line: %d, position %d: %v", line, pos, err)
5554
}
55+
56+
// UnmarshalPB is similar to Unmarshal but specifically for proto.Message to add type safety.
57+
func UnmarshalPB(data []byte, pb proto.Message) error {
58+
opts := protojson.UnmarshalOptions{DiscardUnknown: true}
59+
return annotate(data, opts.Unmarshal(data, pb))
60+
}

go/json2/unmarshal_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ package json2
1818

1919
import (
2020
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
"google.golang.org/protobuf/encoding/protojson"
24+
"google.golang.org/protobuf/types/known/emptypb"
2125
)
2226

2327
func TestUnmarshal(t *testing.T) {
@@ -48,3 +52,14 @@ func TestUnmarshal(t *testing.T) {
4852
}
4953
}
5054
}
55+
56+
func TestUnmarshalPB(t *testing.T) {
57+
want := &emptypb.Empty{}
58+
json, err := protojson.Marshal(want)
59+
require.NoError(t, err)
60+
61+
var got emptypb.Empty
62+
err = UnmarshalPB(json, &got)
63+
require.NoError(t, err)
64+
require.Equal(t, want, &got)
65+
}

0 commit comments

Comments
 (0)