Implement expressions storage on module fields
This commit is contained in:
@@ -61,11 +61,11 @@ func Expression(ctx context.Context, m *types.Module, r *types.Record, old *type
|
||||
}
|
||||
|
||||
for _, f := range m.Fields {
|
||||
if f.Expressions.Value == "" {
|
||||
if f.Expressions.ValueExpr == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
expr := f.Expressions.Value
|
||||
expr := f.Expressions.ValueExpr
|
||||
|
||||
eval, err := exprParser.NewEvaluable(expr)
|
||||
if err != nil {
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestExpressions(t *testing.T) {
|
||||
|
||||
for i := 0; i < len(pairs); i += 3 {
|
||||
f := &types.ModuleField{Name: pairs[i], Kind: pairs[i+1], Options: map[string]interface{}{}}
|
||||
f.Expressions.Value = pairs[i+2]
|
||||
f.Expressions.ValueExpr = pairs[i+2]
|
||||
m.Fields = append(m.Fields, f)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,22 +41,6 @@ type (
|
||||
ModuleID []uint64
|
||||
Deleted filter.State
|
||||
}
|
||||
|
||||
ModuleFieldExpr struct {
|
||||
Value string `json:"value,omitempty"`
|
||||
Sanitizers []string `json:"sanitizers,omitempty"`
|
||||
|
||||
Validators []ModuleFieldValidator `json:"validators,omitempty"`
|
||||
DisableDefaultValidators bool `json:"disableDefaultValidators,omitempty"`
|
||||
|
||||
Formatters []string `json:"formatters,omitempty"`
|
||||
DisableDefaultFormatters bool `json:"disableDefaultFormatters,omitempty"`
|
||||
}
|
||||
|
||||
ModuleFieldValidator struct {
|
||||
Test string `json:"test,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
44
compose/types/module_field_expr.go
Normal file
44
compose/types/module_field_expr.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
ModuleFieldExpr struct {
|
||||
ValueExpr string `json:"value,omitempty"`
|
||||
Sanitizers []string `json:"sanitizers,omitempty"`
|
||||
|
||||
Validators []ModuleFieldValidator `json:"validators,omitempty"`
|
||||
DisableDefaultValidators bool `json:"disableDefaultValidators,omitempty"`
|
||||
|
||||
Formatters []string `json:"formatters,omitempty"`
|
||||
DisableDefaultFormatters bool `json:"disableDefaultFormatters,omitempty"`
|
||||
}
|
||||
|
||||
ModuleFieldValidator struct {
|
||||
Test string `json:"test,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
func (opt *ModuleFieldExpr) Scan(value interface{}) error {
|
||||
//lint:ignore S1034 This typecast is intentional, we need to get []byte out of a []uint8
|
||||
switch value.(type) {
|
||||
case nil:
|
||||
*opt = ModuleFieldExpr{}
|
||||
case []uint8:
|
||||
b := value.([]byte)
|
||||
if err := json.Unmarshal(b, opt); err != nil {
|
||||
return fmt.Errorf("Can not scan '%v' into ModuleFieldExpr: %v", string(b), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (opt ModuleFieldExpr) Value() (driver.Value, error) {
|
||||
return json.Marshal(opt)
|
||||
}
|
||||
@@ -248,6 +248,7 @@ func (s Store) internalComposeModuleFieldRowScanner(row rowScanner) (res *types.
|
||||
&res.Visible,
|
||||
&res.Multi,
|
||||
&res.DefaultValue,
|
||||
&res.Expressions,
|
||||
&res.CreatedAt,
|
||||
&res.UpdatedAt,
|
||||
&res.DeletedAt,
|
||||
@@ -302,6 +303,7 @@ func (Store) composeModuleFieldColumns(aa ...string) []string {
|
||||
alias + "is_visible",
|
||||
alias + "is_multi",
|
||||
alias + "default_value",
|
||||
alias + "expressions",
|
||||
alias + "created_at",
|
||||
alias + "updated_at",
|
||||
alias + "deleted_at",
|
||||
@@ -328,6 +330,7 @@ func (s Store) internalComposeModuleFieldEncoder(res *types.ModuleField) store.P
|
||||
"is_visible": res.Visible,
|
||||
"is_multi": res.Multi,
|
||||
"default_value": res.DefaultValue,
|
||||
"expressions": res.Expressions,
|
||||
"created_at": res.CreatedAt,
|
||||
"updated_at": res.UpdatedAt,
|
||||
"deleted_at": res.DeletedAt,
|
||||
|
||||
@@ -366,7 +366,7 @@ func (g genericUpgrades) AlterComposeModuleRenameJsonToMeta(ctx context.Context)
|
||||
|
||||
func (g genericUpgrades) AlterComposeModuleFieldAddExpresions(ctx context.Context) (err error) {
|
||||
var (
|
||||
col = &ddl.Column{Name: "expressions", Type: ddl.ColumnType{Type: ddl.ColumnTypeJson}, IsNull: true}
|
||||
col = &ddl.Column{Name: "expressions", Type: ddl.ColumnType{Type: ddl.ColumnTypeJson}, IsNull: false}
|
||||
)
|
||||
|
||||
_, err = g.u.AddColumn(ctx, "compose_module_field", col)
|
||||
|
||||
Reference in New Issue
Block a user