3
0

Implement expressions storage on module fields

This commit is contained in:
Denis Arh
2020-11-12 09:11:34 +01:00
parent f114d8c625
commit 112a72410c
6 changed files with 51 additions and 20 deletions

View File

@@ -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 {

View File

@@ -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)
}

View File

@@ -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 (

View 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)
}

View File

@@ -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,

View File

@@ -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)