Skip to content

Commit

Permalink
allow multiple samples for Write, issue #514
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Oct 19, 2022
1 parent ec34605 commit 408f75a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
3 changes: 2 additions & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type Route struct {
ParameterDocs []*Parameter
ResponseErrors map[int]ResponseError
DefaultResponse *ResponseError
ReadSample, WriteSample interface{} // structs that model an example request or response payload
ReadSample, WriteSample interface{} // structs that model an example request or response payload
WriteSamples []interface{} // if more than one return types is possible (oneof) then this will contain multiple values

// Extra information used to store custom information about the route.
Metadata map[string]interface{}
Expand Down
44 changes: 25 additions & 19 deletions route_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,29 @@ type RouteBuilder struct {
typeNameHandleFunc TypeNameHandleFunction // required

// documentation
doc string
notes string
operation string
readSample, writeSample interface{}
parameters []*Parameter
errorMap map[int]ResponseError
defaultResponse *ResponseError
metadata map[string]interface{}
extensions map[string]interface{}
deprecated bool
contentEncodingEnabled *bool
doc string
notes string
operation string
readSample interface{}
writeSamples []interface{}
parameters []*Parameter
errorMap map[int]ResponseError
defaultResponse *ResponseError
metadata map[string]interface{}
extensions map[string]interface{}
deprecated bool
contentEncodingEnabled *bool
}

// Do evaluates each argument with the RouteBuilder itself.
// This allows you to follow DRY principles without breaking the fluent programming style.
// Example:
// ws.Route(ws.DELETE("/{name}").To(t.deletePerson).Do(Returns200, Returns500))
//
// func Returns500(b *RouteBuilder) {
// b.Returns(500, "Internal Server Error", restful.ServiceError{})
// }
// ws.Route(ws.DELETE("/{name}").To(t.deletePerson).Do(Returns200, Returns500))
//
// func Returns500(b *RouteBuilder) {
// b.Returns(500, "Internal Server Error", restful.ServiceError{})
// }
func (b *RouteBuilder) Do(oneArgBlocks ...func(*RouteBuilder)) *RouteBuilder {
for _, each := range oneArgBlocks {
each(b)
Expand Down Expand Up @@ -133,9 +135,9 @@ func (b RouteBuilder) ParameterNamed(name string) (p *Parameter) {
return p
}

// Writes tells what resource type will be written as the response payload. Optional.
func (b *RouteBuilder) Writes(sample interface{}) *RouteBuilder {
b.writeSample = sample
// Writes tells which one of the resource types will be written as the response payload. Optional.
func (b *RouteBuilder) Writes(samples ...interface{}) *RouteBuilder {
b.writeSamples = samples // oneof
return b
}

Expand Down Expand Up @@ -340,12 +342,16 @@ func (b *RouteBuilder) Build() Route {
ResponseErrors: b.errorMap,
DefaultResponse: b.defaultResponse,
ReadSample: b.readSample,
WriteSample: b.writeSample,
WriteSamples: b.writeSamples,
Metadata: b.metadata,
Deprecated: b.deprecated,
contentEncodingEnabled: b.contentEncodingEnabled,
allowedMethodsWithoutContentType: b.allowedMethodsWithoutContentType,
}
// set WriteSample if one specified
if len(b.writeSamples) == 1 {
route.WriteSample = b.writeSamples[0]
}
route.Extensions = b.extensions
route.postBuild()
return route
Expand Down
29 changes: 29 additions & 0 deletions route_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@ import (
"time"
)

func TestRouteBuilderWrites(t *testing.T) {
b := new(RouteBuilder)
b.To(dummy)
type StructA struct{}
type StructB struct{}
b.Writes(StructA{})
if got, want := len(b.writeSamples), 1; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
{
r := b.Build()
var null interface{}
if r.WriteSample == null {
t.Fail()
}
}
b.Writes(StructA{}, StructB{})
if got, want := len(b.writeSamples), 2; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
{
r := b.Build()
var null interface{}
if got, want := r.WriteSample, null; got != want {
t.Errorf("got [%v:%T] want [%v:%T]", got, got, want, want)
}
}
}

func TestRouteBuilder_PathParameter(t *testing.T) {
p := &Parameter{&ParameterData{Name: "name", Description: "desc"}}
p.AllowMultiple(true)
Expand Down

0 comments on commit 408f75a

Please sign in to comment.