File: options.go

package info (click to toggle)
golang-github-go-openapi-loads 0.23.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,228 kB
  • sloc: makefile: 4
file content (74 lines) | stat: -rw-r--r-- 1,585 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package loads

import "github.com/go-openapi/swag/loading"

type options struct {
	loader         *loader
	loadingOptions []loading.Option
}

func defaultOptions() *options {
	return &options{
		loader: loaders,
	}
}

func loaderFromOptions(options []LoaderOption) *loader {
	opts := defaultOptions()
	for _, apply := range options {
		apply(opts)
	}

	l := opts.loader.clone()
	l.loadingOptions = opts.loadingOptions

	return l
}

// LoaderOption allows to fine-tune the spec loader behavior
type LoaderOption func(*options)

// WithDocLoader sets a custom loader for loading specs
func WithDocLoader(l DocLoader) LoaderOption {
	return func(opt *options) {
		if l == nil {
			return
		}
		opt.loader = &loader{
			DocLoaderWithMatch: DocLoaderWithMatch{
				Fn: l,
			},
		}
	}
}

// WithDocLoaderMatches sets a chain of custom loaders for loading specs
// for different extension matches.
//
// Loaders are executed in the order of provided DocLoaderWithMatch'es.
func WithDocLoaderMatches(l ...DocLoaderWithMatch) LoaderOption {
	return func(opt *options) {
		var final, prev *loader
		for _, ldr := range l {
			if ldr.Fn == nil {
				continue
			}

			if prev == nil {
				final = &loader{DocLoaderWithMatch: ldr}
				prev = final
				continue
			}

			prev = prev.WithNext(&loader{DocLoaderWithMatch: ldr})
		}
		opt.loader = final
	}
}

// WithLoadingOptions adds some [loading.Option] to be added when calling a registered loader.
func WithLoadingOptions(loadingOptions ...loading.Option) LoaderOption {
	return func(opt *options) {
		opt.loadingOptions = loadingOptions
	}
}