Skip to content

Commit

Permalink
feat: add more fields to the marshal output of column
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 committed Apr 3, 2024
1 parent cef95ab commit 286f327
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
24 changes: 15 additions & 9 deletions go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,22 @@ type Column struct {
// MarshalJSON returns a JSON representation of Column.
func (col *Column) MarshalJSON() ([]byte, error) {
cj := struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Invisible bool `json:"invisible,omitempty"`
Default string `json:"default,omitempty"`
Name string `json:"name"`
Type string `json:"type,omitempty"`
Invisible bool `json:"invisible,omitempty"`
Default string `json:"default,omitempty"`
Size int32 `json:"size,omitempty"`
Scale int32 `json:"scale,omitempty"`
Nullable bool `json:"nullable,omitempty"`
Values []string `json:"values,omitempty"`
}{
Name: col.Name.String(),
Type: querypb.Type_name[int32(col.Type)],
}
if col.Invisible {
cj.Invisible = true
Name: col.Name.String(),
Type: querypb.Type_name[int32(col.Type)],
Invisible: col.Invisible,
Size: col.Size,
Scale: col.Scale,
Nullable: col.Nullable,
Values: col.Values,
}
if col.Default != nil {
cj.Default = sqlparser.String(col.Default)
Expand Down
49 changes: 49 additions & 0 deletions go/vt/vtgate/vindexes/vschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,12 @@ func TestVSchemaViews(t *testing.T) {
"columns": [
{
"name": "c1",
"nullable": true,
"type": "NULL_TYPE"
},
{
"name": "c2",
"nullable": true,
"type": "VARCHAR"
}
]
Expand All @@ -440,6 +442,51 @@ func TestVSchemaViews(t *testing.T) {
require.JSONEq(t, want, got)
}

func TestColumnMarshal(t *testing.T) {
tests := []struct {
name string
col Column
wanted string
}{
{
name: "Decimal column",
col: Column{
Name: sqlparser.NewIdentifierCI("col1"),
Type: sqltypes.Decimal,
Size: 15,
Scale: 2,
},
wanted: `{"name":"col1", "scale":2, "size":15, "type":"DECIMAL"}`,
},
{
name: "Decimal column with no scale",
col: Column{
Name: sqlparser.NewIdentifierCI("col1"),
Type: sqltypes.Decimal,
Size: 15,
Scale: 0,
},
wanted: `{"name":"col1", "size":15, "type":"DECIMAL"}`,
},
{
name: "enum with values column",
col: Column{
Name: sqlparser.NewIdentifierCI("col1"),
Type: sqltypes.Enum,
Values: []string{"{A", "B\"", "C"},
},
wanted: `{"name":"col1","type":"ENUM","values":["{A","B\"","C"]}`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res, err := test.col.MarshalJSON()
require.NoError(t, err)
require.JSONEq(t, test.wanted, string(res), string(res))
})
}
}

func TestVSchemaForeignKeys(t *testing.T) {
good := vschemapb.SrvVSchema{
Keyspaces: map[string]*vschemapb.Keyspace{
Expand Down Expand Up @@ -482,10 +529,12 @@ func TestVSchemaForeignKeys(t *testing.T) {
"columns": [
{
"name": "c1",
"nullable": true,
"type": "NULL_TYPE"
},
{
"name": "c2",
"nullable": true,
"type": "VARCHAR"
}
],
Expand Down

0 comments on commit 286f327

Please sign in to comment.