Skip to content

Commit 5d3f1fe

Browse files
committed
chore: x-caddy extension type in compose
1 parent 2e585d0 commit 5d3f1fe

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pkg/client/compose/caddy.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package compose
2+
3+
import "fmt"
4+
5+
const CaddyExtensionKey = "x-caddy"
6+
7+
type Caddy struct {
8+
Config string `yaml:"config" json:"config"`
9+
}
10+
11+
// DecodeMapstructure decodes x-caddy extension from either a string or an object.
12+
// When x-caddy is a string, it's mapped directly to the Config field.
13+
func (c *Caddy) DecodeMapstructure(value any) error {
14+
switch v := value.(type) {
15+
case *Caddy:
16+
// Already decoded, happens when mapstructure is called after initial parsing.
17+
*c = *v
18+
return nil
19+
case string:
20+
// Handle x-caddy: "caddyfile config"
21+
*c = Caddy{Config: v}
22+
case map[string]any:
23+
// Handle the long syntax with a config key.
24+
if config, ok := v["config"]; ok {
25+
configStr, ok := config.(string)
26+
if !ok {
27+
return fmt.Errorf("x-caddy.config must be a string, got %T", config)
28+
}
29+
*c = Caddy{Config: configStr}
30+
}
31+
default:
32+
return fmt.Errorf("invalid type %T for x-caddy extension: expected string or object", value)
33+
}
34+
return nil
35+
}

0 commit comments

Comments
 (0)