feat(16.2-01): compile, decode and load plugin field types in cabana
- compileFieldTypes builds a per-boot field type registry (alias, core and cross-plugin collisions, element prefix, assets, option keys) passed down the compile functions; settings and relation forms refuse plugin aliases - fields.yaml accepts a plugin alias with the general keys plus the type's options (typeOptions, element, translatable in the schema); CheckOptions runs at boot; CompileFormWithTypes for plugin tests - save decodes plugin values inside the transaction (FormFieldError is a 422, other errors opaque 500) and answers meta.notices (FieldNotice) only when a type reported something; show and save serve values through Load - GET /field-types lists the types with versioned asset URLs; OpenAPI, schema.d.ts, README and docs/backend/field-types.md updated
This commit is contained in:
@@ -57,7 +57,7 @@ func (c relationModelController) NewRecord() any { return c.newRecord() }
|
||||
// to preview them, and the pivot form (pivot.form, belongsToMany only). It
|
||||
// also enforces the toolbar rules that depend on them: create and update
|
||||
// need a manage form, and unlink on a hasMany needs a nullable ForeignKey.
|
||||
func compileRelationForms(pluginID string, ctl pact.AdminController, fsys fs.FS, file string, doc relationDocument, cr *CompiledRelation) error {
|
||||
func compileRelationForms(pluginID string, ctl pact.AdminController, fsys fs.FS, file string, doc relationDocument, cr *CompiledRelation, types *fieldTypeRegistry) error {
|
||||
name := cr.Contract.Name
|
||||
fail := func(err error) error {
|
||||
return bootErr(pluginID, ctl.ID(), file, fmt.Errorf("relation %s: %w", name, err))
|
||||
@@ -80,7 +80,7 @@ func compileRelationForms(pluginID string, ctl pact.AdminController, fsys fs.FS,
|
||||
}
|
||||
cr.Schema.relatedModel = cr.Contract.NewRelated
|
||||
if manageRef != "" {
|
||||
child, err := compileRelationForm(pluginID, ctl, fsys, file, manageRef, cr, "manage", writes)
|
||||
child, err := compileRelationForm(pluginID, ctl, fsys, file, manageRef, cr, "manage", writes, types)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -91,7 +91,7 @@ func compileRelationForms(pluginID string, ctl pact.AdminController, fsys fs.FS,
|
||||
if viewRef != "" {
|
||||
view := cr.child
|
||||
if viewRef != manageRef || view == nil {
|
||||
compiled, err := compileRelationForm(pluginID, ctl, fsys, file, viewRef, cr, "view", false)
|
||||
compiled, err := compileRelationForm(pluginID, ctl, fsys, file, viewRef, cr, "view", false, types)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -109,7 +109,7 @@ func compileRelationForms(pluginID string, ctl pact.AdminController, fsys fs.FS,
|
||||
if cr.hasMany() {
|
||||
return fail(errors.New("pivot.form is only valid on a belongsToMany relation"))
|
||||
}
|
||||
pivot, err := compileRelationForm(pluginID, ctl, fsys, file, ref, cr, "pivot", false)
|
||||
pivot, err := compileRelationForm(pluginID, ctl, fsys, file, ref, cr, "pivot", false, types)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func compileRelationForms(pluginID string, ctl pact.AdminController, fsys fs.FS,
|
||||
// model: the related model for the manage and view forms, the pivot model
|
||||
// for the pivot form. writable additionally requires the model to implement
|
||||
// lagoon.HasFillable and Rules (create and update fill and validate it).
|
||||
func compileRelationForm(pluginID string, ctl pact.AdminController, fsys fs.FS, cfgFile, ref string, cr *CompiledRelation, purpose string, writable bool) (*CompiledController, error) {
|
||||
func compileRelationForm(pluginID string, ctl pact.AdminController, fsys fs.FS, cfgFile, ref string, cr *CompiledRelation, purpose string, writable bool, types *fieldTypeRegistry) (*CompiledController, error) {
|
||||
relation := cr.Contract.Name
|
||||
path, err := assetPath(pluginID, ref)
|
||||
if err != nil {
|
||||
@@ -140,9 +140,9 @@ func compileRelationForm(pluginID string, ctl pact.AdminController, fsys fs.FS,
|
||||
}
|
||||
var fields []FormField
|
||||
if purpose == "pivot" {
|
||||
fields, err = decodePivotFields(raw)
|
||||
fields, err = decodePivotFields(raw, types.refusing())
|
||||
} else {
|
||||
fields, err = decodeFields(raw)
|
||||
fields, err = decodeFields(raw, types.refusing())
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fail(err)
|
||||
@@ -240,10 +240,11 @@ type pivotFieldsFile struct {
|
||||
|
||||
type pivotFieldMap struct {
|
||||
items []FormField
|
||||
types *fieldTypeRegistry
|
||||
}
|
||||
|
||||
func (m *pivotFieldMap) UnmarshalYAML(node ast.Node) error {
|
||||
items, err := decodeFieldMapping(node, pivotFieldName)
|
||||
items, err := decodeFieldMapping(node, pivotFieldName, m.types)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -251,9 +252,9 @@ func (m *pivotFieldMap) UnmarshalYAML(node ast.Node) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodePivotFields(raw []byte) ([]FormField, error) {
|
||||
func decodePivotFields(raw []byte, types *fieldTypeRegistry) ([]FormField, error) {
|
||||
dec := yaml.NewDecoder(bytes.NewReader(raw), yaml.DisallowUnknownField())
|
||||
var doc pivotFieldsFile
|
||||
doc := pivotFieldsFile{Fields: pivotFieldMap{types: types}}
|
||||
if err := dec.Decode(&doc); err != nil {
|
||||
return nil, normalizeYAMLError(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user