1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-25 06:22:00 +00:00
steve/pkg/schema/table/mapper.go

57 lines
1.3 KiB
Go
Raw Normal View History

2019-08-14 18:08:34 +00:00
package table
import (
types2 "github.com/rancher/apiserver/pkg/types"
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/attributes"
"github.com/rancher/wrangler/v3/pkg/data"
types "github.com/rancher/wrangler/v3/pkg/schemas"
"github.com/rancher/wrangler/v3/pkg/schemas/mappers"
2019-08-14 18:08:34 +00:00
)
type Column struct {
2020-02-08 20:04:20 +00:00
Name string `json:"name,omitempty"`
Field string `json:"field,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Description string `json:"description,omitempty"`
Priority int `json:"priority,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
}