diff --git a/README.md b/README.md index 96f66e7..1e1e961 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,16 @@ Checks for configuration that dioad services, libraries and plugins load: - `configcheck` decodes configuration strictly, rejecting keys that match no field, with the same decode hooks the dioad/cli loader uses, and reports - every problem as a finding with the path of the field it concerns. + every problem as a finding with the path of the field it concerns. It also + holds the checks services repeat for listen addresses, URLs and durations + (`CheckListenAddress`, `IsLoopbackAddress`, `ListenAddressesConflict`, + `CheckURL`, `CheckHTTPURL`, `CheckNonNegative`). +- `configwalk` reads config structs the way the loader does: keys from + `mapstructure` tags with squashed and embedded structs inlined, a walk over + every reachable struct, the defaults a constructor sets, the patterns of + weakly typed scalars, and `KeyNamingProblems`, a test helper that enforces + the key naming rules (kebab-case, no `config` suffix). A schema or example + generator, and the tests over a config type, build on it. It depends only on the standard library, mapstructure and dioad/util, so a library or plugin can check configuration without importing a CLI framework. diff --git a/configcheck/rules.go b/configcheck/rules.go new file mode 100644 index 0000000..aa1d631 --- /dev/null +++ b/configcheck/rules.go @@ -0,0 +1,117 @@ +package configcheck + +import ( + "net" + "net/netip" + "net/url" + "strings" + "time" +) + +// The checks below are the ones every service repeats for its settings: listen +// addresses, URLs and durations. Each reports its findings at the collector it +// is given, which should be positioned at the setting, and each treats an empty +// value as not set, which is not its business to reject. A check mirrors what +// the code that uses the setting does with it, so a config a check passes is +// one that code accepts. + +// CheckListenAddress checks a listen address passed to net.Listen: it must be +// host:port, where the host may be empty to listen on every interface. An empty +// address is not set and is not checked. +func CheckListenAddress(findings *Collector, address string) { + if address == "" { + return + } + if _, port, err := net.SplitHostPort(address); err != nil { + findings.Errorf("%q: %v", address, err) + } else if port == "" { + findings.Errorf("%q has no port", address) + } +} + +// IsLoopbackAddress reports whether a host:port listen address binds only the +// loopback interface. An empty host listens on every interface, so it is not +// loopback; neither is an address that does not parse. +func IsLoopbackAddress(address string) bool { + host, _, err := net.SplitHostPort(address) + if err != nil || host == "" { + return false + } + if strings.EqualFold(host, "localhost") { + return true + } + ip, err := netip.ParseAddr(host) + + return err == nil && ip.IsLoopback() +} + +// ListenAddressesConflict reports whether two host:port listen addresses cannot +// both be bound: they name the same port, on the same host or with a wildcard +// host (empty, 0.0.0.0 or ::) on either side. Port 0 asks the system for a free +// port and never conflicts. Addresses that do not parse do not conflict; +// CheckListenAddress reports those. +func ListenAddressesConflict(a, b string) bool { + hostA, portA, errA := net.SplitHostPort(a) + hostB, portB, errB := net.SplitHostPort(b) + if errA != nil || errB != nil || portA != portB || portA == "" || portA == "0" { + return false + } + + return hostA == hostB || isWildcardHost(hostA) || isWildcardHost(hostB) +} + +func isWildcardHost(host string) bool { + if host == "" { + return true + } + ip, err := netip.ParseAddr(host) + + return err == nil && ip.IsUnspecified() +} + +// CheckURL checks a URL a client dials: it must parse and have a host. Any +// scheme is accepted, since a client may dial any scheme with a host and port. +// An empty URL is not set and is not checked. +func CheckURL(findings *Collector, rawURL string) { + if rawURL == "" { + return + } + u, err := url.Parse(rawURL) + if err != nil { + findings.Errorf("%v", err) + + return + } + if u.Host == "" { + findings.Errorf("%q must be a URL with a host, such as https://example.com/path", rawURL) + } +} + +// CheckHTTPURL checks a URL an HTTP client sends requests to: it must be an +// http or https URL with a host. An empty URL is not set and is not checked. +func CheckHTTPURL(findings *Collector, rawURL string) { + if rawURL == "" { + return + } + u, err := url.Parse(rawURL) + if err != nil { + findings.Errorf("%v", err) + + return + } + if u.Scheme != "http" && u.Scheme != "https" { + findings.Errorf("%q must be an http or https URL", rawURL) + + return + } + if u.Host == "" { + findings.Errorf("%q has no host", rawURL) + } +} + +// CheckNonNegative checks that a duration is not negative. +func CheckNonNegative(findings *Collector, d time.Duration) { + if d < 0 { + findings.Errorf("%v must not be negative", d) + } +} diff --git a/configcheck/rules_test.go b/configcheck/rules_test.go new file mode 100644 index 0000000..55fb61a --- /dev/null +++ b/configcheck/rules_test.go @@ -0,0 +1,130 @@ +package configcheck_test + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/dioad/config/configcheck" +) + +func findingsOf(check func(*configcheck.Collector)) []configcheck.Finding { + c := configcheck.NewCollector() + check(c.Field("x")) + + return c.Findings() +} + +func TestCheckListenAddress(t *testing.T) { + t.Parallel() + + for _, ok := range []string{"", ":1080", "0.0.0.0:1080", "localhost:0", "[::1]:8443"} { + assert.Empty(t, findingsOf(func(c *configcheck.Collector) { configcheck.CheckListenAddress(c, ok) }), ok) + } + assert.Equal(t, []configcheck.Finding{{Path: "x", Message: `"1080": address 1080: missing port in address`}}, + findingsOf(func(c *configcheck.Collector) { configcheck.CheckListenAddress(c, "1080") })) + assert.Equal(t, []configcheck.Finding{{Path: "x", Message: `"localhost:" has no port`}}, + findingsOf(func(c *configcheck.Collector) { configcheck.CheckListenAddress(c, "localhost:") })) +} + +func TestCheckURL(t *testing.T) { + t.Parallel() + + for _, ok := range []string{"", "https://example.com/path", "http://localhost:8080", "tcp://example.com:8119"} { + assert.Empty(t, findingsOf(func(c *configcheck.Collector) { configcheck.CheckURL(c, ok) }), ok) + } + assert.Equal(t, []configcheck.Finding{{Path: "x", Message: `"example.com:8119" must be a URL with a host, such as https://example.com/path`}}, + findingsOf(func(c *configcheck.Collector) { configcheck.CheckURL(c, "example.com:8119") })) +} + +func TestCheckHTTPURL(t *testing.T) { + t.Parallel() + + for _, ok := range []string{"", "https://events.example.com/event", "http://localhost:8120/event"} { + assert.Empty(t, findingsOf(func(c *configcheck.Collector) { configcheck.CheckHTTPURL(c, ok) }), ok) + } + assert.Equal(t, []configcheck.Finding{{Path: "x", Message: `"wss://events.example.com" must be an http or https URL`}}, + findingsOf(func(c *configcheck.Collector) { configcheck.CheckHTTPURL(c, "wss://events.example.com") })) + assert.Equal(t, []configcheck.Finding{{Path: "x", Message: `"https:///event" has no host`}}, + findingsOf(func(c *configcheck.Collector) { configcheck.CheckHTTPURL(c, "https:///event") })) +} + +func TestCheckNonNegative(t *testing.T) { + t.Parallel() + + assert.Empty(t, findingsOf(func(c *configcheck.Collector) { configcheck.CheckNonNegative(c, 0) })) + assert.Equal(t, []configcheck.Finding{{Path: "x", Message: "-1s must not be negative"}}, + findingsOf(func(c *configcheck.Collector) { configcheck.CheckNonNegative(c, -time.Second) })) +} + +func TestIsLoopbackAddress(t *testing.T) { + t.Parallel() + + tests := map[string]bool{ + "127.0.0.1:5555": true, + "[::1]:5555": true, + "localhost:5555": true, + "LOCALHOST:5555": true, + "0.0.0.0:5555": false, + ":5555": false, + "10.0.0.5:5555": false, + "[::]:5555": false, + "example.com:5555": false, + "not-an-address": false, + "": false, + } + for address, want := range tests { + t.Run(address, func(t *testing.T) { + t.Parallel() + assert.Equal(t, want, configcheck.IsLoopbackAddress(address)) + }) + } +} + +func TestListenAddressesConflict(t *testing.T) { + t.Parallel() + + tests := []struct { + a, b string + want bool + }{ + {"0.0.0.0:8443", "0.0.0.0:8443", true}, + {"127.0.0.1:8443", "127.0.0.1:8443", true}, + {"0.0.0.0:8443", "127.0.0.1:8443", true}, + {":8443", "10.0.0.5:8443", true}, + {"[::]:8443", "127.0.0.1:8443", true}, + {"127.0.0.1:8443", "10.0.0.5:8443", false}, + {"0.0.0.0:8443", "0.0.0.0:8444", false}, + {"127.0.0.1:0", "127.0.0.1:0", false}, + {"not-an-address", "not-an-address", false}, + {"", "", false}, + } + for _, tt := range tests { + t.Run(tt.a+" "+tt.b, func(t *testing.T) { + t.Parallel() + assert.Equal(t, tt.want, configcheck.ListenAddressesConflict(tt.a, tt.b)) + assert.Equal(t, tt.want, configcheck.ListenAddressesConflict(tt.b, tt.a), "the check is symmetric") + }) + } +} + +func ExampleCheckListenAddress() { + findings := configcheck.NewCollector() + configcheck.CheckListenAddress(findings.Field("management").Field("listen-address"), "4444") + configcheck.CheckListenAddress(findings.Field("control").Field("listen-address"), ":8119") + + for _, f := range findings.Findings() { + fmt.Println(f) + } + // Output: management.listen-address: "4444": address 4444: missing port in address +} + +func ExampleListenAddressesConflict() { + fmt.Println(configcheck.ListenAddressesConflict(":8443", "10.0.0.5:8443")) + fmt.Println(configcheck.ListenAddressesConflict("127.0.0.1:8443", "10.0.0.5:8443")) + // Output: + // true + // false +} diff --git a/configwalk/configwalk_test.go b/configwalk/configwalk_test.go new file mode 100644 index 0000000..de3edd6 --- /dev/null +++ b/configwalk/configwalk_test.go @@ -0,0 +1,167 @@ +package configwalk_test + +import ( + "fmt" + "reflect" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/dioad/config/configwalk" +) + +type tlsBlock struct { + CertFile string `mapstructure:"cert-file"` +} + +type embedded struct { + Shared string `mapstructure:"shared"` +} + +type rateLimit struct { + RPS float64 `mapstructure:"rate-limit-rps"` +} + +type node struct { + Name string `mapstructure:"name"` + Next *node `mapstructure:"next"` +} + +type sample struct { + embedded + + ListenAddress string `mapstructure:"listen-address"` + Plain string // no tag: keyed by its lower-cased Go name + Drain time.Duration `mapstructure:"drain-delay"` + Origins []string `mapstructure:"cors-allowed-origins"` + TLS tlsBlock `mapstructure:"tls-server"` + Limit rateLimit `mapstructure:"limit,squash"` + Skipped string `mapstructure:"-"` + hidden string //nolint:unused // proves an unexported field is not a key + Labels map[string]tlsBlock `mapstructure:"labels"` +} + +func keys(fields []configwalk.Field) []string { + out := make([]string, 0, len(fields)) + for _, f := range fields { + out = append(out, f.Key) + } + + return out +} + +func TestFields_ReadsKeysTheWayTheLoaderDoes(t *testing.T) { + t.Parallel() + + got := configwalk.Fields(reflect.TypeFor[sample]()) + + assert.Equal(t, []string{"shared", "listen-address", "plain", "drain-delay", "cors-allowed-origins", "tls-server", "rate-limit-rps", "labels"}, keys(got), + "an embedded struct and a squashed one are inlined; ignored and unexported fields are not keys") + assert.Equal(t, reflect.TypeFor[rateLimit](), got[6].Owner, "an inlined field knows its own struct") +} + +func TestWalk_VisitsEachReachableStructOnce(t *testing.T) { + t.Parallel() + var visited []string + + configwalk.Walk(reflect.TypeFor[sample](), func(t reflect.Type, _ []configwalk.Field) { visited = append(visited, t.Name()) }) + + assert.ElementsMatch(t, []string{"sample", "embedded", "tlsBlock", "rateLimit"}, visited, "through fields, squashed structs and map values") + + visited = nil + configwalk.Walk(reflect.TypeFor[node](), func(t reflect.Type, _ []configwalk.Field) { visited = append(visited, t.Name()) }) + assert.Equal(t, []string{"node"}, visited, "a struct that refers to itself is visited once") +} + +func TestSquashedFields_ListsNamedSquashedStructs(t *testing.T) { + t.Parallel() + + got := configwalk.SquashedFields(reflect.TypeFor[sample]()) + + require.Len(t, got, 1) + assert.Equal(t, "Limit", got[0].Name) +} + +func TestDefaults_RecordsWhatTheConstructorSets(t *testing.T) { + t.Parallel() + prototype := &sample{ListenAddress: ":8080", Drain: 5 * time.Second, Origins: []string{"a", "b"}, TLS: tlsBlock{CertFile: "ignored"}, Limit: rateLimit{RPS: 2}} + + assert.Equal(t, map[string]any{ + "listen-address": ":8080", + "drain-delay": "5s", + "cors-allowed-origins": []any{"a", "b"}, + "rate-limit-rps": 2.0, + }, configwalk.Defaults(prototype), "a squashed struct's defaults are the parent's; a nested struct records none") +} + +func TestDefaults_OfANonStructIsNil(t *testing.T) { + t.Parallel() + + assert.Nil(t, configwalk.Defaults(42)) + assert.Nil(t, configwalk.Defaults((*sample)(nil))) +} + +func TestKeyNamingProblems_FindsBadKeysAtAnyDepth(t *testing.T) { + t.Parallel() + + // The keys are wrong on purpose. + type leaf struct { + Good string `json:"good" mapstructure:"good"` + Bad string `json:"bad_config"` + Suffix string `mapstructure:"tls-server-config"` + Camel string `mapstructure:"listenAddress"` //nolint:tagliatelle // deliberately bad + Snake string `mapstructure:"listen_address"` //nolint:tagliatelle // deliberately bad + } + type root struct { + Items []map[string]*leaf `mapstructure:"items"` + } + + problems := configwalk.KeyNamingProblems(reflect.TypeFor[root]()) + + got := make([]string, 0, len(problems)) + for _, p := range problems { + got = append(got, p.String()) + } + assert.ElementsMatch(t, []string{ + `configwalk_test.leaf.Bad: bad_config: ends in "config"; drop the suffix`, + `configwalk_test.leaf.Suffix: tls-server-config: ends in "config"; drop the suffix`, + `configwalk_test.leaf.Camel: listenAddress: is not kebab-case`, + `configwalk_test.leaf.Snake: listen_address: is not kebab-case`, + }, got) +} + +func TestKeyNamingProblems_AcceptsAWellNamedConfig(t *testing.T) { + t.Parallel() + + assert.Empty(t, configwalk.KeyNamingProblems(reflect.TypeFor[sample]())) +} + +func ExampleFields() { + type config struct { + ListenAddress string `mapstructure:"listen-address"` + Limit struct { + RPS float64 `mapstructure:"rate-limit-rps"` + } `mapstructure:"limit,squash"` + } + + for _, f := range configwalk.Fields(reflect.TypeFor[config]()) { + fmt.Println(f.Key) + } + // Output: + // listen-address + // rate-limit-rps +} + +func ExampleKeyNamingProblems() { + type config struct { + TLS string `mapstructure:"tls-server-config"` + Listen string `mapstructure:"listen-address"` + } + + for _, p := range configwalk.KeyNamingProblems(reflect.TypeFor[config]()) { + fmt.Println(p.Key, "-", p.Reason) + } + // Output: tls-server-config - ends in "config"; drop the suffix +} diff --git a/configwalk/defaults.go b/configwalk/defaults.go new file mode 100644 index 0000000..9fcbc0c --- /dev/null +++ b/configwalk/defaults.go @@ -0,0 +1,78 @@ +package configwalk + +import ( + "reflect" + "time" +) + +// Defaults returns the defaults a config type's constructor sets, keyed by the +// keys Fields reports: the value of each scalar field and list of scalars that +// prototype, a struct or a pointer to one, holds non-zero, written as it is in +// a config file (a duration as its string, "5s"). Structs, maps, pointers and +// interfaces record no default. +func Defaults(prototype any) map[string]any { + v := reflect.ValueOf(prototype) + for v.Kind() == reflect.Pointer { + if v.IsNil() { + return nil + } + v = v.Elem() + } + if v.Kind() != reflect.Struct { + return nil + } + + defaults := map[string]any{} + collectDefaults(defaults, v) + + return defaults +} + +func collectDefaults(into map[string]any, v reflect.Value) { + for i := range v.NumField() { + f, fv := v.Type().Field(i), v.Field(i) + if IsIgnored(f) || fv.IsZero() { + continue + } + if IsSquashed(f) || (f.Anonymous && f.Tag.Get("mapstructure") == "") { + if fv.Kind() == reflect.Struct { + collectDefaults(into, fv) + } + + continue + } + if value, ok := fileValue(fv); ok { + into[Key(f)] = value + } + } +} + +// fileValue returns fv as it is written in a config file, if it is a scalar or +// a list of scalars. +func fileValue(fv reflect.Value) (any, bool) { + if d, ok := reflect.TypeAssert[time.Duration](fv); ok { + return d.String(), true + } + // Structs, maps, pointers and interfaces have no default recorded. + //exhaustive:ignore + switch fv.Kind() { + case reflect.String, reflect.Bool, + reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, + reflect.Float32, reflect.Float64: + return fv.Interface(), true + case reflect.Slice: + values := make([]any, 0, fv.Len()) + for i := range fv.Len() { + value, ok := fileValue(fv.Index(i)) + if !ok { + return nil, false + } + values = append(values, value) + } + + return values, true + default: + return nil, false + } +} diff --git a/configwalk/doc.go b/configwalk/doc.go new file mode 100644 index 0000000..80dea69 --- /dev/null +++ b/configwalk/doc.go @@ -0,0 +1,10 @@ +// Package configwalk reads config structs the way the config loader does, so +// the things generated from them (a JSON Schema, an example file, a reference +// page) and the tests over them agree with what is decoded. +// +// A struct's keys are its fields' mapstructure names, lower-cased as viper +// lower-cases keys; a field with no name uses its Go name. mapstructure reads +// the fields of a struct tagged ",squash" from the parent's keys, named field +// or embedded, and an embedded struct with no mapstructure tag likewise, so +// Fields inlines both. The package depends only on the standard library. +package configwalk diff --git a/configwalk/naming.go b/configwalk/naming.go new file mode 100644 index 0000000..4194116 --- /dev/null +++ b/configwalk/naming.go @@ -0,0 +1,67 @@ +package configwalk + +import ( + "fmt" + "reflect" + "regexp" + "strings" +) + +var kebabCase = regexp.MustCompile(`^[a-z][a-z0-9]*(-[a-z0-9]+)*$`) + +// KeyProblem is a config key that breaks the naming rules. +type KeyProblem struct { + // Field names the field as "Type.Field". + Field string + // Key is the offending key. + Key string + // Reason says which rule it breaks. + Reason string +} + +// String returns the problem as "Type.Field: key: reason". +func (p KeyProblem) String() string { + return fmt.Sprintf("%s: %s: %s", p.Field, p.Key, p.Reason) +} + +// KeyNamingProblems checks the keys of every struct reachable from root against +// the naming rules Dioad config files follow, and returns each key that breaks +// one: +// +// - a key must not end in "config", since being in a config already says so: +// "tls-server", not "tls-server-config". This applies to the mapstructure +// key and to the json name of the same field, which names it in an API. +// - a mapstructure key is kebab-case: lower-case words joined by hyphens. +// +// A key may be exempt for a reason; filter the result in the test that calls +// this, keeping the reason beside the exemption. +func KeyNamingProblems(root reflect.Type) []KeyProblem { + var problems []KeyProblem + Walk(root, func(t reflect.Type, _ []Field) { + for i := range t.NumField() { + f := t.Field(i) + if IsIgnored(f) { + continue + } + where := t.String() + "." + f.Name + + if key, _, _ := strings.Cut(f.Tag.Get("mapstructure"), ","); key != "" { + if !kebabCase.MatchString(key) { + problems = append(problems, KeyProblem{where, key, "is not kebab-case"}) + } + if endsInConfig(key) { + problems = append(problems, KeyProblem{where, key, `ends in "config"; drop the suffix`}) + } + } + if key, _, _ := strings.Cut(f.Tag.Get("json"), ","); endsInConfig(key) { + problems = append(problems, KeyProblem{where, key, `ends in "config"; drop the suffix`}) + } + } + }) + + return problems +} + +func endsInConfig(key string) bool { + return strings.HasSuffix(key, "-config") || strings.HasSuffix(key, "_config") +} diff --git a/configwalk/scalar.go b/configwalk/scalar.go new file mode 100644 index 0000000..b08478f --- /dev/null +++ b/configwalk/scalar.go @@ -0,0 +1,18 @@ +package configwalk + +// The patterns of the strings the config loader's weakly typed decoding +// converts to a number or a duration, for a generator that writes the schema of +// a scalar field. mapstructure parses an integer string with strconv.ParseInt +// in base 0, so it also accepts forms such as 0x1f and 1_000; these patterns +// accept decimal integers only. An empty string decodes to zero. +const ( + IntegerPattern = `^([-+]?[0-9]+)?$` + UnsignedPattern = `^(\+?[0-9]+)?$` + NumberPattern = `^([-+]?([0-9]+(\.[0-9]*)?|\.[0-9]+)([eE][-+]?[0-9]+)?)?$` + // DurationPattern matches what time.ParseDuration accepts. + DurationPattern = `^[-+]?(0|(([0-9]+(\.[0-9]*)?|\.[0-9]+)(ns|us|µs|μs|ms|s|m|h))+)$` +) + +// BoolStrings are the strings strconv.ParseBool accepts, which weakly typed +// decoding converts to a bool, and "", which it decodes to false. +var BoolStrings = []any{"", "1", "t", "T", "TRUE", "true", "True", "0", "f", "F", "FALSE", "false", "False"} diff --git a/configwalk/walk.go b/configwalk/walk.go new file mode 100644 index 0000000..b6aaf56 --- /dev/null +++ b/configwalk/walk.go @@ -0,0 +1,135 @@ +package configwalk + +import ( + "reflect" + "slices" + "strings" +) + +// Key returns the key f is read from: its mapstructure name, or its Go name if +// it has none, lower-cased. +func Key(f reflect.StructField) string { + name, _, _ := strings.Cut(f.Tag.Get("mapstructure"), ",") + if name == "" { + name = f.Name + } + + return strings.ToLower(name) +} + +// IsSquashed reports whether f's fields are read from its parent's keys. +func IsSquashed(f reflect.StructField) bool { + _, options, _ := strings.Cut(f.Tag.Get("mapstructure"), ",") + + return slices.Contains(strings.Split(options, ","), "squash") +} + +// IsIgnored reports whether f is not a key: it is unexported and not embedded, +// or tagged `mapstructure:"-"`. +func IsIgnored(f reflect.StructField) bool { + name, _, _ := strings.Cut(f.Tag.Get("mapstructure"), ",") + + return (!f.IsExported() && !f.Anonymous) || name == "-" +} + +// Elem returns the type a field of type t holds values of, looking through +// pointers, lists and maps. +func Elem(t reflect.Type) reflect.Type { + for { + // Only container kinds are looked through. + //exhaustive:ignore + switch t.Kind() { + case reflect.Pointer, reflect.Slice, reflect.Array, reflect.Map: + t = t.Elem() + default: + return t + } + } +} + +// Field is one key of a struct. +type Field struct { + // Key is the key the field is read from. + Key string + // StructField is the field itself. For a field of a squashed or embedded + // struct it is that struct's field, not the field that holds the struct. + StructField reflect.StructField + // Owner is the struct type StructField belongs to. + Owner reflect.Type +} + +// Fields returns the keys of struct type t, in field order. The fields of an +// embedded struct, and of a struct tagged ",squash", are inlined, since they +// are read from t's own keys; a field ignored by IsIgnored is left out. +func Fields(t reflect.Type) []Field { + t = Elem(t) + if t.Kind() != reflect.Struct { + return nil + } + + var fields []Field + for i := range t.NumField() { + f := t.Field(i) + if IsIgnored(f) { + continue + } + if inner := Elem(f.Type); inner.Kind() == reflect.Struct && (IsSquashed(f) || (f.Anonymous && f.Tag.Get("mapstructure") == "")) { + fields = append(fields, Fields(inner)...) + + continue + } + fields = append(fields, Field{Key: Key(f), StructField: f, Owner: t}) + } + + return fields +} + +// SquashedFields returns t's named struct fields tagged ",squash", including +// those of the structs it embeds. Some reflectors inline only embedded structs, +// so a generator can use these to inline the named ones as well. +func SquashedFields(t reflect.Type) []reflect.StructField { + var fields []reflect.StructField + for i := range Elem(t).NumField() { + f := Elem(t).Field(i) + if !f.IsExported() && !f.Anonymous { + continue + } + inner := Elem(f.Type) + if f.Anonymous && (IsSquashed(f) || f.Tag.Get("mapstructure") == "") { + if inner.Kind() == reflect.Struct { + fields = append(fields, SquashedFields(inner)...) + } + + continue + } + if IsSquashed(f) && inner.Kind() == reflect.Struct { + fields = append(fields, f) + } + } + + return fields +} + +// Walk calls visit once for each struct type reachable from root through +// fields, pointers, lists and maps, with the struct's keys as Fields returns +// them. A struct type that refers to itself is visited once. +func Walk(root reflect.Type, visit func(t reflect.Type, fields []Field)) { + seen := map[reflect.Type]bool{} + var walk func(reflect.Type) + walk = func(t reflect.Type) { + t = Elem(t) + if t.Kind() != reflect.Struct || seen[t] { + return + } + seen[t] = true + + visit(t, Fields(t)) + for i := range t.NumField() { + f := t.Field(i) + if !IsIgnored(f) { + walk(f.Type) + } + } + } + walk(root) +}