2019-08-14 18:08:34 +00:00
|
|
|
package table
|
|
|
|
|
|
|
|
import (
|
2019-09-11 21:05:00 +00:00
|
|
|
"github.com/rancher/steve/pkg/attributes"
|
2020-01-31 05:01:21 +00:00
|
|
|
types2 "github.com/rancher/steve/pkg/schemaserver/types"
|
|
|
|
"github.com/rancher/wrangler/pkg/data"
|
|
|
|
types "github.com/rancher/wrangler/pkg/schemas"
|
|
|
|
"github.com/rancher/wrangler/pkg/schemas/mappers"
|
2019-08-14 18:08:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Column struct {
|
2019-08-14 18:16:37 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Field string `json:"field,omitempty"`
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Format string `json:"format,omitempty"`
|
2019-08-14 18:08:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Table struct {
|
|
|
|
Columns []Column
|
|
|
|
Computed func(data.Object)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ColumnMapper struct {
|
|
|
|
definition Table
|
2020-01-31 05:01:21 +00:00
|
|
|
mappers.EmptyMapper
|
2019-08-14 18:08:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
func NewColumns(computed func(data.Object), columns ...Column) *ColumnMapper {
|
|
|
|
return &ColumnMapper{
|
|
|
|
definition: Table{
|
|
|
|
Columns: columns,
|
|
|
|
Computed: computed,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 18:08:34 +00:00
|
|
|
func (t *ColumnMapper) FromInternal(d data.Object) {
|
|
|
|
if t.definition.Computed != nil {
|
|
|
|
t.definition.Computed(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ColumnMapper) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
|
2020-01-31 05:01:21 +00:00
|
|
|
as := &types2.APISchema{
|
|
|
|
Schema: schema,
|
|
|
|
}
|
|
|
|
cols := t.definition.Columns
|
|
|
|
columnObj := attributes.Columns(as)
|
|
|
|
if columns, ok := columnObj.([]Column); ok {
|
|
|
|
cols = append(columns, cols...)
|
|
|
|
}
|
|
|
|
attributes.SetColumns(as, cols)
|
2019-08-14 18:08:34 +00:00
|
|
|
return nil
|
|
|
|
}
|