♻️ Refactor av data structure

This commit is contained in:
Daniel 2023-07-12 23:52:25 +08:00
parent e66fc9c096
commit ba396a5573
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 36 additions and 29 deletions

View file

@ -41,8 +41,8 @@ type AttributeView struct {
// KeyValues 描述了属性视图属性列值的结构。
type KeyValues struct {
Key *Key `json:"key"` // 属性视图属性列
Values []*Value `json:"values"` // 属性视图属性列值
Key *Key `json:"key"` // 属性视图属性列
Values []*Value `json:"values,omitempty"` // 属性视图属性列值
}
type KeyType string
@ -65,7 +65,7 @@ type Key struct {
// 以下是某些列类型的特有属性
Options []*KeySelectOption `json:"options"` // 选项列表
Options []*KeySelectOption `json:"options,omitempty"` // 选项列表
}
func NewKey(name string, keyType KeyType) *Key {
@ -182,21 +182,29 @@ func NewAttributeView(id string) (ret *AttributeView) {
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
avJSONPath := getAttributeViewDataPath(avID)
toCreate := false
if !gulu.File.IsExist(avJSONPath) {
ret = NewAttributeView(avID)
return
toCreate = true
}
data, err := filelock.ReadFile(avJSONPath)
if nil != err {
logging.LogErrorf("read attribute view [%s] failed: %s", avID, err)
return
}
if !toCreate {
data, readErr := filelock.ReadFile(avJSONPath)
if nil != readErr {
logging.LogErrorf("read attribute view [%s] failed: %s", avID, readErr)
return
}
ret = &AttributeView{}
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
return
ret = &AttributeView{}
if err = gulu.JSON.UnmarshalJSON(data, ret); nil != err {
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
return
}
} else {
if err = SaveAttributeView(ret); nil != err {
logging.LogErrorf("save attribute view [%s] failed: %s", avID, err)
return
}
}
return
}
@ -216,9 +224,9 @@ func SaveAttributeView(av *AttributeView) (err error) {
return
}
func (av *AttributeView) GetView(viewID string) (ret *View, err error) {
func (av *AttributeView) GetView() (ret *View, err error) {
for _, v := range av.Views {
if v.ID == viewID {
if v.ID == av.ViewID {
ret = v
return
}

View file

@ -37,10 +37,10 @@ type LayoutTable struct {
type ViewTableColumn struct {
ID string `json:"id"` // 列 ID
Wrap bool `json:"wrap"` // 是否换行
Hidden bool `json:"hidden"` // 是否隐藏
Width string `json:"width"` // 列宽度
Calc *ColumnCalc `json:"calc"` // 计算
Wrap bool `json:"wrap"` // 是否换行
Hidden bool `json:"hidden"` // 是否隐藏
Width string `json:"width"` // 列宽度
Calc *ColumnCalc `json:"calc,omitempty"` // 计算
}
type Calculable interface {