Skip to content

Commit

Permalink
refactor: rename builder func signature struct names to reduce noise …
Browse files Browse the repository at this point in the history
…in API
  • Loading branch information
mefellows committed Jun 28, 2022
1 parent 63cd056 commit f718679
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 94 deletions.
48 changes: 24 additions & 24 deletions consumer/http_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ type V2InteractionWithRequest struct {
provider *V2HTTPMockProvider
}

type V2RequestBuilder func(*V2InteractionWithRequestBuilder)
type V2RequestBuilderFunc func(*V2RequestBuilder)

type V2InteractionWithRequestBuilder struct {
type V2RequestBuilder struct {
interaction *Interaction
provider *V2HTTPMockProvider
}
Expand Down Expand Up @@ -106,16 +106,16 @@ func (i *V2InteractionWithCompleteRequest) WithCompleteResponse(response Respons
}

// WithRequest provides a builder for the expected request
func (i *UnconfiguredV2Interaction) WithRequest(method Method, path string, builders ...V2RequestBuilder) *V2InteractionWithRequest {
func (i *UnconfiguredV2Interaction) WithRequest(method Method, path string, builders ...V2RequestBuilderFunc) *V2InteractionWithRequest {
return i.WithRequestPathMatcher(method, matchers.String(path), builders...)
}

// WithRequestPathMatcher allows a matcher in the expected request path
func (i *UnconfiguredV2Interaction) WithRequestPathMatcher(method Method, path matchers.Matcher, builders ...V2RequestBuilder) *V2InteractionWithRequest {
func (i *UnconfiguredV2Interaction) WithRequestPathMatcher(method Method, path matchers.Matcher, builders ...V2RequestBuilderFunc) *V2InteractionWithRequest {
i.interaction.interaction.WithRequest(string(method), path)

for _, builder := range builders {
builder(&V2InteractionWithRequestBuilder{
builder(&V2RequestBuilder{
interaction: i.interaction,
provider: i.provider,
})
Expand All @@ -128,28 +128,28 @@ func (i *UnconfiguredV2Interaction) WithRequestPathMatcher(method Method, path m
}

// Query specifies any query string on the expect request
func (i *V2InteractionWithRequestBuilder) Query(key string, values ...matchers.Matcher) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) Query(key string, values ...matchers.Matcher) *V2RequestBuilder {
i.interaction.interaction.WithQuery(keyValuesToMapStringArrayInterface(key, values...))

return i
}

// Header adds a header to the expected request
func (i *V2InteractionWithRequestBuilder) Header(key string, values ...matchers.Matcher) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) Header(key string, values ...matchers.Matcher) *V2RequestBuilder {
i.interaction.interaction.WithRequestHeaders(keyValuesToMapStringArrayInterface(key, values...))

return i
}

// Headers sets the headers on the expected request
func (i *V2InteractionWithRequestBuilder) Headers(headers matchers.HeadersMatcher) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) Headers(headers matchers.HeadersMatcher) *V2RequestBuilder {
i.interaction.interaction.WithRequestHeaders(headersMatcherToNativeHeaders(headers))

return i
}

// JSONBody adds a JSON body to the expected request
func (i *V2InteractionWithRequestBuilder) JSONBody(body interface{}) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) JSONBody(body interface{}) *V2RequestBuilder {
// TODO: Don't like panic, but not sure if there is a better builder experience?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
panic(err)
Expand All @@ -172,21 +172,21 @@ func (i *V2InteractionWithRequestBuilder) JSONBody(body interface{}) *V2Interact
}

// BinaryBody adds a binary body to the expected request
func (i *V2InteractionWithRequestBuilder) BinaryBody(body []byte) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) BinaryBody(body []byte) *V2RequestBuilder {
i.interaction.interaction.WithBinaryRequestBody(body)

return i
}

// MultipartBody adds a multipart body to the expected request
func (i *V2InteractionWithRequestBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V2RequestBuilder {
i.interaction.interaction.WithRequestMultipartFile(contentType, filename, mimePartName)

return i
}

// Body adds general body to the expected request
func (i *V2InteractionWithRequestBuilder) Body(contentType string, body []byte) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) Body(contentType string, body []byte) *V2RequestBuilder {
// Check if someone tried to add an object as a string representation
// as per original allowed implementation, e.g.
// { "foo": "bar", "baz": like("bat") }
Expand All @@ -202,19 +202,19 @@ func (i *V2InteractionWithRequestBuilder) Body(contentType string, body []byte)
}

// BodyMatch uses struct tags to automatically determine matchers from the given struct
func (i *V2InteractionWithRequestBuilder) BodyMatch(body interface{}) *V2InteractionWithRequestBuilder {
func (i *V2RequestBuilder) BodyMatch(body interface{}) *V2RequestBuilder {
i.interaction.interaction.WithJSONRequestBody(matchers.MatchV2(body))

return i
}

// WillRespondWith sets the expected status and provides a response builder
func (i *V2InteractionWithRequest) WillRespondWith(status int, builders ...V2ResponseBuilder) *V2InteractionWithResponse {
func (i *V2InteractionWithRequest) WillRespondWith(status int, builders ...V2ResponseBuilderFunc) *V2InteractionWithResponse {
i.interaction.interaction.WithStatus(status)

for _, builder := range builders {

builder(&V2InteractionWithResponseBuilder{
builder(&V2ResponseBuilder{
interaction: i.interaction,
provider: i.provider,
})
Expand All @@ -226,9 +226,9 @@ func (i *V2InteractionWithRequest) WillRespondWith(status int, builders ...V2Res
}
}

type V2ResponseBuilder func(*V2InteractionWithResponseBuilder)
type V2ResponseBuilderFunc func(*V2ResponseBuilder)

type V2InteractionWithResponseBuilder struct {
type V2ResponseBuilder struct {
interaction *Interaction
provider *V2HTTPMockProvider
}
Expand All @@ -239,21 +239,21 @@ type V2InteractionWithResponse struct {
}

// Header adds a header to the expected response
func (i *V2InteractionWithResponseBuilder) Header(key string, values ...matchers.Matcher) *V2InteractionWithResponseBuilder {
func (i *V2ResponseBuilder) Header(key string, values ...matchers.Matcher) *V2ResponseBuilder {
i.interaction.interaction.WithResponseHeaders(keyValuesToMapStringArrayInterface(key, values...))

return i
}

// Headers sets the headers on the expected response
func (i *V2InteractionWithResponseBuilder) Headers(headers matchers.HeadersMatcher) *V2InteractionWithResponseBuilder {
func (i *V2ResponseBuilder) Headers(headers matchers.HeadersMatcher) *V2ResponseBuilder {
i.interaction.interaction.WithResponseHeaders(headersMatcherToNativeHeaders(headers))

return i
}

// JSONBody adds a JSON body to the expected response
func (i *V2InteractionWithResponseBuilder) JSONBody(body interface{}) *V2InteractionWithResponseBuilder {
func (i *V2ResponseBuilder) JSONBody(body interface{}) *V2ResponseBuilder {
// TODO: Don't like panic, how to build a better builder here - nil return + log?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
panic(err)
Expand All @@ -275,28 +275,28 @@ func (i *V2InteractionWithResponseBuilder) JSONBody(body interface{}) *V2Interac
}

// BinaryBody adds a binary body to the expected response
func (i *V2InteractionWithResponseBuilder) BinaryBody(body []byte) *V2InteractionWithResponseBuilder {
func (i *V2ResponseBuilder) BinaryBody(body []byte) *V2ResponseBuilder {
i.interaction.interaction.WithBinaryResponseBody(body)

return i
}

// MultipartBody adds a multipart body to the expected response
func (i *V2InteractionWithResponseBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V2InteractionWithResponseBuilder {
func (i *V2ResponseBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V2ResponseBuilder {
i.interaction.interaction.WithResponseMultipartFile(contentType, filename, mimePartName)

return i
}

// Body adds general body to the expected request
func (i *V2InteractionWithResponseBuilder) Body(contentType string, body []byte) *V2InteractionWithResponseBuilder {
func (i *V2ResponseBuilder) Body(contentType string, body []byte) *V2ResponseBuilder {
i.interaction.interaction.WithResponseBody(contentType, body)

return i
}

// BodyMatch uses struct tags to automatically determine matchers from the given struct
func (i *V2InteractionWithResponseBuilder) BodyMatch(body interface{}) *V2InteractionWithResponseBuilder {
func (i *V2ResponseBuilder) BodyMatch(body interface{}) *V2ResponseBuilder {
i.interaction.interaction.WithJSONResponseBody(matchers.MatchV2(body))

return i
Expand Down
48 changes: 24 additions & 24 deletions consumer/http_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ type V3InteractionWithRequest struct {
provider *V3HTTPMockProvider
}

type V3RequestBuilder func(*V3InteractionWithRequestBuilder)
type V3RequestBuilderFunc func(*V3RequestBuilder)

type V3InteractionWithRequestBuilder struct {
type V3RequestBuilder struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
Expand All @@ -92,16 +92,16 @@ func (i *UnconfiguredV3Interaction) UponReceiving(description string) *Unconfigu
}

// WithRequest provides a builder for the expected request
func (i *UnconfiguredV3Interaction) WithRequest(method Method, path string, builders ...V3RequestBuilder) *V3InteractionWithRequest {
func (i *UnconfiguredV3Interaction) WithRequest(method Method, path string, builders ...V3RequestBuilderFunc) *V3InteractionWithRequest {
return i.WithRequestPathMatcher(method, matchers.String(path), builders...)
}

// WithRequestPathMatcher allows a matcher in the expected request path
func (i *UnconfiguredV3Interaction) WithRequestPathMatcher(method Method, path matchers.Matcher, builders ...V3RequestBuilder) *V3InteractionWithRequest {
func (i *UnconfiguredV3Interaction) WithRequestPathMatcher(method Method, path matchers.Matcher, builders ...V3RequestBuilderFunc) *V3InteractionWithRequest {
i.interaction.interaction.WithRequest(string(method), path)

for _, builder := range builders {
builder(&V3InteractionWithRequestBuilder{
builder(&V3RequestBuilder{
interaction: i.interaction,
provider: i.provider,
})
Expand All @@ -114,28 +114,28 @@ func (i *UnconfiguredV3Interaction) WithRequestPathMatcher(method Method, path m
}

// Query specifies any query string on the expect request
func (i *V3InteractionWithRequestBuilder) Query(key string, values ...matchers.Matcher) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) Query(key string, values ...matchers.Matcher) *V3RequestBuilder {
i.interaction.interaction.WithQuery(keyValuesToMapStringArrayInterface(key, values...))

return i
}

// Header adds a header to the expected request
func (i *V3InteractionWithRequestBuilder) Header(key string, values ...matchers.Matcher) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) Header(key string, values ...matchers.Matcher) *V3RequestBuilder {
i.interaction.interaction.WithRequestHeaders(keyValuesToMapStringArrayInterface(key, values...))

return i
}

// Headers sets the headers on the expected request
func (i *V3InteractionWithRequestBuilder) Headers(headers matchers.HeadersMatcher) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) Headers(headers matchers.HeadersMatcher) *V3RequestBuilder {
i.interaction.interaction.WithRequestHeaders(headersMatcherToNativeHeaders(headers))

return i
}

// JSONBody adds a JSON body to the expected request
func (i *V3InteractionWithRequestBuilder) JSONBody(body interface{}) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) JSONBody(body interface{}) *V3RequestBuilder {
// TODO: Don't like panic, but not sure if there is a better builder experience?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
panic(err)
Expand All @@ -158,21 +158,21 @@ func (i *V3InteractionWithRequestBuilder) JSONBody(body interface{}) *V3Interact
}

// BinaryBody adds a binary body to the expected request
func (i *V3InteractionWithRequestBuilder) BinaryBody(body []byte) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) BinaryBody(body []byte) *V3RequestBuilder {
i.interaction.interaction.WithBinaryRequestBody(body)

return i
}

// MultipartBody adds a multipart body to the expected request
func (i *V3InteractionWithRequestBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V3RequestBuilder {
i.interaction.interaction.WithRequestMultipartFile(contentType, filename, mimePartName)

return i
}

// Body adds general body to the expected request
func (i *V3InteractionWithRequestBuilder) Body(contentType string, body []byte) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) Body(contentType string, body []byte) *V3RequestBuilder {
// Check if someone tried to add an object as a string representation
// as per original allowed implementation, e.g.
// { "foo": "bar", "baz": like("bat") }
Expand All @@ -188,19 +188,19 @@ func (i *V3InteractionWithRequestBuilder) Body(contentType string, body []byte)
}

// BodyMatch uses struct tags to automatically determine matchers from the given struct
func (i *V3InteractionWithRequestBuilder) BodyMatch(body interface{}) *V3InteractionWithRequestBuilder {
func (i *V3RequestBuilder) BodyMatch(body interface{}) *V3RequestBuilder {
i.interaction.interaction.WithJSONRequestBody(matchers.MatchV2(body))

return i
}

// WillRespondWith sets the expected status and provides a response builder
func (i *V3InteractionWithRequest) WillRespondWith(status int, builders ...V3ResponseBuilder) *V3InteractionWithResponse {
func (i *V3InteractionWithRequest) WillRespondWith(status int, builders ...V3ResponseBuilderFunc) *V3InteractionWithResponse {
i.interaction.interaction.WithStatus(status)

for _, builder := range builders {

builder(&V3InteractionWithResponseBuilder{
builder(&V3ResponseBuilder{
interaction: i.interaction,
provider: i.provider,
})
Expand All @@ -212,9 +212,9 @@ func (i *V3InteractionWithRequest) WillRespondWith(status int, builders ...V3Res
}
}

type V3ResponseBuilder func(*V3InteractionWithResponseBuilder)
type V3ResponseBuilderFunc func(*V3ResponseBuilder)

type V3InteractionWithResponseBuilder struct {
type V3ResponseBuilder struct {
interaction *Interaction
provider *V3HTTPMockProvider
}
Expand All @@ -225,21 +225,21 @@ type V3InteractionWithResponse struct {
}

// Header adds a header to the expected response
func (i *V3InteractionWithResponseBuilder) Header(key string, values ...matchers.Matcher) *V3InteractionWithResponseBuilder {
func (i *V3ResponseBuilder) Header(key string, values ...matchers.Matcher) *V3ResponseBuilder {
i.interaction.interaction.WithResponseHeaders(keyValuesToMapStringArrayInterface(key, values...))

return i
}

// Headers sets the headers on the expected response
func (i *V3InteractionWithResponseBuilder) Headers(headers matchers.HeadersMatcher) *V3InteractionWithResponseBuilder {
func (i *V3ResponseBuilder) Headers(headers matchers.HeadersMatcher) *V3ResponseBuilder {
i.interaction.interaction.WithResponseHeaders(headersMatcherToNativeHeaders(headers))

return i
}

// JSONBody adds a JSON body to the expected response
func (i *V3InteractionWithResponseBuilder) JSONBody(body interface{}) *V3InteractionWithResponseBuilder {
func (i *V3ResponseBuilder) JSONBody(body interface{}) *V3ResponseBuilder {
// TODO: Don't like panic, how to build a better builder here - nil return + log?
if err := validateMatchers(i.interaction.specificationVersion, body); err != nil {
panic(err)
Expand All @@ -261,28 +261,28 @@ func (i *V3InteractionWithResponseBuilder) JSONBody(body interface{}) *V3Interac
}

// BinaryBody adds a binary body to the expected response
func (i *V3InteractionWithResponseBuilder) BinaryBody(body []byte) *V3InteractionWithResponseBuilder {
func (i *V3ResponseBuilder) BinaryBody(body []byte) *V3ResponseBuilder {
i.interaction.interaction.WithBinaryResponseBody(body)

return i
}

// MultipartBody adds a multipart body to the expected response
func (i *V3InteractionWithResponseBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V3InteractionWithResponseBuilder {
func (i *V3ResponseBuilder) MultipartBody(contentType string, filename string, mimePartName string) *V3ResponseBuilder {
i.interaction.interaction.WithResponseMultipartFile(contentType, filename, mimePartName)

return i
}

// Body adds general body to the expected request
func (i *V3InteractionWithResponseBuilder) Body(contentType string, body []byte) *V3InteractionWithResponseBuilder {
func (i *V3ResponseBuilder) Body(contentType string, body []byte) *V3ResponseBuilder {
i.interaction.interaction.WithResponseBody(contentType, body)

return i
}

// BodyMatch uses struct tags to automatically determine matchers from the given struct
func (i *V3InteractionWithResponseBuilder) BodyMatch(body interface{}) *V3InteractionWithResponseBuilder {
func (i *V3ResponseBuilder) BodyMatch(body interface{}) *V3ResponseBuilder {
i.interaction.interaction.WithJSONResponseBody(matchers.MatchV2(body))

return i
Expand Down
Loading

0 comments on commit f718679

Please sign in to comment.