feat(quick-261115-cha): let a mail template use another plugin's layout by full code

- resolveLayoutLocked: after the plugin's own alias and the built-in
  aliases, a full layout name already in the catalog resolves to itself;
  an unregistered full name fails with a require-the-owner hint
- another plugin's short alias and the short-alias error are unchanged
- TestCrossPluginLayout covers register, send, errors and precedence
- postcard README and docs/services/mail.md describe the rule
This commit is contained in:
Jakub Zych
2026-11-15 09:08:21 +01:00
parent 42ab223e79
commit 1fb4254291
5 changed files with 214 additions and 7 deletions

View File

@@ -73,7 +73,7 @@ type parsedTemplate struct {
pluginID string
subject string
description string
layout string // short name from header; empty means default
layout string // the header's alias or full layout name; after resolveLayoutLocked the full name or the default alias
body string
samples map[string]string // sample.<path> header keys by path
source string // the file's text, CRLF normalized to LF
@@ -224,6 +224,14 @@ func (c *Catalog) Register(pluginID string, fsys fs.FS, templates []string, layo
return nil
}
// resolveLayoutLocked turns the template's layout header value into a full
// layout name. Empty and default give the default alias. Otherwise the value
// is looked up in this order: the registering plugin's own aliases, the
// built-in aliases (system, minimal), then, for a value holding "::", the
// full name of any layout already in the catalog, another plugin's or a
// built-in one. Another plugin's short alias is not visible. A full name no
// registered plugin declares fails: its owner must be in the template
// plugin's Requires(), so it registers first. The caller holds c.mu.
func (c *Catalog) resolveLayoutLocked(t *parsedTemplate) error {
short := strings.TrimSpace(t.layout)
if short == "" || short == defaultLayoutAlias {
@@ -235,6 +243,13 @@ func (c *Catalog) resolveLayoutLocked(t *parsedTemplate) error {
// Not one of the plugin's own aliases: a built-in layout.
full, ok = builtinLayoutAliases[short]
}
if !ok && strings.Contains(short, "::") {
// A full layout name, possibly another plugin's.
if _, registered := c.layouts[short]; !registered {
return fmt.Errorf("postcard: unknown layout %s in %s: no registered plugin declares it (the plugin that owns it must be required, so it registers first)", short, t.name)
}
full, ok = short, true
}
if !ok {
return fmt.Errorf("postcard: unknown layout %s", short)
}