blob: abbd2a63b69a935608a1db7fdb395a125561e525 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001// Copyright 2017, The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE.md file.
4
5package cmp
6
7import (
8 "fmt"
9 "reflect"
Pragya Arya324337e2020-02-20 14:35:08 +053010 "regexp"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070011 "strings"
12
13 "github.com/google/go-cmp/cmp/internal/function"
14)
15
16// Option configures for specific behavior of Equal and Diff. In particular,
17// the fundamental Option functions (Ignore, Transformer, and Comparer),
18// configure how equality is determined.
19//
20// The fundamental options may be composed with filters (FilterPath and
21// FilterValues) to control the scope over which they are applied.
22//
23// The cmp/cmpopts package provides helper functions for creating options that
24// may be used with Equal and Diff.
25type Option interface {
26 // filter applies all filters and returns the option that remains.
27 // Each option may only read s.curPath and call s.callTTBFunc.
28 //
29 // An Options is returned only if multiple comparers or transformers
30 // can apply simultaneously and will only contain values of those types
31 // or sub-Options containing values of those types.
Pragya Arya324337e2020-02-20 14:35:08 +053032 filter(s *state, t reflect.Type, vx, vy reflect.Value) applicableOption
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070033}
34
35// applicableOption represents the following types:
Pragya Arya324337e2020-02-20 14:35:08 +053036// Fundamental: ignore | validator | *comparer | *transformer
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070037// Grouping: Options
38type applicableOption interface {
39 Option
40
41 // apply executes the option, which may mutate s or panic.
42 apply(s *state, vx, vy reflect.Value)
43}
44
45// coreOption represents the following types:
Pragya Arya324337e2020-02-20 14:35:08 +053046// Fundamental: ignore | validator | *comparer | *transformer
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070047// Filters: *pathFilter | *valuesFilter
48type coreOption interface {
49 Option
50 isCore()
51}
52
53type core struct{}
54
55func (core) isCore() {}
56
57// Options is a list of Option values that also satisfies the Option interface.
58// Helper comparison packages may return an Options value when packing multiple
59// Option values into a single Option. When this package processes an Options,
60// it will be implicitly expanded into a flat list.
61//
62// Applying a filter on an Options is equivalent to applying that same filter
63// on all individual options held within.
64type Options []Option
65
Pragya Arya324337e2020-02-20 14:35:08 +053066func (opts Options) filter(s *state, t reflect.Type, vx, vy reflect.Value) (out applicableOption) {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070067 for _, opt := range opts {
Pragya Arya324337e2020-02-20 14:35:08 +053068 switch opt := opt.filter(s, t, vx, vy); opt.(type) {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070069 case ignore:
70 return ignore{} // Only ignore can short-circuit evaluation
Pragya Arya324337e2020-02-20 14:35:08 +053071 case validator:
72 out = validator{} // Takes precedence over comparer or transformer
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070073 case *comparer, *transformer, Options:
74 switch out.(type) {
75 case nil:
76 out = opt
Pragya Arya324337e2020-02-20 14:35:08 +053077 case validator:
78 // Keep validator
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070079 case *comparer, *transformer, Options:
80 out = Options{out, opt} // Conflicting comparers or transformers
81 }
82 }
83 }
84 return out
85}
86
87func (opts Options) apply(s *state, _, _ reflect.Value) {
88 const warning = "ambiguous set of applicable options"
89 const help = "consider using filters to ensure at most one Comparer or Transformer may apply"
90 var ss []string
91 for _, opt := range flattenOptions(nil, opts) {
92 ss = append(ss, fmt.Sprint(opt))
93 }
94 set := strings.Join(ss, "\n\t")
95 panic(fmt.Sprintf("%s at %#v:\n\t%s\n%s", warning, s.curPath, set, help))
96}
97
98func (opts Options) String() string {
99 var ss []string
100 for _, opt := range opts {
101 ss = append(ss, fmt.Sprint(opt))
102 }
103 return fmt.Sprintf("Options{%s}", strings.Join(ss, ", "))
104}
105
106// FilterPath returns a new Option where opt is only evaluated if filter f
107// returns true for the current Path in the value tree.
108//
Pragya Arya324337e2020-02-20 14:35:08 +0530109// This filter is called even if a slice element or map entry is missing and
110// provides an opportunity to ignore such cases. The filter function must be
111// symmetric such that the filter result is identical regardless of whether the
112// missing value is from x or y.
113//
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700114// The option passed in may be an Ignore, Transformer, Comparer, Options, or
115// a previously filtered Option.
116func FilterPath(f func(Path) bool, opt Option) Option {
117 if f == nil {
118 panic("invalid path filter function")
119 }
120 if opt := normalizeOption(opt); opt != nil {
121 return &pathFilter{fnc: f, opt: opt}
122 }
123 return nil
124}
125
126type pathFilter struct {
127 core
128 fnc func(Path) bool
129 opt Option
130}
131
Pragya Arya324337e2020-02-20 14:35:08 +0530132func (f pathFilter) filter(s *state, t reflect.Type, vx, vy reflect.Value) applicableOption {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700133 if f.fnc(s.curPath) {
Pragya Arya324337e2020-02-20 14:35:08 +0530134 return f.opt.filter(s, t, vx, vy)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700135 }
136 return nil
137}
138
139func (f pathFilter) String() string {
Pragya Arya324337e2020-02-20 14:35:08 +0530140 return fmt.Sprintf("FilterPath(%s, %v)", function.NameOf(reflect.ValueOf(f.fnc)), f.opt)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700141}
142
143// FilterValues returns a new Option where opt is only evaluated if filter f,
144// which is a function of the form "func(T, T) bool", returns true for the
Pragya Arya324337e2020-02-20 14:35:08 +0530145// current pair of values being compared. If either value is invalid or
146// the type of the values is not assignable to T, then this filter implicitly
147// returns false.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700148//
149// The filter function must be
150// symmetric (i.e., agnostic to the order of the inputs) and
151// deterministic (i.e., produces the same result when given the same inputs).
152// If T is an interface, it is possible that f is called with two values with
153// different concrete types that both implement T.
154//
155// The option passed in may be an Ignore, Transformer, Comparer, Options, or
156// a previously filtered Option.
157func FilterValues(f interface{}, opt Option) Option {
158 v := reflect.ValueOf(f)
159 if !function.IsType(v.Type(), function.ValueFilter) || v.IsNil() {
160 panic(fmt.Sprintf("invalid values filter function: %T", f))
161 }
162 if opt := normalizeOption(opt); opt != nil {
163 vf := &valuesFilter{fnc: v, opt: opt}
164 if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
165 vf.typ = ti
166 }
167 return vf
168 }
169 return nil
170}
171
172type valuesFilter struct {
173 core
174 typ reflect.Type // T
175 fnc reflect.Value // func(T, T) bool
176 opt Option
177}
178
Pragya Arya324337e2020-02-20 14:35:08 +0530179func (f valuesFilter) filter(s *state, t reflect.Type, vx, vy reflect.Value) applicableOption {
180 if !vx.IsValid() || !vx.CanInterface() || !vy.IsValid() || !vy.CanInterface() {
181 return nil
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700182 }
183 if (f.typ == nil || t.AssignableTo(f.typ)) && s.callTTBFunc(f.fnc, vx, vy) {
Pragya Arya324337e2020-02-20 14:35:08 +0530184 return f.opt.filter(s, t, vx, vy)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700185 }
186 return nil
187}
188
189func (f valuesFilter) String() string {
Pragya Arya324337e2020-02-20 14:35:08 +0530190 return fmt.Sprintf("FilterValues(%s, %v)", function.NameOf(f.fnc), f.opt)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700191}
192
193// Ignore is an Option that causes all comparisons to be ignored.
194// This value is intended to be combined with FilterPath or FilterValues.
195// It is an error to pass an unfiltered Ignore option to Equal.
196func Ignore() Option { return ignore{} }
197
198type ignore struct{ core }
199
200func (ignore) isFiltered() bool { return false }
Pragya Arya324337e2020-02-20 14:35:08 +0530201func (ignore) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption { return ignore{} }
202func (ignore) apply(s *state, _, _ reflect.Value) { s.report(true, reportByIgnore) }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700203func (ignore) String() string { return "Ignore()" }
204
Pragya Arya324337e2020-02-20 14:35:08 +0530205// validator is a sentinel Option type to indicate that some options could not
206// be evaluated due to unexported fields, missing slice elements, or
207// missing map entries. Both values are validator only for unexported fields.
208type validator struct{ core }
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700209
Pragya Arya324337e2020-02-20 14:35:08 +0530210func (validator) filter(_ *state, _ reflect.Type, vx, vy reflect.Value) applicableOption {
211 if !vx.IsValid() || !vy.IsValid() {
212 return validator{}
213 }
214 if !vx.CanInterface() || !vy.CanInterface() {
215 return validator{}
216 }
217 return nil
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700218}
Pragya Arya324337e2020-02-20 14:35:08 +0530219func (validator) apply(s *state, vx, vy reflect.Value) {
220 // Implies missing slice element or map entry.
221 if !vx.IsValid() || !vy.IsValid() {
222 s.report(vx.IsValid() == vy.IsValid(), 0)
223 return
224 }
225
226 // Unable to Interface implies unexported field without visibility access.
227 if !vx.CanInterface() || !vy.CanInterface() {
228 const help = "consider using a custom Comparer; if you control the implementation of type, you can also consider using an Exporter, AllowUnexported, or cmpopts.IgnoreUnexported"
229 var name string
230 if t := s.curPath.Index(-2).Type(); t.Name() != "" {
231 // Named type with unexported fields.
232 name = fmt.Sprintf("%q.%v", t.PkgPath(), t.Name()) // e.g., "path/to/package".MyType
233 } else {
234 // Unnamed type with unexported fields. Derive PkgPath from field.
235 var pkgPath string
236 for i := 0; i < t.NumField() && pkgPath == ""; i++ {
237 pkgPath = t.Field(i).PkgPath
238 }
239 name = fmt.Sprintf("%q.(%v)", pkgPath, t.String()) // e.g., "path/to/package".(struct { a int })
240 }
241 panic(fmt.Sprintf("cannot handle unexported field at %#v:\n\t%v\n%s", s.curPath, name, help))
242 }
243
244 panic("not reachable")
245}
246
247// identRx represents a valid identifier according to the Go specification.
248const identRx = `[_\p{L}][_\p{L}\p{N}]*`
249
250var identsRx = regexp.MustCompile(`^` + identRx + `(\.` + identRx + `)*$`)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700251
252// Transformer returns an Option that applies a transformation function that
253// converts values of a certain type into that of another.
254//
255// The transformer f must be a function "func(T) R" that converts values of
256// type T to those of type R and is implicitly filtered to input values
257// assignable to T. The transformer must not mutate T in any way.
258//
259// To help prevent some cases of infinite recursive cycles applying the
260// same transform to the output of itself (e.g., in the case where the
261// input and output types are the same), an implicit filter is added such that
262// a transformer is applicable only if that exact transformer is not already
263// in the tail of the Path since the last non-Transform step.
Pragya Arya324337e2020-02-20 14:35:08 +0530264// For situations where the implicit filter is still insufficient,
265// consider using cmpopts.AcyclicTransformer, which adds a filter
266// to prevent the transformer from being recursively applied upon itself.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700267//
268// The name is a user provided label that is used as the Transform.Name in the
Pragya Arya324337e2020-02-20 14:35:08 +0530269// transformation PathStep (and eventually shown in the Diff output).
270// The name must be a valid identifier or qualified identifier in Go syntax.
271// If empty, an arbitrary name is used.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700272func Transformer(name string, f interface{}) Option {
273 v := reflect.ValueOf(f)
274 if !function.IsType(v.Type(), function.Transformer) || v.IsNil() {
275 panic(fmt.Sprintf("invalid transformer function: %T", f))
276 }
277 if name == "" {
Pragya Arya324337e2020-02-20 14:35:08 +0530278 name = function.NameOf(v)
279 if !identsRx.MatchString(name) {
280 name = "λ" // Lambda-symbol as placeholder name
281 }
282 } else if !identsRx.MatchString(name) {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700283 panic(fmt.Sprintf("invalid name: %q", name))
284 }
285 tr := &transformer{name: name, fnc: reflect.ValueOf(f)}
286 if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
287 tr.typ = ti
288 }
289 return tr
290}
291
292type transformer struct {
293 core
294 name string
295 typ reflect.Type // T
296 fnc reflect.Value // func(T) R
297}
298
299func (tr *transformer) isFiltered() bool { return tr.typ != nil }
300
Pragya Arya324337e2020-02-20 14:35:08 +0530301func (tr *transformer) filter(s *state, t reflect.Type, _, _ reflect.Value) applicableOption {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700302 for i := len(s.curPath) - 1; i >= 0; i-- {
Pragya Arya324337e2020-02-20 14:35:08 +0530303 if t, ok := s.curPath[i].(Transform); !ok {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700304 break // Hit most recent non-Transform step
305 } else if tr == t.trans {
306 return nil // Cannot directly use same Transform
307 }
308 }
309 if tr.typ == nil || t.AssignableTo(tr.typ) {
310 return tr
311 }
312 return nil
313}
314
315func (tr *transformer) apply(s *state, vx, vy reflect.Value) {
Pragya Arya324337e2020-02-20 14:35:08 +0530316 step := Transform{&transform{pathStep{typ: tr.fnc.Type().Out(0)}, tr}}
317 vvx := s.callTRFunc(tr.fnc, vx, step)
318 vvy := s.callTRFunc(tr.fnc, vy, step)
319 step.vx, step.vy = vvx, vvy
320 s.compareAny(step)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700321}
322
323func (tr transformer) String() string {
Pragya Arya324337e2020-02-20 14:35:08 +0530324 return fmt.Sprintf("Transformer(%s, %s)", tr.name, function.NameOf(tr.fnc))
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700325}
326
327// Comparer returns an Option that determines whether two values are equal
328// to each other.
329//
330// The comparer f must be a function "func(T, T) bool" and is implicitly
331// filtered to input values assignable to T. If T is an interface, it is
332// possible that f is called with two values of different concrete types that
333// both implement T.
334//
335// The equality function must be:
336// • Symmetric: equal(x, y) == equal(y, x)
337// • Deterministic: equal(x, y) == equal(x, y)
338// • Pure: equal(x, y) does not modify x or y
339func Comparer(f interface{}) Option {
340 v := reflect.ValueOf(f)
341 if !function.IsType(v.Type(), function.Equal) || v.IsNil() {
342 panic(fmt.Sprintf("invalid comparer function: %T", f))
343 }
344 cm := &comparer{fnc: v}
345 if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
346 cm.typ = ti
347 }
348 return cm
349}
350
351type comparer struct {
352 core
353 typ reflect.Type // T
354 fnc reflect.Value // func(T, T) bool
355}
356
357func (cm *comparer) isFiltered() bool { return cm.typ != nil }
358
Pragya Arya324337e2020-02-20 14:35:08 +0530359func (cm *comparer) filter(_ *state, t reflect.Type, _, _ reflect.Value) applicableOption {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700360 if cm.typ == nil || t.AssignableTo(cm.typ) {
361 return cm
362 }
363 return nil
364}
365
366func (cm *comparer) apply(s *state, vx, vy reflect.Value) {
367 eq := s.callTTBFunc(cm.fnc, vx, vy)
Pragya Arya324337e2020-02-20 14:35:08 +0530368 s.report(eq, reportByFunc)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700369}
370
371func (cm comparer) String() string {
Pragya Arya324337e2020-02-20 14:35:08 +0530372 return fmt.Sprintf("Comparer(%s)", function.NameOf(cm.fnc))
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700373}
374
Pragya Arya324337e2020-02-20 14:35:08 +0530375// Exporter returns an Option that specifies whether Equal is allowed to
376// introspect into the unexported fields of certain struct types.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700377//
378// Users of this option must understand that comparing on unexported fields
379// from external packages is not safe since changes in the internal
380// implementation of some external package may cause the result of Equal
381// to unexpectedly change. However, it may be valid to use this option on types
382// defined in an internal package where the semantic meaning of an unexported
383// field is in the control of the user.
384//
Pragya Arya324337e2020-02-20 14:35:08 +0530385// In many cases, a custom Comparer should be used instead that defines
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700386// equality as a function of the public API of a type rather than the underlying
387// unexported implementation.
388//
389// For example, the reflect.Type documentation defines equality to be determined
390// by the == operator on the interface (essentially performing a shallow pointer
391// comparison) and most attempts to compare *regexp.Regexp types are interested
392// in only checking that the regular expression strings are equal.
393// Both of these are accomplished using Comparers:
394//
395// Comparer(func(x, y reflect.Type) bool { return x == y })
396// Comparer(func(x, y *regexp.Regexp) bool { return x.String() == y.String() })
397//
398// In other cases, the cmpopts.IgnoreUnexported option can be used to ignore
399// all unexported fields on specified struct types.
Pragya Arya324337e2020-02-20 14:35:08 +0530400func Exporter(f func(reflect.Type) bool) Option {
401 if !supportExporters {
402 panic("Exporter is not supported on purego builds")
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700403 }
Pragya Arya324337e2020-02-20 14:35:08 +0530404 return exporter(f)
405}
406
407type exporter func(reflect.Type) bool
408
409func (exporter) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption {
410 panic("not implemented")
411}
412
413// AllowUnexported returns an Options that allows Equal to forcibly introspect
414// unexported fields of the specified struct types.
415//
416// See Exporter for the proper use of this option.
417func AllowUnexported(types ...interface{}) Option {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700418 m := make(map[reflect.Type]bool)
419 for _, typ := range types {
420 t := reflect.TypeOf(typ)
421 if t.Kind() != reflect.Struct {
422 panic(fmt.Sprintf("invalid struct type: %T", typ))
423 }
424 m[t] = true
425 }
Pragya Arya324337e2020-02-20 14:35:08 +0530426 return exporter(func(t reflect.Type) bool { return m[t] })
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700427}
428
Pragya Arya324337e2020-02-20 14:35:08 +0530429// Result represents the comparison result for a single node and
430// is provided by cmp when calling Result (see Reporter).
431type Result struct {
432 _ [0]func() // Make Result incomparable
433 flags resultFlags
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700434}
435
Pragya Arya324337e2020-02-20 14:35:08 +0530436// Equal reports whether the node was determined to be equal or not.
437// As a special case, ignored nodes are considered equal.
438func (r Result) Equal() bool {
439 return r.flags&(reportEqual|reportByIgnore) != 0
440}
441
442// ByIgnore reports whether the node is equal because it was ignored.
443// This never reports true if Equal reports false.
444func (r Result) ByIgnore() bool {
445 return r.flags&reportByIgnore != 0
446}
447
448// ByMethod reports whether the Equal method determined equality.
449func (r Result) ByMethod() bool {
450 return r.flags&reportByMethod != 0
451}
452
453// ByFunc reports whether a Comparer function determined equality.
454func (r Result) ByFunc() bool {
455 return r.flags&reportByFunc != 0
456}
457
458// ByCycle reports whether a reference cycle was detected.
459func (r Result) ByCycle() bool {
460 return r.flags&reportByCycle != 0
461}
462
463type resultFlags uint
464
465const (
466 _ resultFlags = (1 << iota) / 2
467
468 reportEqual
469 reportUnequal
470 reportByIgnore
471 reportByMethod
472 reportByFunc
473 reportByCycle
474)
475
476// Reporter is an Option that can be passed to Equal. When Equal traverses
477// the value trees, it calls PushStep as it descends into each node in the
478// tree and PopStep as it ascend out of the node. The leaves of the tree are
479// either compared (determined to be equal or not equal) or ignored and reported
480// as such by calling the Report method.
481func Reporter(r interface {
482 // PushStep is called when a tree-traversal operation is performed.
483 // The PathStep itself is only valid until the step is popped.
484 // The PathStep.Values are valid for the duration of the entire traversal
485 // and must not be mutated.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700486 //
Pragya Arya324337e2020-02-20 14:35:08 +0530487 // Equal always calls PushStep at the start to provide an operation-less
488 // PathStep used to report the root values.
489 //
490 // Within a slice, the exact set of inserted, removed, or modified elements
491 // is unspecified and may change in future implementations.
492 // The entries of a map are iterated through in an unspecified order.
493 PushStep(PathStep)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700494
Pragya Arya324337e2020-02-20 14:35:08 +0530495 // Report is called exactly once on leaf nodes to report whether the
496 // comparison identified the node as equal, unequal, or ignored.
497 // A leaf node is one that is immediately preceded by and followed by
498 // a pair of PushStep and PopStep calls.
499 Report(Result)
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700500
Pragya Arya324337e2020-02-20 14:35:08 +0530501 // PopStep ascends back up the value tree.
502 // There is always a matching pop call for every push call.
503 PopStep()
504}) Option {
505 return reporter{r}
506}
507
508type reporter struct{ reporterIface }
509type reporterIface interface {
510 PushStep(PathStep)
511 Report(Result)
512 PopStep()
513}
514
515func (reporter) filter(_ *state, _ reflect.Type, _, _ reflect.Value) applicableOption {
516 panic("not implemented")
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700517}
518
519// normalizeOption normalizes the input options such that all Options groups
520// are flattened and groups with a single element are reduced to that element.
521// Only coreOptions and Options containing coreOptions are allowed.
522func normalizeOption(src Option) Option {
523 switch opts := flattenOptions(nil, Options{src}); len(opts) {
524 case 0:
525 return nil
526 case 1:
527 return opts[0]
528 default:
529 return opts
530 }
531}
532
533// flattenOptions copies all options in src to dst as a flat list.
534// Only coreOptions and Options containing coreOptions are allowed.
535func flattenOptions(dst, src Options) Options {
536 for _, opt := range src {
537 switch opt := opt.(type) {
538 case nil:
539 continue
540 case Options:
541 dst = flattenOptions(dst, opt)
542 case coreOption:
543 dst = append(dst, opt)
544 default:
545 panic(fmt.Sprintf("invalid option type: %T", opt))
546 }
547 }
548 return dst
549}