Skip to content

Commit

Permalink
interface{} -> any
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwhatcott committed May 13, 2024
1 parent d9e923c commit 2957920
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 68 deletions.
8 changes: 4 additions & 4 deletions assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func Assert(t testingT) *That {
type That struct{ t testingT }

// That is an intermediate method call, as in: assert.With(t).That(actual).Equals(expected)
func (this *That) That(actual interface{}) *Assertion {
func (this *That) That(actual any) *Assertion {
return &Assertion{
testingT: this.t,
actual: actual,
Expand All @@ -19,13 +19,13 @@ func (this *That) That(actual interface{}) *Assertion {

type testingT interface {
Helper()
Errorf(format string, args ...interface{})
Errorf(format string, args ...any)
}

// Assertion is an intermediate type, not to be instantiated directly.
type Assertion struct {
testingT
actual interface{}
actual any
}

// IsNil asserts that the value provided to That is nil.
Expand All @@ -49,7 +49,7 @@ func (this *Assertion) IsFalse() {
}

// Equals asserts that the value provided is equal to the expected value.
func (this *Assertion) Equals(expected interface{}) {
func (this *Assertion) Equals(expected any) {
this.Helper()

if !reflect.DeepEqual(this.actual, expected) {
Expand Down
16 changes: 8 additions & 8 deletions contracts_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ type InputModel interface {
// InputModel in some fashion or to otherwise short-circuit the request pipeline by returning a result to be rendered
// the caller's HTTP response stream. If a nil (meaning successful) result is returned, then processing continues.
type Reader interface {
Read(InputModel, *http.Request) interface{}
Read(InputModel, *http.Request) any
}

// ResultContainer sets the content on the underlying instance.
type ResultContainer interface {
SetContent(interface{})
Result() interface{}
SetContent(any)
Result() any
}

// Processor represents the mechanism used to carry out the desired instruction or user-provided intention. The second
Expand All @@ -56,30 +56,30 @@ type ResultContainer interface {
// and http.ResponseWriter. If the value returned is not one of the aforementioned types, it will be serialized using
// either the requested HTTP Accept type or it will use the default serializer configured, if any.
type Processor interface {
Process(context.Context, interface{}) interface{}
Process(context.Context, any) any
}

// Writer is responsible to render to result provided to the associated response stream.
type Writer interface {
Write(http.ResponseWriter, *http.Request, interface{})
Write(http.ResponseWriter, *http.Request, any)
}

// DeserializeBody is an interface that is optionally implemented by a given InputModel and is used to provide the
// target instance into which the HTTP request body will be deserialized.
type DeserializeBody interface {
Body() interface{}
Body() any
}

// Deserializer instances provide the ability to transform an opaque byte stream into an instance of a structure.
type Deserializer interface {
// Deserialize renders the decodes the source stream into the instance provided. If there are any problems, an error is returned.
Deserialize(interface{}, io.Reader) error
Deserialize(any, io.Reader) error
}

// Serializer instances provide the ability to transform an instance of a structure into a byte stream.
type Serializer interface {
// Serialize renders the instance provided to the io.Writer. If there are any problems, an error is returned.
Serialize(io.Writer, interface{}) error
Serialize(io.Writer, any) error
// ContentType returns HTTP Content-Type header that will be used when writing to the HTTP response.
ContentType() string
}
Expand Down
20 changes: 10 additions & 10 deletions contracts_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ type SerializeResult struct {
Headers map[string][]string

// Content, if provided, use this value, otherwise no content will be written to the response stream.
Content interface{}
Content any
}

func (this *SerializeResult) SetContent(value interface{}) { this.Content = value }
func (this *SerializeResult) Result() interface{} { return this }
func (this *SerializeResult) SetContent(value any) { this.Content = value }
func (this *SerializeResult) Result() any { return this }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -123,22 +123,22 @@ type fixedResultContainer struct{ ResultContainer }
type bindErrorContainer struct{ *SerializeResult }
type validationErrorContainer struct{ *SerializeResult }

func (this *fixedResultContainer) SetContent(interface{}) {} // no-op
func (this *fixedResultContainer) Result() interface{} { return this.ResultContainer }
func (this *fixedResultContainer) SetContent(any) {} // no-op
func (this *fixedResultContainer) Result() any { return this.ResultContainer }

func (this *bindErrorContainer) SetContent(value interface{}) {
func (this *bindErrorContainer) SetContent(value any) {
inputErrors := this.SerializeResult.Content.(*InputErrors)
inputErrors.Errors = inputErrors.Errors[0:0]
inputErrors.Errors = append(inputErrors.Errors, value.(error))
}
func (this *bindErrorContainer) Result() interface{} { return this.SerializeResult }
func (this *bindErrorContainer) Result() any { return this.SerializeResult }

func (this *validationErrorContainer) SetContent(value interface{}) {
func (this *validationErrorContainer) SetContent(value any) {
inputErrors := this.SerializeResult.Content.(*InputErrors)
inputErrors.Errors = inputErrors.Errors[0:0]
inputErrors.Errors = value.([]error)
}
func (this *validationErrorContainer) Result() interface{} { return this.SerializeResult }
func (this *validationErrorContainer) Result() any { return this.SerializeResult }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -215,7 +215,7 @@ var (
}
)

func _serializeJSON(instance interface{}) string {
func _serializeJSON(instance any) string {
raw, _ := json.Marshal(instance)
return string(raw)
}
4 changes: 2 additions & 2 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type semiPersistentHandler struct {
}

func newSemiPersistentHandler(options []option) http.Handler {
buffer := &sync.Pool{New: func() interface{} {
buffer := &sync.Pool{New: func() any {
// The config is a "shared nothing" style wherein each handler gets its own configuration values which include
// callbacks to stateful error writers and stateful serializers.
config := newConfig(options)
Expand Down Expand Up @@ -92,7 +92,7 @@ func (this *transientHandler) ServeHTTP(response http.ResponseWriter, request *h
result := this.process(request)
this.writer.Write(response, request, result)
}
func (this *transientHandler) process(request *http.Request) interface{} {
func (this *transientHandler) process(request *http.Request) any {
this.input.Reset()

for _, reader := range this.readers {
Expand Down
14 changes: 7 additions & 7 deletions handlers_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type configuration struct {
Readers []func() Reader
Writer func() Writer
NotAcceptableResult *TextResult
UnsupportedMediaTypeResult interface{}
UnsupportedMediaTypeResult any
DeserializationFailedResult func() ResultContainer
ParseFormFailedResult interface{}
ParseFormFailedResult any
BindFailedResult func() ResultContainer
ValidationFailedResult func() ResultContainer
Monitor Monitor
Expand All @@ -38,7 +38,7 @@ func newConfig(options []option) configuration {
return this
}

// InputModel provides the callback that returns a unique instance on each invocation.
// InputModel provides the callback that returns a unique instance of an InputModel on each invocation.
func (singleton) InputModel(value func() InputModel) option {
return func(this *configuration) { this.InputModel = value }
}
Expand All @@ -49,7 +49,7 @@ func (singleton) ProcessorSharedInstance(value Processor) option {
return Options.Processor(func() Processor { return value })
}

// Processor is used when an long-lived, reusable, and stateful processor (and associated component tree) is created to
// Processor is used when a long-lived, reusable, and stateful processor (and associated component tree) is created to
// service many requests, each request going through a pooled instance of that processor.
func (singleton) Processor(value func() Processor) option {
return func(this *configuration) { this.Processor = value }
Expand Down Expand Up @@ -195,7 +195,7 @@ func (singleton) Writer(value func() Writer) option {
// UnsupportedMediaTypeResult registers the result to be written to the underlying HTTP response stream to indicate when
// the values in the provided Content-Type HTTP request header are not recognized. A single, shared instance of this
// instance can be provided across all routes.
func (singleton) UnsupportedMediaTypeResult(value interface{}) option {
func (singleton) UnsupportedMediaTypeResult(value any) option {
return func(this *configuration) { this.UnsupportedMediaTypeResult = value }
}

Expand All @@ -207,7 +207,7 @@ func (singleton) DeserializationFailedResult(value func() ResultContainer) optio

// ParseFormFailedResult registers the result to be written to the underlying HTTP response stream to indicate when
// parsing the form and query fields of the HTTP request have failed.
func (singleton) ParseFormFailedResult(value interface{}) option {
func (singleton) ParseFormFailedResult(value any) option {
return func(this *configuration) { this.ParseFormFailedResult = value }
}

Expand Down Expand Up @@ -313,7 +313,7 @@ var Options singleton
type nop struct{}
type nopMonitor struct{}

func (*nop) Process(context.Context, interface{}) interface{} { return nil }
func (*nop) Process(context.Context, any) any { return nil }

func (*nop) Reset() {}
func (*nop) Bind(*http.Request) error { return nil }
Expand Down
18 changes: 9 additions & 9 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ type FakeReader struct {
t *testing.T
expectedRequest *http.Request
callSequence int
result interface{}
result any
}

func newFakeReader(t *testing.T, expectedSequence int, result interface{}, request *http.Request) Reader {
func newFakeReader(t *testing.T, expectedSequence int, result any, request *http.Request) Reader {
return &FakeReader{t: t, callSequence: expectedSequence, result: result, expectedRequest: request}
}

func (this *FakeReader) Read(input InputModel, request *http.Request) interface{} {
func (this *FakeReader) Read(input InputModel, request *http.Request) any {
sequential := input.(*FakeSequentialInputModel)
Assert(this.t).That(this.expectedRequest).Equals(request)
Assert(this.t).That(this.callSequence).Equals(sequential.ID)
Expand All @@ -73,14 +73,14 @@ func (this *FakeSequentialInputModel) Validate([]error) int { return 0 }
type FakeProcessor struct {
t *testing.T
expectedCtx context.Context
expectedInput interface{}
result interface{}
expectedInput any
result any
}

func newFakeProcessor(t *testing.T, expectedCtx context.Context, expectedInput, result interface{}) *FakeProcessor {
func newFakeProcessor(t *testing.T, expectedCtx context.Context, expectedInput, result any) *FakeProcessor {
return &FakeProcessor{t: t, expectedCtx: expectedCtx, expectedInput: expectedInput, result: result}
}
func (this *FakeProcessor) Process(ctx context.Context, input interface{}) interface{} {
func (this *FakeProcessor) Process(ctx context.Context, input any) any {
Assert(this.t).That(ctx).Equals(this.expectedCtx)
Assert(this.t).That(input).Equals(this.expectedInput)
return this.result
Expand All @@ -92,14 +92,14 @@ type FakeCaptureWriter struct {
t *testing.T
response http.ResponseWriter
request *http.Request
result interface{}
result any
}

func newFakeCaptureWriter(t *testing.T, response http.ResponseWriter, request *http.Request) *FakeCaptureWriter {
return &FakeCaptureWriter{t: t, response: response, request: request}
}

func (this *FakeCaptureWriter) Write(response http.ResponseWriter, request *http.Request, result interface{}) {
func (this *FakeCaptureWriter) Write(response http.ResponseWriter, request *http.Request, result any) {
Assert(this.t).That(response).Equals(this.response)
Assert(this.t).That(request).Equals(this.request)
this.result = result
Expand Down
22 changes: 11 additions & 11 deletions readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type acceptReader struct {
acceptable map[string][]string
result interface{}
result any
useDefaultIfAcceptNotFound bool
maxAcceptTypes int
monitor Monitor
Expand All @@ -28,7 +28,7 @@ func newAcceptReader(serializerFactories map[string]func() Serializer, result *T
}
}

func (this *acceptReader) Read(_ InputModel, request *http.Request) interface{} {
func (this *acceptReader) Read(_ InputModel, request *http.Request) any {
if normalized, found := this.findAcceptType(request.Header[headerAccept]); !found {
this.monitor.NotAcceptable()
return this.result
Expand Down Expand Up @@ -87,12 +87,12 @@ func normalizeMediaType(value string) string {

type deserializeReader struct {
available map[string]Deserializer
unsupportedMediaTypeResult interface{}
unsupportedMediaTypeResult any
result ResultContainer
monitor Monitor
}

func newDeserializeReader(deserializerFactories map[string]func() Deserializer, unsupportedMediaTypeResult interface{}, result ResultContainer, monitor Monitor) Reader {
func newDeserializeReader(deserializerFactories map[string]func() Deserializer, unsupportedMediaTypeResult any, result ResultContainer, monitor Monitor) Reader {
available := make(map[string]Deserializer, len(deserializerFactories))
for contentType, factory := range deserializerFactories {
available[contentType] = factory()
Expand All @@ -106,10 +106,10 @@ func newDeserializeReader(deserializerFactories map[string]func() Deserializer,
}
}

func (this *deserializeReader) Read(input InputModel, request *http.Request) interface{} {
func (this *deserializeReader) Read(input InputModel, request *http.Request) any {
this.monitor.Deserialize()

var target interface{} = input
var target any = input
if value, ok := input.(DeserializeBody); ok {
target = value.Body()
}
Expand Down Expand Up @@ -138,18 +138,18 @@ func (this *deserializeReader) loadDeserializer(contentTypes []string) Deseriali
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

type parseFormReader struct {
result interface{}
result any
monitor Monitor
}

func newParseFormReader(result interface{}, monitor Monitor) Reader {
func newParseFormReader(result any, monitor Monitor) Reader {
return &parseFormReader{
result: result,
monitor: monitor,
}
}

func (this *parseFormReader) Read(_ InputModel, request *http.Request) interface{} {
func (this *parseFormReader) Read(_ InputModel, request *http.Request) any {
this.monitor.ParseForm()
if err := request.ParseForm(); err != nil {
this.monitor.ParseFormFailed(err)
Expand All @@ -170,7 +170,7 @@ func newBindReader(result ResultContainer, monitor Monitor) Reader {
return &bindReader{result: result, monitor: monitor}
}

func (this *bindReader) Read(target InputModel, request *http.Request) interface{} {
func (this *bindReader) Read(target InputModel, request *http.Request) any {
this.monitor.Bind()
if err := target.Bind(request); err != nil {
this.monitor.BindFailed(err)
Expand All @@ -193,7 +193,7 @@ func newValidateReader(result ResultContainer, bufferSize int, monitor Monitor)
return &validateReader{result: result, buffer: make([]error, bufferSize), monitor: monitor}
}

func (this *validateReader) Read(target InputModel, _ *http.Request) interface{} {
func (this *validateReader) Read(target InputModel, _ *http.Request) any {
this.monitor.Validate()
if count := target.Validate(this.buffer); count > 0 {
errs := this.buffer[0:count]
Expand Down
14 changes: 7 additions & 7 deletions readers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestDeserializeReader_KnownAdvancedContentType_Success(t *testing.T) {
func TestDeserializeReader_XMLKnownAdvancedContentType_Success(t *testing.T) {
assertDeserializeReader(t, nil, []string{"application/xml; charset=utf-8"}, nil)
}
func assertDeserializeReader(t *testing.T, expectedResult interface{}, contentTypes []string, deserializeError error) {
func assertDeserializeReader(t *testing.T, expectedResult any, contentTypes []string, deserializeError error) {
input := &FakeInputModel{}
request := httptest.NewRequest("GET", "/", nil)
request.Header["Content-Type"] = contentTypes
Expand Down Expand Up @@ -185,25 +185,25 @@ func (this *FakeInputModel) Validate(errs []error) int {
return len(this.validationErrors)
}

func (this *FakeInputModel) Body() interface{} { return this }
func (this *FakeInputModel) Body() any { return this }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

type FakeDeserializer struct {
source io.Reader
target interface{}
target any
err error
}

func (this *FakeDeserializer) Deserialize(target interface{}, source io.Reader) error {
func (this *FakeDeserializer) Deserialize(target any, source io.Reader) error {
this.target = target
this.source = source
return this.err
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

type FakeContentResult struct{ value interface{} }
type FakeContentResult struct{ value any }

func (this *FakeContentResult) SetContent(value interface{}) { this.value = value }
func (this *FakeContentResult) Result() interface{} { return this }
func (this *FakeContentResult) SetContent(value any) { this.value = value }
func (this *FakeContentResult) Result() any { return this }
Loading

0 comments on commit 2957920

Please sign in to comment.