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:
@@ -149,13 +149,34 @@ type formFieldsFile struct {
|
||||
Fields fieldMap `yaml:"fields"`
|
||||
}
|
||||
|
||||
// fieldMap is a fields mapping. types, set before decoding, resolves plugin
|
||||
// field type aliases; nil accepts the built-in types only.
|
||||
type fieldMap struct {
|
||||
items []FormField
|
||||
types *fieldTypeRegistry
|
||||
}
|
||||
|
||||
// CompileForm compiles config_form.yaml and the model fields.yaml it names.
|
||||
// The returned schema is safe to cache: labels stay as source keys.
|
||||
// The returned schema is safe to cache: labels stay as source keys. Only the
|
||||
// built-in field types are accepted; CompileFormWithTypes also accepts plugin
|
||||
// field types.
|
||||
func CompileForm(pluginID string, ctl pact.AdminController, fsys fs.FS) (*FormSchema, error) {
|
||||
return compileForm(pluginID, ctl, fsys, nil)
|
||||
}
|
||||
|
||||
// CompileFormWithTypes is CompileForm with the plugin field types registered
|
||||
// by plugin pluginID (pact.HasFormFieldTypes) accepted as field types. The
|
||||
// aliases, option keys and CheckOptions run as at boot; the element prefix and
|
||||
// the type's scripts and styles are checked when the admin is activated.
|
||||
func CompileFormWithTypes(pluginID string, ctl pact.AdminController, fsys fs.FS, types []pact.FormFieldType) (*FormSchema, error) {
|
||||
reg, err := newFieldTypeRegistry(pluginID, types)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return compileForm(pluginID, ctl, fsys, reg)
|
||||
}
|
||||
|
||||
func compileForm(pluginID string, ctl pact.AdminController, fsys fs.FS, types *fieldTypeRegistry) (*FormSchema, error) {
|
||||
if ctl == nil {
|
||||
return nil, fmt.Errorf("cabana: admin schema %s: controller is nil", pluginID)
|
||||
}
|
||||
@@ -191,7 +212,7 @@ func CompileForm(pluginID string, ctl pact.AdminController, fsys fs.FS) (*FormSc
|
||||
if err != nil {
|
||||
return nil, bootErr(pluginID, ctl.ID(), fieldsPath, err)
|
||||
}
|
||||
fields, err := decodeFields(fieldsRaw)
|
||||
fields, err := decodeFields(fieldsRaw, types)
|
||||
if err != nil {
|
||||
return nil, bootErr(pluginID, ctl.ID(), fieldsPath, err)
|
||||
}
|
||||
@@ -396,9 +417,11 @@ func localizeOptions(ctx context.Context, tr *phrasebook.Translator, src FormFie
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
func decodeFields(raw []byte) ([]FormField, error) {
|
||||
// decodeFields compiles a fields.yaml. types resolves plugin field type
|
||||
// aliases; nil accepts the built-in types only.
|
||||
func decodeFields(raw []byte, types *fieldTypeRegistry) ([]FormField, error) {
|
||||
dec := yaml.NewDecoder(bytes.NewReader(raw), yaml.DisallowUnknownField())
|
||||
var doc formFieldsFile
|
||||
doc := formFieldsFile{Fields: fieldMap{types: types}}
|
||||
if err := dec.Decode(&doc); err != nil {
|
||||
return nil, normalizeYAMLError(err)
|
||||
}
|
||||
@@ -409,7 +432,7 @@ func decodeFields(raw []byte) ([]FormField, error) {
|
||||
}
|
||||
|
||||
func (m *fieldMap) UnmarshalYAML(node ast.Node) error {
|
||||
items, err := decodeFieldMapping(node, nil)
|
||||
items, err := decodeFieldMapping(node, nil, m.types)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -419,8 +442,9 @@ func (m *fieldMap) UnmarshalYAML(node ast.Node) error {
|
||||
|
||||
// decodeFieldMapping compiles a fields mapping in source order. rename, when
|
||||
// set, maps a raw field name (such as WinterCMS's pivot[role]) to the name
|
||||
// that must be an identifier.
|
||||
func decodeFieldMapping(node ast.Node, rename func(string) string) ([]FormField, error) {
|
||||
// that must be an identifier. types resolves plugin field type aliases; nil
|
||||
// accepts the built-in types only.
|
||||
func decodeFieldMapping(node ast.Node, rename func(string) string, types *fieldTypeRegistry) ([]FormField, error) {
|
||||
node = unwrapNode(node)
|
||||
if _, ok := node.(*ast.NullNode); ok || node == nil {
|
||||
return []FormField{}, nil
|
||||
@@ -443,7 +467,7 @@ func decodeFieldMapping(node ast.Node, rename func(string) string) ([]FormField,
|
||||
return nil, fmt.Errorf("duplicate field %s", name)
|
||||
}
|
||||
seen[name] = struct{}{}
|
||||
field, err := compileFieldNode(name, unwrapNode(entry.Value))
|
||||
field, err := compileFieldNode(name, unwrapNode(entry.Value), types)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("field %s: %w", name, err)
|
||||
}
|
||||
@@ -452,18 +476,25 @@ func decodeFieldMapping(node ast.Node, rename func(string) string) ([]FormField,
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func compileFieldNode(name string, node ast.Node) (FormField, error) {
|
||||
func compileFieldNode(name string, node ast.Node, types *fieldTypeRegistry) (FormField, error) {
|
||||
mapping, ok := node.(*ast.MappingNode)
|
||||
if !ok {
|
||||
return FormField{}, fmt.Errorf("must be a mapping")
|
||||
}
|
||||
// A plugin field type accepts the general keys plus its own options, so
|
||||
// the type is resolved before the keys are checked. A built-in type keeps
|
||||
// the check against formFieldKeys.
|
||||
plugin, err := pluginFieldTypeOf(mapping, types)
|
||||
if err != nil {
|
||||
return FormField{}, err
|
||||
}
|
||||
values := map[string]ast.Node{}
|
||||
for _, entry := range mapping.Values {
|
||||
key, err := nodeString(unwrapNode(entry.Key))
|
||||
if err != nil {
|
||||
return FormField{}, err
|
||||
}
|
||||
if _, known := formFieldKeys[key]; !known {
|
||||
if !fieldKeyAllowed(plugin, key) {
|
||||
return FormField{}, fmt.Errorf("unknown field %s", key)
|
||||
}
|
||||
if _, dup := values[key]; dup {
|
||||
@@ -475,7 +506,7 @@ func compileFieldNode(name string, node ast.Node) (FormField, error) {
|
||||
if err != nil || typ == "" {
|
||||
return FormField{}, fmt.Errorf("type is required")
|
||||
}
|
||||
if _, ok := formFieldTypes[typ]; !ok {
|
||||
if _, ok := formFieldTypes[typ]; !ok && plugin == nil {
|
||||
return FormField{}, fmt.Errorf("unsupported type %s", typ)
|
||||
}
|
||||
field := FormField{Name: name, Type: typ}
|
||||
@@ -586,6 +617,11 @@ func compileFieldNode(name string, node ast.Node) (FormField, error) {
|
||||
return FormField{}, fmt.Errorf("options: %w", err)
|
||||
}
|
||||
}
|
||||
if plugin != nil {
|
||||
if err := compilePluginField(plugin, values, &field); err != nil {
|
||||
return FormField{}, err
|
||||
}
|
||||
}
|
||||
return field, nil
|
||||
}
|
||||
|
||||
@@ -915,7 +951,7 @@ func normalizeYAMLError(err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func compileFormIfPresent(pluginID string, ctl pact.AdminController, fsys fs.FS) (*FormSchema, error) {
|
||||
func compileFormIfPresent(pluginID string, ctl pact.AdminController, fsys fs.FS, types *fieldTypeRegistry) (*FormSchema, error) {
|
||||
dir := strings.Trim(path.Clean(ctl.ConfigDir()), "/")
|
||||
if dir == "." || strings.HasPrefix(dir, "..") {
|
||||
return nil, bootErr(pluginID, ctl.ID(), ctl.ConfigDir(), fmt.Errorf("config directory escapes the plugin"))
|
||||
@@ -927,5 +963,5 @@ func compileFormIfPresent(pluginID string, ctl pact.AdminController, fsys fs.FS)
|
||||
}
|
||||
return nil, bootErr(pluginID, ctl.ID(), cfgPath, err)
|
||||
}
|
||||
return CompileForm(pluginID, ctl, fsys)
|
||||
return compileForm(pluginID, ctl, fsys, types)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user