♻️ Refactor av data structure

This commit is contained in:
Daniel 2023-07-11 23:40:05 +08:00
parent cea83ad522
commit ad77e4d7f3
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 129 additions and 97 deletions

View file

@ -47,8 +47,8 @@ type View struct {
Name string `json:"name"` // 视图名称
Type ViewType `json:"type"` // 视图类型
Filters []*ViewFilter `json:"filters"` // 过滤规则
Sorts []*ViewSort `json:"sorts"` // 排序规则
Table *Table `json:"table,omitempty"` // 表格视图
// TODO Kanban *Kanban `json:"kanban,omitempty"` // 看板视图
}
// ViewType 描述了视图的类型。
@ -59,6 +59,22 @@ const (
ViewTypeKanban ViewType = "kanban" // 属性视图类型 - 看板
)
func NewView() *View {
id := ast.NewNodeID()
name := "Table"
return &View{
ID: id,
Name: name,
Type: ViewTypeTable,
Table: &Table{
Spec: 0,
ID: id,
Filters: []*ViewFilter{},
Sorts: []*ViewSort{},
},
}
}
// Viewable 描述了视图的接口。
type Viewable interface {
Filterable
@ -70,13 +86,7 @@ type Viewable interface {
}
func NewAttributeView(id string) *AttributeView {
view := &View{
ID: ast.NewNodeID(),
Name: "Table",
Type: ViewTypeTable,
Filters: []*ViewFilter{},
Sorts: []*ViewSort{},
}
view := NewView()
return &AttributeView{
Spec: 0,
@ -108,14 +118,7 @@ func ParseAttributeView(avID string) (ret *AttributeView, err error) {
}
if 1 > len(ret.Views) {
view := &View{
ID: ast.NewNodeID(),
Name: "Table",
Type: ViewTypeTable,
Filters: []*ViewFilter{},
Sorts: []*ViewSort{},
}
view := NewView()
ret.CurrentViewID = view.ID
ret.Views = []*View{view}
}

View file

@ -23,32 +23,24 @@ import (
type ColumnType string
const (
ColumnTypeBlock ColumnType = "block"
ColumnTypeText ColumnType = "text"
ColumnTypeNumber ColumnType = "number"
ColumnTypeDate ColumnType = "date"
ColumnTypeSelect ColumnType = "select"
ColumnTypeMSelect ColumnType = "mSelect"
ColumnTypeRelation ColumnType = "relation"
ColumnTypeRollup ColumnType = "rollup"
ColumnTypeBlock ColumnType = "block"
ColumnTypeText ColumnType = "text"
ColumnTypeNumber ColumnType = "number"
ColumnTypeDate ColumnType = "date"
ColumnTypeSelect ColumnType = "select"
ColumnTypeMSelect ColumnType = "mSelect"
)
// Column 描述了属性视图的基础结构。
type Column struct {
ID string `json:"id"` // 列 ID
Name string `json:"name"` // 列名
Type ColumnType `json:"type"` // 列类型
Icon string `json:"icon"` // 列图标
Wrap bool `json:"wrap"` // 是否换行
Hidden bool `json:"hidden"` // 是否隐藏
Width string `json:"width"` // 列宽度
Calc *ColumnCalc `json:"calc"` // 计算
ID string `json:"id"` // 列 ID
Name string `json:"name"` // 列名
Type ColumnType `json:"type"` // 列类型
Icon string `json:"icon"` // 列图标
// 以下是某些列类型的特有属性
AttributeViewID string `json:"attributeViewId"` // 关联的属性视图 ID
RelationColumnID string `json:"relationColumnId"` // 目标关联列 ID
Options []*ColumnSelectOption `json:"options"` // 选项列表
Options []*ColumnSelectOption `json:"options"` // 选项列表
}
type ColumnSelectOption struct {

View file

@ -23,14 +23,30 @@ import (
// Table 描述了表格视图的结构。
type Table struct {
Spec int `json:"spec"` // 视图格式版本
Spec int `json:"spec"` // 视图格式版本
ID string `json:"id"` // 视图 ID
ID string `json:"id"` // 表格 ID
Name string `json:"name"` // 表格名称
Columns []*Column `json:"columns"` // 表格列
Rows []*Row `json:"rows"` // 表格行
Filters []*ViewFilter `json:"filters"` // 过滤规则
Sorts []*ViewSort `json:"sorts"` // 排序规则
Columns []*TableColumn `json:"columns"` // 表格列
Rows []*TableRow `json:"rows"` // 表格行
Filters []*ViewFilter `json:"filters"` // 过滤规则
Sorts []*ViewSort `json:"sorts"` // 排序规则
}
type TableColumn struct {
ID string `json:"id"` // 列 ID
Name string `json:"name"` // 列名
Type ColumnType `json:"type"` // 列类型
Icon string `json:"icon"` // 列图标
Wrap bool `json:"wrap"` // 是否换行
Hidden bool `json:"hidden"` // 是否隐藏
Width string `json:"width"` // 列宽度
Calc *ColumnCalc `json:"calc"` // 计算
}
type TableRow struct {
ID string `json:"id"`
Cells []*Cell `json:"cells"`
}
func (table *Table) GetType() ViewType {
@ -97,7 +113,7 @@ func (table *Table) FilterRows() {
}
}
rows := []*Row{}
rows := []*TableRow{}
for _, row := range table.Rows {
pass := true
for j, index := range colIndexes {
@ -143,7 +159,7 @@ func (table *Table) CalcCols() {
}
}
func (table *Table) calcColMSelect(col *Column, colIndex int) {
func (table *Table) calcColMSelect(col *TableColumn, colIndex int) {
switch col.Calc.Operator {
case CalcOperatorCountAll:
col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(len(table.Rows))}}
@ -204,7 +220,7 @@ func (table *Table) calcColMSelect(col *Column, colIndex int) {
}
}
func (table *Table) calcColSelect(col *Column, colIndex int) {
func (table *Table) calcColSelect(col *TableColumn, colIndex int) {
switch col.Calc.Operator {
case CalcOperatorCountAll:
col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(len(table.Rows))}}
@ -261,7 +277,7 @@ func (table *Table) calcColSelect(col *Column, colIndex int) {
}
}
func (table *Table) calcColDate(col *Column, colIndex int) {
func (table *Table) calcColDate(col *TableColumn, colIndex int) {
switch col.Calc.Operator {
case CalcOperatorCountAll:
col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(len(table.Rows))}}
@ -360,7 +376,7 @@ func (table *Table) calcColDate(col *Column, colIndex int) {
}
}
func (table *Table) calcColNumber(col *Column, colIndex int) {
func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
switch col.Calc.Operator {
case CalcOperatorCountAll:
col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(len(table.Rows))}}
@ -500,7 +516,7 @@ func (table *Table) calcColNumber(col *Column, colIndex int) {
}
}
func (table *Table) calcColText(col *Column, colIndex int) {
func (table *Table) calcColText(col *TableColumn, colIndex int) {
switch col.Calc.Operator {
case CalcOperatorCountAll:
col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(len(table.Rows))}}