Skip to content

Commit 544aadc

Browse files
committed
go 1.18
1 parent 7bb6365 commit 544aadc

File tree

9 files changed

+20
-17
lines changed

9 files changed

+20
-17
lines changed

calling/calling.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
type WithStringArgsFunc func(args ...string)
99
type WithStringArgsErrorFunc func(args ...string) error
1010

11-
func WithStringArgs(function interface{}) WithStringArgsFunc {
11+
func WithStringArgs(function any) WithStringArgsFunc {
1212
v := reflect.ValueOf(function)
1313
t := v.Type()
1414
if t.Kind() != reflect.Func {
@@ -40,7 +40,7 @@ func WithStringArgs(function interface{}) WithStringArgsFunc {
4040

4141
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
4242

43-
func WithStringArgsError(function interface{}) WithStringArgsErrorFunc {
43+
func WithStringArgsError(function any) WithStringArgsErrorFunc {
4444
v := reflect.ValueOf(function)
4545
t := v.Type()
4646
if t.Kind() != reflect.Func {

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/ungerik/go-httpx
22

3-
go 1.17
3+
go 1.18

httperr/handler.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ type Handler interface {
1313
type HandlerFunc func(err error, writer http.ResponseWriter, request *http.Request) (handled bool)
1414

1515
func (f HandlerFunc) HandleError(err error, writer http.ResponseWriter, request *http.Request) (handled bool) {
16+
if err == nil {
17+
return false
18+
}
1619
return f(err, writer, request)
1720
}
1821

@@ -25,7 +28,7 @@ func Handle(err error, writer http.ResponseWriter, request *http.Request) (handl
2528
}
2629

2730
// HandlePanic will call DefaultHandler.HandleError(AsError(recoverResult), writer, request)
28-
func HandlePanic(recoverResult interface{}, writer http.ResponseWriter, request *http.Request) (handled bool) {
31+
func HandlePanic(recoverResult any, writer http.ResponseWriter, request *http.Request) (handled bool) {
2932
return Handle(AsError(recoverResult), writer, request)
3033
}
3134

@@ -87,7 +90,7 @@ func WriteHandler(err error, writer http.ResponseWriter, request *http.Request)
8790
// If Logger is not nil, then it will be used to log an error message.
8891
// If DebugShowInternalErrorsInResponse is true, then the error message
8992
// will be shown in the response body, else only "Internal Server Error" will be used.
90-
func WriteInternalServerError(err interface{}, writer http.ResponseWriter) {
93+
func WriteInternalServerError(err any, writer http.ResponseWriter) {
9194
message := http.StatusText(http.StatusInternalServerError)
9295
if DebugShowInternalErrorsInResponse {
9396
message += fmt.Sprintf(DebugShowInternalErrorsInResponseFormat, err)

httperr/json.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// response body using the passed statusCode.
1313
// If err could not be marshalled as JSON, then an internal server error
1414
// will be written instead using WriteInternalServerError with a wrapped erorr message.
15-
func WriteAsJSON(err interface{}, statusCode int, writer http.ResponseWriter) {
15+
func WriteAsJSON(err any, statusCode int, writer http.ResponseWriter) {
1616
body, e := json.MarshalIndent(err, "", " ")
1717
if e != nil {
1818
e = fmt.Errorf("can't marshall error of type %T as JSON because: %w", err, e)
@@ -31,7 +31,7 @@ func WriteAsJSON(err interface{}, statusCode int, writer http.ResponseWriter) {
3131
// and the passed body marshalled as JSON.
3232
// Pass a json.RawMessage as body if the error
3333
// message is already in JSON format.
34-
func JSON(statusCode int, body interface{}) Response {
34+
func JSON(statusCode int, body any) Response {
3535
return statusCodeAndJSON{
3636
statusCode: statusCode,
3737
body: body,
@@ -40,7 +40,7 @@ func JSON(statusCode int, body interface{}) Response {
4040

4141
type statusCodeAndJSON struct {
4242
statusCode int
43-
body interface{}
43+
body any
4444
}
4545

4646
func (e statusCodeAndJSON) Error() string {

httperr/response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func New(statusCode int, statusText ...string) Response {
2525
}
2626
}
2727

28-
func Errorf(statusCode int, format string, a ...interface{}) Response {
28+
func Errorf(statusCode int, format string, a ...any) Response {
2929
return statusCodeAndText{
3030
statusCode: statusCode,
3131
statusText: fmt.Sprintf(format, a...),

httperr/wrap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (e errDontLog) Unwrap() error {
4343
// AsError converts val to an error by either casting val to error if possible,
4444
// or using its string value or String method as error message,
4545
// or using fmt.Errorf("%+v", val) to format the value as error.
46-
func AsError(val interface{}) error {
46+
func AsError(val any) error {
4747
switch x := val.(type) {
4848
case nil:
4949
return nil

logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package httpx
22

33
type Logger interface {
4-
Printf(format string, args ...interface{})
4+
Printf(format string, args ...any)
55
}

respond/json.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/ungerik/go-httpx/httperr"
99
)
1010

11-
type JSON func(http.ResponseWriter, *http.Request) (response interface{}, err error)
11+
type JSON func(http.ResponseWriter, *http.Request) (response any, err error)
1212

1313
func (handlerFunc JSON) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
1414
if CatchPanics {
@@ -25,7 +25,7 @@ func (handlerFunc JSON) ServeHTTP(writer http.ResponseWriter, request *http.Requ
2525
WriteJSON(writer, response)
2626
}
2727

28-
func WriteJSON(writer http.ResponseWriter, response interface{}) {
28+
func WriteJSON(writer http.ResponseWriter, response any) {
2929
b, err := EncodeJSON(response)
3030
if err != nil {
3131
httperr.WriteInternalServerError(err, writer)
@@ -35,7 +35,7 @@ func WriteJSON(writer http.ResponseWriter, response interface{}) {
3535
writer.Write(b) //#nosec G104
3636
}
3737

38-
func EncodeJSON(response interface{}) ([]byte, error) {
38+
func EncodeJSON(response any) ([]byte, error) {
3939
if PrettyPrint {
4040
return json.MarshalIndent(response, "", PrettyPrintIndent)
4141
}

respond/xml.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/ungerik/go-httpx/httperr"
99
)
1010

11-
type XML func(http.ResponseWriter, *http.Request) (response interface{}, err error)
11+
type XML func(http.ResponseWriter, *http.Request) (response any, err error)
1212

1313
func (handlerFunc XML) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
1414
if CatchPanics {
@@ -25,7 +25,7 @@ func (handlerFunc XML) ServeHTTP(writer http.ResponseWriter, request *http.Reque
2525
WriteXML(writer, response)
2626
}
2727

28-
func WriteXML(writer http.ResponseWriter, response interface{}) {
28+
func WriteXML(writer http.ResponseWriter, response any) {
2929
b, err := EncodeXML(response)
3030
if err != nil {
3131
httperr.WriteInternalServerError(err, writer)
@@ -36,7 +36,7 @@ func WriteXML(writer http.ResponseWriter, response interface{}) {
3636
writer.Write(b) //#nosec G104
3737
}
3838

39-
func EncodeXML(response interface{}) ([]byte, error) {
39+
func EncodeXML(response any) ([]byte, error) {
4040
if PrettyPrint {
4141
return xml.MarshalIndent(response, "", PrettyPrintIndent)
4242
}

0 commit comments

Comments
 (0)