Skip to content

Commit d8377c9

Browse files
committed
When adding the _ctx column to table schema, if that column already exists, keep prepending '_' until a unique name is found. Closes #466
1 parent 1bb5471 commit d8377c9

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

plugin/query_data.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,8 @@ func (d *QueryData) buildRowAsync(ctx context.Context, rowData *rowData, rowChan
700700

701701
func (d *QueryData) addContextData(row *proto.Row) {
702702
jsonValue, _ := json.Marshal(map[string]string{"connection_name": d.Connection.Name})
703-
row.Columns[ContextColumnName] = &proto.Column{Value: &proto.Column_JsonValue{JsonValue: jsonValue}}
703+
contextColumnName := contextColumnName(d.Table.columnNameMap())
704+
row.Columns[contextColumnName] = &proto.Column{Value: &proto.Column_JsonValue{JsonValue: jsonValue}}
704705
}
705706

706707
func (d *QueryData) waitForRowsToComplete(rowWg *sync.WaitGroup, rowChan chan *proto.Row) {

plugin/table_schema.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package plugin
22

33
import (
4-
"fmt"
5-
64
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
75
)
86

9-
const ContextColumnName = "_ctx"
7+
func contextColumnName(columns map[string]struct{}) string {
8+
c := "_ctx"
9+
_, columnExists := columns[c]
10+
for columnExists {
11+
c = "_" + c
12+
_, columnExists = columns[c]
13+
}
14+
return c
15+
}
1016

1117
// GetSchema returns the [proto.TableSchema], which defines the columns returned by the table.
1218
//
@@ -22,9 +28,6 @@ func (t Table) GetSchema() (*proto.TableSchema, error) {
2228
// This is therefore a reserved column name
2329
// column schema
2430
for i, column := range t.Columns {
25-
if column.Name == ContextColumnName {
26-
return nil, fmt.Errorf("column '%s' is reserved and may not be used within a plugin schema", ContextColumnName)
27-
}
2831
schema.Columns[i] = &proto.ColumnDefinition{
2932
Name: column.Name,
3033
Type: column.Type,
@@ -33,7 +36,7 @@ func (t Table) GetSchema() (*proto.TableSchema, error) {
3336
}
3437
// add _ctx column
3538
schema.Columns[len(t.Columns)] = &proto.ColumnDefinition{
36-
Name: ContextColumnName,
39+
Name: contextColumnName(t.columnNameMap()),
3740
Type: proto.ColumnType_JSON,
3841
Description: "Steampipe context in JSON form, e.g. connection_name.",
3942
}

0 commit comments

Comments
 (0)