package rest import ( "database/sql/driver" "encoding/json" "errors" ) var ( errDbTypeUnsupported = errors.New("database type unsupported") ) const ( ScenarioCreate = "create" ScenarioUpdate = "update" ScenarioDelete = "delete" ScenarioSearch = "search" ScenarioExport = "export" ScenarioList = "list" ScenarioView = "view" ScenarioMapping = "mapping" ) const ( MatchExactly = "exactly" //精确匹配 MatchFuzzy = "fuzzy" //模糊匹配 ) const ( LiveTypeDropdown = "dropdown" LiveTypeCascader = "cascader" ) const ( TypeInteger = "integer" TypeFloat = "float" TypeBoolean = "boolean" TypeString = "string" ) const ( FormatInteger = "integer" FormatFloat = "float" FormatBoolean = "boolean" FormatString = "string" FormatText = "text" FormatDropdown = "dropdown" FormatDatetime = "datetime" FormatDate = "date" FormatTime = "time" FormatTimestamp = "timestamp" FormatPassword = "password" ) type ( LiveValue struct { Enable bool `json:"enable"` Type string `json:"type"` Url string `json:"url"` Method string `json:"method"` Body string `json:"body"` ContentType string `json:"content_type"` Columns []string `json:"columns"` } EnumValue struct { Label string `json:"label"` Value string `json:"value"` Color string `json:"color"` } VisibleCondition struct { Column string `json:"column"` Values []interface{} `json:"values"` } Rule struct { Min int `json:"min"` Max int `json:"max"` Type string `json:"type"` Unique bool `json:"unique"` Required []string `json:"required"` Regular string `json:"regular"` } Attribute struct { Match string `json:"match"` //匹配模式 PrimaryKey bool `json:"primary_key"` //是否为主键 DefaultValue string `json:"default_value"` //默认值 Readonly []string `json:"readonly"` //只读场景 Disable []string `json:"disable"` //禁用场景 Visible []VisibleCondition `json:"visible"` //可见条件 Values []EnumValue `json:"values"` //值 Live LiveValue `json:"live"` //延时加载配置 Icon string `json:"icon"` //显示图标 Sort bool `json:"sort"` //是否允许排序 Suffix string `json:"suffix"` //追加内容 Tooltip string `json:"tooltip"` //字段提示信息 Description string `json:"description"` //字段说明信息 } Scenarios []string Schema struct { Id uint64 `json:"id" gorm:"primary_key"` CreatedAt int64 `json:"created_at" gorm:"autoCreateTime"` //创建时间 UpdatedAt int64 `json:"updated_at" gorm:"autoUpdateTime"` //更新时间 Namespace string `json:"namespace" gorm:"column:namespace;type:char(60);index"` //域 ModuleName string `json:"module_name" gorm:"column:module_name;type:varchar(60);index"` //模块名称 TableName string `json:"table_name" gorm:"column:table_name;type:varchar(120);index"` //表名称 Enable uint8 `json:"enable" gorm:"column:enable;type:int(1)"` //是否启用 Column string `json:"column" gorm:"type:varchar(120)"` //字段名称 Label string `json:"label" gorm:"type:varchar(120)"` //显示名称 Type string `json:"type" gorm:"type:varchar(120)"` //字段类型 Format string `json:"format" gorm:"type:varchar(120)"` //字段格式 Native uint8 `json:"native" gorm:"type:int(1)"` //是否为原生字段 IsPrimaryKey uint8 `json:"is_primary_key" gorm:"type:int(1)"` //是否为主键 Expression string `json:"expression" gorm:"type:varchar(526)"` //计算规则 Scenarios Scenarios `json:"scenarios" gorm:"type:varchar(120)"` //场景 Rule Rule `json:"rule" gorm:"type:varchar(2048)"` //字段规则 Attribute Attribute `json:"attribute" gorm:"type:varchar(4096)"` //字段属性 Position int `json:"position"` //字段排序位置 } ) func (n Scenarios) Has(str string) bool { for _, v := range n { if v == str { return true } } return false } // Scan implements the Scanner interface. func (n *Scenarios) Scan(value interface{}) error { if value == nil { return nil } switch s := value.(type) { case string: return json.Unmarshal([]byte(s), n) case []byte: return json.Unmarshal(s, n) } return errDbTypeUnsupported } // Value implements the driver Valuer interface. func (n Scenarios) Value() (driver.Value, error) { return json.Marshal(n) } // Scan implements the Scanner interface. func (n *Attribute) Scan(value interface{}) error { if value == nil { return nil } switch s := value.(type) { case string: return json.Unmarshal([]byte(s), n) case []byte: return json.Unmarshal(s, n) } return errDbTypeUnsupported } // Value implements the driver Valuer interface. func (n Attribute) Value() (driver.Value, error) { return json.Marshal(n) } // Scan implements the Scanner interface. func (n *Rule) Scan(value interface{}) error { if value == nil { return nil } switch s := value.(type) { case string: return json.Unmarshal([]byte(s), n) case []byte: return json.Unmarshal(s, n) } return errDbTypeUnsupported } // Value implements the driver Valuer interface. func (n Rule) Value() (driver.Value, error) { return json.Marshal(n) }