From 5d3f1fe225077ebcf609da9023f36849c14ce832 Mon Sep 17 00:00:00 2001 From: Pasha Sviderski Date: Mon, 11 Aug 2025 21:11:40 +1000 Subject: [PATCH] chore: x-caddy extension type in compose --- pkg/client/compose/caddy.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 pkg/client/compose/caddy.go diff --git a/pkg/client/compose/caddy.go b/pkg/client/compose/caddy.go new file mode 100644 index 00000000..5390d719 --- /dev/null +++ b/pkg/client/compose/caddy.go @@ -0,0 +1,35 @@ +package compose + +import "fmt" + +const CaddyExtensionKey = "x-caddy" + +type Caddy struct { + Config string `yaml:"config" json:"config"` +} + +// DecodeMapstructure decodes x-caddy extension from either a string or an object. +// When x-caddy is a string, it's mapped directly to the Config field. +func (c *Caddy) DecodeMapstructure(value any) error { + switch v := value.(type) { + case *Caddy: + // Already decoded, happens when mapstructure is called after initial parsing. + *c = *v + return nil + case string: + // Handle x-caddy: "caddyfile config" + *c = Caddy{Config: v} + case map[string]any: + // Handle the long syntax with a config key. + if config, ok := v["config"]; ok { + configStr, ok := config.(string) + if !ok { + return fmt.Errorf("x-caddy.config must be a string, got %T", config) + } + *c = Caddy{Config: configStr} + } + default: + return fmt.Errorf("invalid type %T for x-caddy extension: expected string or object", value) + } + return nil +}