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:
2026-10-10 11:06:12 +02:00
parent 1e20ace15c
commit 717d46ec3d
21 changed files with 1617 additions and 73 deletions

View File

@@ -256,42 +256,14 @@ func compileClientAssets(pluginID string, cc *CompiledController, fsys fs.FS) er
return nil
}
id := cc.Controller.ID()
vendor, plugin, found := strings.Cut(pluginID, ".")
if !found || !assetSegment.MatchString(vendor) || !assetSegment.MatchString(plugin) {
if _, _, ok := assetPluginSegments(pluginID); !ok {
return fmt.Errorf("cabana: admin controller %s/%s: plugin ID must be vendor.plugin to serve admin assets", pluginID, id)
}
read := func(files []string, exts ...string) ([]*pluginAsset, error) {
out := make([]*pluginAsset, 0, len(files))
seen := map[string]bool{}
for _, name := range files {
if err := checkAssetPath(name, exts); err != nil {
return nil, bootErr(pluginID, id, name, err)
}
if seen[name] {
return nil, bootErr(pluginID, id, name, fmt.Errorf("asset declared twice"))
}
seen[name] = true
body, err := fs.ReadFile(fsys, name)
if err != nil {
return nil, bootErr(pluginID, id, name, fmt.Errorf("asset is not in the plugin's embedded files: %w", err))
}
sum := sha256.Sum256(body)
digest := hex.EncodeToString(sum[:])
out = append(out, &pluginAsset{
key: vendor + "/" + plugin + "/" + strings.TrimPrefix(name, "assets/"),
body: body,
contentType: boardwalk.ContentType(name),
etag: `"` + digest + `"`,
version: digest[:12],
})
}
return out, nil
}
scripts, err := read(src.AdminJS(), ".js", ".mjs")
scripts, err := readClientAssets(pluginID, id, fsys, src.AdminJS(), ".js", ".mjs")
if err != nil {
return err
}
styles, err := read(src.AdminCSS(), ".css")
styles, err := readClientAssets(pluginID, id, fsys, src.AdminCSS(), ".css")
if err != nil {
return err
}
@@ -299,6 +271,53 @@ func compileClientAssets(pluginID string, cc *CompiledController, fsys fs.FS) er
return nil
}
// assetPluginSegments splits a vendor.plugin ID into the two URL segments of
// its asset paths; ok is false when the ID cannot serve admin assets.
func assetPluginSegments(pluginID string) (vendor, plugin string, ok bool) {
vendor, plugin, found := strings.Cut(pluginID, ".")
if !found || !assetSegment.MatchString(vendor) || !assetSegment.MatchString(plugin) {
return "", "", false
}
return vendor, plugin, true
}
// readClientAssets reads and hashes the declared files of one owner (an
// admin controller ID or "field type <name>") from the plugin's embedded
// tree. Each path must be clean, live under assets/, end in one of exts and
// exist; a file declared twice fails. Errors are boot errors naming the
// plugin, the owner and the file.
func readClientAssets(pluginID, owner string, fsys fs.FS, files []string, exts ...string) ([]*pluginAsset, error) {
vendor, plugin, ok := assetPluginSegments(pluginID)
if !ok {
return nil, fmt.Errorf("cabana: %s %s: plugin ID must be vendor.plugin to serve admin assets", pluginID, owner)
}
out := make([]*pluginAsset, 0, len(files))
seen := map[string]bool{}
for _, name := range files {
if err := checkAssetPath(name, exts); err != nil {
return nil, bootErr(pluginID, owner, name, err)
}
if seen[name] {
return nil, bootErr(pluginID, owner, name, fmt.Errorf("asset declared twice"))
}
seen[name] = true
body, err := fs.ReadFile(fsys, name)
if err != nil {
return nil, bootErr(pluginID, owner, name, fmt.Errorf("asset is not in the plugin's embedded files: %w", err))
}
sum := sha256.Sum256(body)
digest := hex.EncodeToString(sum[:])
out = append(out, &pluginAsset{
key: vendor + "/" + plugin + "/" + strings.TrimPrefix(name, "assets/"),
body: body,
contentType: boardwalk.ContentType(name),
etag: `"` + digest + `"`,
version: digest[:12],
})
}
return out, nil
}
func checkAssetPath(name string, exts []string) error {
if name != path.Clean(name) || !strings.HasPrefix(name, "assets/") || len(name) == len("assets/") {
return fmt.Errorf("asset path must be a clean path under assets/")