...

Source file src/github.com/go-openapi/loads/options.go

Documentation: github.com/go-openapi/loads

     1  package loads
     2  
     3  type options struct {
     4  	loader *loader
     5  }
     6  
     7  func defaultOptions() *options {
     8  	return &options{
     9  		loader: loaders,
    10  	}
    11  }
    12  
    13  func loaderFromOptions(options []LoaderOption) *loader {
    14  	opts := defaultOptions()
    15  	for _, apply := range options {
    16  		apply(opts)
    17  	}
    18  
    19  	return opts.loader
    20  }
    21  
    22  // LoaderOption allows to fine-tune the spec loader behavior
    23  type LoaderOption func(*options)
    24  
    25  // WithDocLoader sets a custom loader for loading specs
    26  func WithDocLoader(l DocLoader) LoaderOption {
    27  	return func(opt *options) {
    28  		if l == nil {
    29  			return
    30  		}
    31  		opt.loader = &loader{
    32  			DocLoaderWithMatch: DocLoaderWithMatch{
    33  				Fn: l,
    34  			},
    35  		}
    36  	}
    37  }
    38  
    39  // WithDocLoaderMatches sets a chain of custom loaders for loading specs
    40  // for different extension matches.
    41  //
    42  // Loaders are executed in the order of provided DocLoaderWithMatch'es.
    43  func WithDocLoaderMatches(l ...DocLoaderWithMatch) LoaderOption {
    44  	return func(opt *options) {
    45  		var final, prev *loader
    46  		for _, ldr := range l {
    47  			if ldr.Fn == nil {
    48  				continue
    49  			}
    50  
    51  			if prev == nil {
    52  				final = &loader{DocLoaderWithMatch: ldr}
    53  				prev = final
    54  				continue
    55  			}
    56  
    57  			prev = prev.WithNext(&loader{DocLoaderWithMatch: ldr})
    58  		}
    59  		opt.loader = final
    60  	}
    61  }
    62  

View as plain text