Skip to content

Commit

Permalink
separate options to dedicated file
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven committed Feb 4, 2024
1 parent 30a4a75 commit 5283b5c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
3 changes: 2 additions & 1 deletion load/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Package load loads OpenAPI specs from different sources like URL, Path, stdin
// Package load loads OpenAPI specs from different sources like URLs, paths, globs and stdin
// Optionally, specs can be preprocessed after loading
package load
48 changes: 48 additions & 0 deletions load/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package load

import (
"github.com/tufin/oasdiff/flatten/allof"
"github.com/tufin/oasdiff/flatten/commonparams"
)

// option functions can be used to preprocess specs after loading them
type Option func(Loader, []*SpecInfo) ([]*SpecInfo, error)

// WithIdentity returns the original SpecInfos
func WithIdentity() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
return specInfos, nil
}
}

// GetOption returns the requested option or the identity option
func GetOption(option Option, enable bool) Option {
if !enable {
return WithIdentity()
}
return option
}

// WithFlattenAllOf returns SpecInfos with flattened allOf
func WithFlattenAllOf() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
var err error
for _, specInfo := range specInfos {
if specInfo.Spec, err = allof.MergeSpec(specInfo.Spec); err != nil {
return nil, err
}
}
return specInfos, nil
}
}

// WithFlattenParams returns SpecInfos with Common Parameters combined into operation parameters
// See here for Common Parameters definition: https://swagger.io/docs/specification/describing-parameters/
func WithFlattenParams() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
for _, specInfo := range specInfos {
commonparams.Move(specInfo.Spec)
}
return specInfos, nil
}
}
42 changes: 0 additions & 42 deletions load/spec_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"net/url"

"github.com/getkin/kin-openapi/openapi3"
"github.com/tufin/oasdiff/flatten/allof"
"github.com/tufin/oasdiff/flatten/commonparams"
"github.com/yargevad/filepathx"
)

Expand Down Expand Up @@ -40,46 +38,6 @@ func getVersion(spec *openapi3.T) string {
return spec.Info.Version
}

type Option func(Loader, []*SpecInfo) ([]*SpecInfo, error)

// WithIdentity returns the original SpecInfos
func WithIdentity() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
return specInfos, nil
}
}

func GetOption(option Option, enable bool) Option {
if !enable {
return WithIdentity()
}
return option
}

// WithFlattenAllOf returns SpecInfos with flattened allOf
func WithFlattenAllOf() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
var err error
for _, specInfo := range specInfos {
if specInfo.Spec, err = allof.MergeSpec(specInfo.Spec); err != nil {
return nil, err
}
}
return specInfos, nil
}
}

// WithFlattenParams returns SpecInfos with Common Parameters combined into operation parameters
// See here for Common Parameters definition: https://swagger.io/docs/specification/describing-parameters/
func WithFlattenParams() Option {
return func(loader Loader, specInfos []*SpecInfo) ([]*SpecInfo, error) {
for _, specInfo := range specInfos {
commonparams.Move(specInfo.Spec)
}
return specInfos, nil
}
}

// NewSpecInfo creates a SpecInfo from a local file path, a URL, or stdin
func NewSpecInfo(loader Loader, source *Source, options ...Option) (*SpecInfo, error) {
specInfo, err := loadSpecInfo(loader, source)
Expand Down

0 comments on commit 5283b5c

Please sign in to comment.