Skip to content

Commit

Permalink
use builders
Browse files Browse the repository at this point in the history
  • Loading branch information
SVilgelm committed Dec 15, 2024
1 parent 75cee4c commit 6d25706
Show file tree
Hide file tree
Showing 38 changed files with 2,164 additions and 632 deletions.
11 changes: 11 additions & 0 deletions bool_or_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,14 @@ func (o *BoolOrSchema) validateSpec(path string, opts *specValidationOptions) []
}
return errs
}

func NewBoolOrSchema(v any) *BoolOrSchema {
switch v := v.(type) {
case bool:
return &BoolOrSchema{Allowed: v}
case *RefOrSpec[Schema]:
return &BoolOrSchema{Schema: v}
default:
return nil

Check warning on line 82 in bool_or_schema.go

View check run for this annotation

Codecov / codecov/patch

bool_or_schema.go#L75-L82

Added lines #L75 - L82 were not covered by tests
}
}
56 changes: 50 additions & 6 deletions callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,77 @@ import (
// '200':
// description: callback successfully processed
type Callback struct {
Callback map[string]*RefOrSpec[Extendable[PathItem]]
Paths map[string]*RefOrSpec[Extendable[PathItem]]
}

// MarshalJSON implements json.Marshaler interface.
func (o *Callback) MarshalJSON() ([]byte, error) {
return json.Marshal(&o.Callback)
return json.Marshal(&o.Paths)
}

// UnmarshalJSON implements json.Unmarshaler interface.
func (o *Callback) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &o.Callback)
return json.Unmarshal(data, &o.Paths)
}

// MarshalYAML implements yaml.Marshaler interface.
func (o *Callback) MarshalYAML() (any, error) {
return o.Callback, nil
return o.Paths, nil
}

// UnmarshalYAML implements yaml.Unmarshaler interface.
func (o *Callback) UnmarshalYAML(node *yaml.Node) error {
return node.Decode(&o.Callback)
return node.Decode(&o.Paths)
}

func (o *Callback) validateSpec(location string, opts *specValidationOptions) []*validationError {
var errs []*validationError
for k, v := range o.Callback {
for k, v := range o.Paths {
errs = append(errs, v.validateSpec(joinLoc(location, k), opts)...)
}
return nil
}

func (o *Callback) Add(expression string, item *RefOrSpec[Extendable[PathItem]]) *Callback {
if o.Paths == nil {
o.Paths = make(map[string]*RefOrSpec[Extendable[PathItem]], 1)
}

Check warning on line 66 in callback.go

View check run for this annotation

Codecov / codecov/patch

callback.go#L65-L66

Added lines #L65 - L66 were not covered by tests
o.Paths[expression] = item
return o
}

type CallbackBuilder struct {
spec *RefOrSpec[Extendable[Callback]]
}

func NewCallbackBuilder() *CallbackBuilder {
return &CallbackBuilder{
spec: NewRefOrExtSpec[Callback](&Callback{
Paths: make(map[string]*RefOrSpec[Extendable[PathItem]]),
}),
}
}

func (b *CallbackBuilder) Build() *RefOrSpec[Extendable[Callback]] {
return b.spec
}

func (b *CallbackBuilder) Extensions(v map[string]any) *CallbackBuilder {
b.spec.Spec.Extensions = v
return b

Check warning on line 89 in callback.go

View check run for this annotation

Codecov / codecov/patch

callback.go#L87-L89

Added lines #L87 - L89 were not covered by tests
}

func (b *CallbackBuilder) AddExt(name string, value any) *CallbackBuilder {
b.spec.Spec.Add(name, value)
return b

Check warning on line 94 in callback.go

View check run for this annotation

Codecov / codecov/patch

callback.go#L92-L94

Added lines #L92 - L94 were not covered by tests
}

func (b *CallbackBuilder) Paths(paths map[string]*RefOrSpec[Extendable[PathItem]]) *CallbackBuilder {
b.spec.Spec.Spec.Paths = paths
return b

Check warning on line 99 in callback.go

View check run for this annotation

Codecov / codecov/patch

callback.go#L97-L99

Added lines #L97 - L99 were not covered by tests
}

func (b *CallbackBuilder) AddPathItem(expression string, item *RefOrSpec[Extendable[PathItem]]) *CallbackBuilder {
b.spec.Spec.Spec.Add(expression, item)
return b
}
107 changes: 7 additions & 100 deletions components.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package openapi

import "fmt"

// Components holds a set of reusable objects for different aspects of the OAS.
// All objects defined within the components object will have no effect on the API unless they are explicitly referenced
// from properties outside the components object.
Expand Down Expand Up @@ -100,156 +98,61 @@ type Components struct {
Paths map[string]*RefOrSpec[Extendable[PathItem]] `json:"paths,omitempty" yaml:"paths,omitempty"`
}

// WithRefOrSpec adds the given object to the appropriate list based on a type and returns the current object (self|this).
func (o *Components) WithRefOrSpec(name string, v any) *Components {
// Add adds the given object to the appropriate list based on a type and returns the current object (self|this).
func (o *Components) Add(name string, v any) *Components {
switch spec := v.(type) {
case *RefOrSpec[Schema]:
if o.Schemas == nil {
o.Schemas = make(map[string]*RefOrSpec[Schema], 1)
}
o.Schemas[name] = spec
case *Schema:
if o.Schemas == nil {
o.Schemas = make(map[string]*RefOrSpec[Schema], 1)
}
o.Schemas[name] = NewRefOrSpec[Schema](spec)
case *RefOrSpec[Extendable[Response]]:
if o.Responses == nil {
o.Responses = make(map[string]*RefOrSpec[Extendable[Response]], 1)
}
o.Responses[name] = spec
case *Extendable[Response]:
if o.Responses == nil {
o.Responses = make(map[string]*RefOrSpec[Extendable[Response]], 1)
}
o.Responses[name] = NewRefOrSpec[Extendable[Response]](spec)
case *Response:
if o.Responses == nil {
o.Responses = make(map[string]*RefOrSpec[Extendable[Response]], 1)
}
o.Responses[name] = NewRefOrSpec[Extendable[Response]](NewExtendable(spec))
case *RefOrSpec[Extendable[Parameter]]:
if o.Parameters == nil {
o.Parameters = make(map[string]*RefOrSpec[Extendable[Parameter]], 1)
}
o.Parameters[name] = spec
case *Extendable[Parameter]:
if o.Parameters == nil {
o.Parameters = make(map[string]*RefOrSpec[Extendable[Parameter]], 1)
}
o.Parameters[name] = NewRefOrSpec[Extendable[Parameter]](spec)
case *Parameter:
if o.Parameters == nil {
o.Parameters = make(map[string]*RefOrSpec[Extendable[Parameter]], 1)
}
o.Parameters[name] = NewRefOrSpec[Extendable[Parameter]](NewExtendable(spec))
case *RefOrSpec[Extendable[Example]]:
if o.Examples == nil {
o.Examples = make(map[string]*RefOrSpec[Extendable[Example]], 1)
}
o.Examples[name] = spec
case *Extendable[Example]:
if o.Examples == nil {
o.Examples = make(map[string]*RefOrSpec[Extendable[Example]], 1)
}
o.Examples[name] = NewRefOrSpec[Extendable[Example]](spec)
case *Example:
if o.Examples == nil {
o.Examples = make(map[string]*RefOrSpec[Extendable[Example]], 1)
}
o.Examples[name] = NewRefOrSpec[Extendable[Example]](NewExtendable(spec))
case *RefOrSpec[Extendable[RequestBody]]:
if o.RequestBodies == nil {
o.RequestBodies = make(map[string]*RefOrSpec[Extendable[RequestBody]], 1)
}
o.RequestBodies[name] = spec
case *Extendable[RequestBody]:
if o.RequestBodies == nil {
o.RequestBodies = make(map[string]*RefOrSpec[Extendable[RequestBody]], 1)
}
o.RequestBodies[name] = NewRefOrSpec[Extendable[RequestBody]](spec)
case *RequestBody:
if o.RequestBodies == nil {
o.RequestBodies = make(map[string]*RefOrSpec[Extendable[RequestBody]], 1)
}
o.RequestBodies[name] = NewRefOrSpec[Extendable[RequestBody]](NewExtendable(spec))
case *RefOrSpec[Extendable[Header]]:
if o.Headers == nil {
o.Headers = make(map[string]*RefOrSpec[Extendable[Header]], 1)
}
o.Headers[name] = spec
case *Extendable[Header]:
if o.Headers == nil {
o.Headers = make(map[string]*RefOrSpec[Extendable[Header]], 1)
}
o.Headers[name] = NewRefOrSpec[Extendable[Header]](spec)
case *Header:
if o.Headers == nil {
o.Headers = make(map[string]*RefOrSpec[Extendable[Header]], 1)
}
o.Headers[name] = NewRefOrSpec[Extendable[Header]](NewExtendable(spec))
case *RefOrSpec[Extendable[SecurityScheme]]:
if o.SecuritySchemes == nil {
o.SecuritySchemes = make(map[string]*RefOrSpec[Extendable[SecurityScheme]], 1)
}
o.SecuritySchemes[name] = spec
case *Extendable[SecurityScheme]:
if o.SecuritySchemes == nil {
o.SecuritySchemes = make(map[string]*RefOrSpec[Extendable[SecurityScheme]], 1)
}
o.SecuritySchemes[name] = NewRefOrSpec[Extendable[SecurityScheme]](spec)
case *SecurityScheme:
if o.SecuritySchemes == nil {
o.SecuritySchemes = make(map[string]*RefOrSpec[Extendable[SecurityScheme]], 1)
}
o.SecuritySchemes[name] = NewRefOrSpec[Extendable[SecurityScheme]](NewExtendable(spec))
case *RefOrSpec[Extendable[Link]]:
if o.Links == nil {
o.Links = make(map[string]*RefOrSpec[Extendable[Link]], 1)
}
o.Links[name] = spec
case *Extendable[Link]:
if o.Links == nil {
o.Links = make(map[string]*RefOrSpec[Extendable[Link]], 1)
}
o.Links[name] = NewRefOrSpec[Extendable[Link]](spec)
case *Link:
if o.Links == nil {
o.Links = make(map[string]*RefOrSpec[Extendable[Link]], 1)
}
o.Links[name] = NewRefOrSpec[Extendable[Link]](NewExtendable(spec))
case *RefOrSpec[Extendable[Callback]]:
if o.Callbacks == nil {
o.Callbacks = make(map[string]*RefOrSpec[Extendable[Callback]], 1)
}
o.Callbacks[name] = spec
case *Extendable[Callback]:
if o.Callbacks == nil {
o.Callbacks = make(map[string]*RefOrSpec[Extendable[Callback]], 1)
}
o.Callbacks[name] = NewRefOrSpec[Extendable[Callback]](spec)
case *Callback:
if o.Callbacks == nil {
o.Callbacks = make(map[string]*RefOrSpec[Extendable[Callback]], 1)
}
o.Callbacks[name] = NewRefOrSpec[Extendable[Callback]](NewExtendable(spec))
case *RefOrSpec[Extendable[PathItem]]:
if o.Paths == nil {
o.Paths = make(map[string]*RefOrSpec[Extendable[PathItem]], 1)
}
o.Paths[name] = spec
case *Extendable[PathItem]:
if o.Paths == nil {
o.Paths = make(map[string]*RefOrSpec[Extendable[PathItem]], 1)
}
o.Paths[name] = NewRefOrSpec[Extendable[PathItem]](spec)
case *PathItem:
if o.Paths == nil {
o.Paths = make(map[string]*RefOrSpec[Extendable[PathItem]], 1)
}
o.Paths[name] = NewRefOrSpec[Extendable[PathItem]](NewExtendable(spec))
default:
panic(fmt.Errorf("wrong component type: %T", spec))
// ignore to avoid panic
}
return o
}
Expand Down Expand Up @@ -309,3 +212,7 @@ func (o *Components) validateSpec(location string, opts *specValidationOptions)

return errs
}

func NewComponents() *Extendable[Components] {
return NewExtendable[Components](&Components{})
}
Loading

0 comments on commit 6d25706

Please sign in to comment.