Skip to content

Commit c774e52

Browse files
Add more fields to the marshal output of column (#15622)
Signed-off-by: Manan Gupta <manan@planetscale.com>
1 parent 256a54d commit c774e52

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

go/vt/vtgate/vindexes/vschema.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,22 @@ type Column struct {
203203
// MarshalJSON returns a JSON representation of Column.
204204
func (col *Column) MarshalJSON() ([]byte, error) {
205205
cj := struct {
206-
Name string `json:"name"`
207-
Type string `json:"type,omitempty"`
208-
Invisible bool `json:"invisible,omitempty"`
209-
Default string `json:"default,omitempty"`
206+
Name string `json:"name"`
207+
Type string `json:"type,omitempty"`
208+
Invisible bool `json:"invisible,omitempty"`
209+
Default string `json:"default,omitempty"`
210+
Size int32 `json:"size,omitempty"`
211+
Scale int32 `json:"scale,omitempty"`
212+
Nullable bool `json:"nullable,omitempty"`
213+
Values []string `json:"values,omitempty"`
210214
}{
211-
Name: col.Name.String(),
212-
Type: querypb.Type_name[int32(col.Type)],
213-
}
214-
if col.Invisible {
215-
cj.Invisible = true
215+
Name: col.Name.String(),
216+
Type: querypb.Type_name[int32(col.Type)],
217+
Invisible: col.Invisible,
218+
Size: col.Size,
219+
Scale: col.Scale,
220+
Nullable: col.Nullable,
221+
Values: col.Values,
216222
}
217223
if col.Default != nil {
218224
cj.Default = sqlparser.String(col.Default)

go/vt/vtgate/vindexes/vschema_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,12 @@ func TestVSchemaViews(t *testing.T) {
424424
"columns": [
425425
{
426426
"name": "c1",
427+
"nullable": true,
427428
"type": "NULL_TYPE"
428429
},
429430
{
430431
"name": "c2",
432+
"nullable": true,
431433
"type": "VARCHAR"
432434
}
433435
]
@@ -440,6 +442,51 @@ func TestVSchemaViews(t *testing.T) {
440442
require.JSONEq(t, want, got)
441443
}
442444

445+
func TestColumnMarshal(t *testing.T) {
446+
tests := []struct {
447+
name string
448+
col Column
449+
wanted string
450+
}{
451+
{
452+
name: "Decimal column",
453+
col: Column{
454+
Name: sqlparser.NewIdentifierCI("col1"),
455+
Type: sqltypes.Decimal,
456+
Size: 15,
457+
Scale: 2,
458+
},
459+
wanted: `{"name":"col1", "scale":2, "size":15, "type":"DECIMAL"}`,
460+
},
461+
{
462+
name: "Decimal column with no scale",
463+
col: Column{
464+
Name: sqlparser.NewIdentifierCI("col1"),
465+
Type: sqltypes.Decimal,
466+
Size: 15,
467+
Scale: 0,
468+
},
469+
wanted: `{"name":"col1", "size":15, "type":"DECIMAL"}`,
470+
},
471+
{
472+
name: "enum with values column",
473+
col: Column{
474+
Name: sqlparser.NewIdentifierCI("col1"),
475+
Type: sqltypes.Enum,
476+
Values: []string{"{A", "B\"", "C"},
477+
},
478+
wanted: `{"name":"col1","type":"ENUM","values":["{A","B\"","C"]}`,
479+
},
480+
}
481+
for _, test := range tests {
482+
t.Run(test.name, func(t *testing.T) {
483+
res, err := test.col.MarshalJSON()
484+
require.NoError(t, err)
485+
require.JSONEq(t, test.wanted, string(res), string(res))
486+
})
487+
}
488+
}
489+
443490
func TestVSchemaForeignKeys(t *testing.T) {
444491
good := vschemapb.SrvVSchema{
445492
Keyspaces: map[string]*vschemapb.Keyspace{
@@ -482,10 +529,12 @@ func TestVSchemaForeignKeys(t *testing.T) {
482529
"columns": [
483530
{
484531
"name": "c1",
532+
"nullable": true,
485533
"type": "NULL_TYPE"
486534
},
487535
{
488536
"name": "c2",
537+
"nullable": true,
489538
"type": "VARCHAR"
490539
}
491540
],

0 commit comments

Comments
 (0)