1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-28 15:57:24 +00:00
steve/pkg/schema/table/mapper.go
Darren Shepherd 19c6732de0 Vendor
2020-01-30 22:01:21 -07:00

55 lines
1.2 KiB
Go

package table
import (
"github.com/rancher/steve/pkg/attributes"
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"
)
type Column struct {
Name string `json:"name,omitempty"`
Field string `json:"field,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
}
type Table struct {
Columns []Column
Computed func(data.Object)
}
type ColumnMapper struct {
definition Table
mappers.EmptyMapper
}
func NewColumns(computed func(data.Object), columns ...Column) *ColumnMapper {
return &ColumnMapper{
definition: Table{
Columns: columns,
Computed: computed,
},
}
}
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 {
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)
return nil
}