67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package cabana
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/goccy/go-yaml/ast"
|
|
)
|
|
|
|
const (
|
|
codeEditorType = "codeeditor"
|
|
colorPickerType = "colorpicker"
|
|
mailImageType = "mailimage"
|
|
mailPreviewType = "mailpreview"
|
|
)
|
|
|
|
var codeEditorLanguages = map[string]struct{}{
|
|
"html": {}, "css": {}, "text": {},
|
|
}
|
|
|
|
var mailPreviewModes = map[string]struct{}{
|
|
"template": {}, "layout": {}, "partial": {}, "branding": {},
|
|
}
|
|
|
|
// compileMailFieldKeys decodes the mail field keys: language on codeeditor,
|
|
// mailImages on markdown and codeeditor, and mode on mailpreview. They are
|
|
// refused on every other type; a missing required value is a boot error.
|
|
func compileMailFieldKeys(typ string, values map[string]ast.Node, field *FormField) error {
|
|
if node, ok := values["language"]; ok {
|
|
if typ != codeEditorType {
|
|
return fmt.Errorf("language is only valid on type: codeeditor")
|
|
}
|
|
lang, err := nodeString(node)
|
|
if err != nil || lang == "" {
|
|
return fmt.Errorf("language is required on type: codeeditor")
|
|
}
|
|
if _, ok := codeEditorLanguages[lang]; !ok {
|
|
return fmt.Errorf("language %q must be html, css or text", lang)
|
|
}
|
|
field.Language = lang
|
|
} else if typ == codeEditorType {
|
|
return fmt.Errorf("language is required on type: codeeditor")
|
|
}
|
|
|
|
if node, ok := values["mailImages"]; ok {
|
|
if typ != codeEditorType && typ != "markdown" {
|
|
return fmt.Errorf("mailImages is only valid on type: markdown or codeeditor")
|
|
}
|
|
on, err := nodeBool(node)
|
|
if err != nil {
|
|
return fmt.Errorf("mailImages: %w", err)
|
|
}
|
|
field.MailImages = &on
|
|
}
|
|
|
|
if typ == mailPreviewType {
|
|
mode, err := nodeString(values["mode"])
|
|
if err != nil || mode == "" {
|
|
return fmt.Errorf("mode is required on type: mailpreview")
|
|
}
|
|
if _, ok := mailPreviewModes[mode]; !ok {
|
|
return fmt.Errorf("mode %q must be template, layout, partial or branding", mode)
|
|
}
|
|
field.Mode = mode
|
|
}
|
|
return nil
|
|
}
|