Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions astra/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,15 @@ func (d *Db) CreateType(ctx context.Context, name string, definition table.UDTDe

// alterTypePayload is the payload for the alterType command.
type alterTypePayload struct {
Name string `json:"name"`
Operation table.AlterTypeOperation `json:"operation"`
Name string
Op table.AlterTypeOperation
}

func (p alterTypePayload) MarshalAstra(_ serdes.EncodeCtx) (any, error) {
return map[string]any{
"name": p.Name,
p.Op.OpKey(): p.Op,
}, nil
}

// AlterType alters an existing user-defined type (UDT) in the database.
Expand All @@ -313,8 +320,8 @@ func (d *Db) AlterType(ctx context.Context, name string, op table.AlterTypeOpera
}

cmd := d.newCmd("alterType", alterTypePayload{
Name: name,
Operation: op,
Name: name,
Op: op,
}, merged.APIOptions)

_, _, _, err = cmd.Execute(ctx)
Expand Down
8 changes: 4 additions & 4 deletions astra/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func TestAlterTypeCommandMarshal(t *testing.T) {
t.Run("add fields", func(t *testing.T) {
cmd := command.NewDataAPICommand("", "", "alterType", alterTypePayload{
Name: "address",
Operation: table.AddTypeFields{
Op: table.AddTypeFields{
Fields: table.Columns{
{Name: "country", Column: table.Text()},
{Name: "zip_code", Column: table.Int()},
Expand All @@ -350,7 +350,7 @@ func TestAlterTypeCommandMarshal(t *testing.T) {
t.Fatalf("failed to serialize: %v", err)
}

expected := `{"alterType":{"name":"address","operation":{"add":{"fields":{"country":{"type":"text"},"zip_code":{"type":"int"}}}}}}`
expected := `{"alterType":{"add":{"fields":{"country":{"type":"text"},"zip_code":{"type":"int"}}},"name":"address"}}`
if string(got) != expected {
t.Errorf("expected %s, got %s", expected, string(got))
}
Expand All @@ -359,7 +359,7 @@ func TestAlterTypeCommandMarshal(t *testing.T) {
t.Run("rename fields", func(t *testing.T) {
cmd := command.NewDataAPICommand("", "", "alterType", alterTypePayload{
Name: "address",
Operation: table.RenameTypeFields{
Op: table.RenameTypeFields{
Fields: map[string]string{
"street": "street_address",
},
Expand All @@ -371,7 +371,7 @@ func TestAlterTypeCommandMarshal(t *testing.T) {
t.Fatalf("failed to serialize: %v", err)
}

expected := `{"alterType":{"name":"address","operation":{"rename":{"fields":{"street":"street_address"}}}}}`
expected := `{"alterType":{"name":"address","rename":{"fields":{"street":"street_address"}}}}`
if string(got) != expected {
t.Errorf("expected %s, got %s", expected, string(got))
}
Expand Down
15 changes: 4 additions & 11 deletions astra/table/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ type RerankService struct {
// AlterTypeOperation represents an operation to alter a user-defined type (UDT).
type AlterTypeOperation interface {
isAlterTypeOp()
serdes.AstraRawMarshaler
// OpKey returns the JSON key used for this operation (e.g. "add", "rename").
OpKey() string
}

// AddTypeFields is the payload for the alterType "add" operation.
Expand All @@ -558,20 +559,12 @@ type AddTypeFields struct {
}

func (a AddTypeFields) isAlterTypeOp() {}

func (a AddTypeFields) MarshalAstraRaw(ctx serdes.EncodeCtx, dst []byte) ([]byte, error) {
type alias AddTypeFields
return serdes.SerializeInto(map[string]any{"add": alias(a)}, ctx.Target, dst, ctx.Flags)
}
func (a AddTypeFields) OpKey() string { return "add" }

// RenameTypeFields is the payload for the alterType "rename" operation.
type RenameTypeFields struct {
Fields map[string]string `json:"fields"`
}

func (r RenameTypeFields) isAlterTypeOp() {}

func (r RenameTypeFields) MarshalAstraRaw(ctx serdes.EncodeCtx, dst []byte) ([]byte, error) {
type alias RenameTypeFields
return serdes.SerializeInto(map[string]any{"rename": alias(r)}, ctx.Target, dst, ctx.Flags)
}
func (r RenameTypeFields) OpKey() string { return "rename" }