fig icon indicating copy to clipboard operation
fig copied to clipboard

Are embedded structs supported?

Open milkpirate opened this issue 1 year ago • 2 comments

I have structs similar to:

type Foo struct {
	Bar string `fig:"bar"`
}

type Config struct {
	*Foo
	Baz string `fig:"baz"`
}

And a config file like so:

# config.yaml
bar: barBar
baz: bazBaz

fig loads the config as follows:

(main.Config) {
 Foo: (*main.Foo)(<nil>),
 Baz: (string) (len=6) "bazBaz"
}

but I would hope to get:

(main.Config) {
 Foo: (*main.Foo)(0xc0000289a0)({
  Bar: (string) (len=6) "barBar"
 }),
 Baz: (string) (len=6) "bazBaz"
}

Is there a workaround or a setting I need to tweak?

milkpirate avatar Dec 02 '24 21:12 milkpirate

I was looking for the same!

coxley avatar Apr 28 '25 16:04 coxley

@milkpirate So because fig uses mitchellh/mapstructure under the hood, you can include ,squash in your configured tag for the field.

type Base struct {
  Env string `fig:"env"`
}

type Config struct {
  // If you override [fig.Tag], then use that instead.
  Base `fig:",squash"`
}

Refs:

  • https://github.com/kkyr/fig/blob/0afa4d5b6cf143c2e1326a6fa9bfe5162e47db6f/fig.go#L193
  • https://pkg.go.dev/github.com/mitchellh/mapstructure#hdr-Embedded_Structs_and_Squashing

coxley avatar Apr 28 '25 16:04 coxley