Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

story(issue-31): migrate to edition 2023 #32

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Go Reference](https://pkg.go.dev/badge/github.com/z5labs/humus.svg)](https://pkg.go.dev/github.com/z5labs/humus)
[![Go Report Card](https://goreportcard.com/badge/github.com/z5labs/humus)](https://goreportcard.com/report/github.com/z5labs/humus)
![Coverage](https://img.shields.io/badge/Coverage-55.3%25-yellow)
![Coverage](https://img.shields.io/badge/Coverage-55.9%25-yellow)
[![build](https://github.com/z5labs/humus/actions/workflows/build.yaml/badge.svg)](https://github.com/z5labs/humus/actions/workflows/build.yaml)

**humus one stop shop framework for all Z5Labs projects in Go.**
4 changes: 2 additions & 2 deletions example/rest-petstore/endpoint/delete_pet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestDeletePet(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.Code) {
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestDeletePet(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.Code) {
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.GetCode()) {
return
}
})
Expand Down
6 changes: 3 additions & 3 deletions example/rest-petstore/endpoint/find_pet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestFindPetByID(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.Code) {
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestFindPetByID(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.Code) {
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestFindPetByID(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_NOT_FOUND, status.Code) {
if !assert.Equal(t, humuspb.Code_NOT_FOUND, status.GetCode()) {
return
}
})
Expand Down
53 changes: 21 additions & 32 deletions humuspb/humus.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion humus.proto → humuspb/humus.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
// protoc -I . -I $PATH_TO_PROTOBUF_SRC --go_out . --go_opt=module=github.com/z5labs/humus humus.proto

syntax = "proto3";
edition = "2023";

package humus;

Expand Down
2 changes: 1 addition & 1 deletion humuspb/humuspb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// Error implements the [error] interface.
func (s *Status) Error() string {
return fmt.Sprintf("status: %s: %s", s.Code, s.Message)
return fmt.Sprintf("status: %s: %s", s.GetCode(), s.GetMessage())
}

var httpToStatusCode = map[int]Code{
Expand Down
35 changes: 18 additions & 17 deletions rest/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/z5labs/humus/humuspb"

"github.com/z5labs/bedrock/pkg/ptr"
"github.com/z5labs/bedrock/rest/endpoint"
"go.opentelemetry.io/otel"
"google.golang.org/protobuf/proto"
Expand All @@ -23,8 +24,8 @@ import (
// Error
func Error(status int, message string, details ...*anypb.Any) *humuspb.Status {
return &humuspb.Status{
Code: humuspb.HttpCodeToStatusCode(status),
Message: message,
Code: humuspb.HttpCodeToStatusCode(status).Enum(),
Message: &message,
Details: details,
}
}
Expand All @@ -38,7 +39,7 @@ func (h *errHandler) HandleError(ctx context.Context, w http.ResponseWriter, err
defer span.End()

status := mapErrorToStatus(err)
httpCode := humuspb.StatusCodeToHttpCode(status.Code)
httpCode := humuspb.StatusCodeToHttpCode(status.GetCode())
b, err := h.marshal(status)
if err != nil {
span.RecordError(err)
Expand All @@ -64,38 +65,38 @@ func mapErrorToStatus(err error) *humuspb.Status {
return e
case endpoint.InvalidHeaderError:
return &humuspb.Status{
Code: humuspb.Code_INVALID_ARGUMENT,
Message: fmt.Sprintf("invalid header: %s", e.Header),
Code: humuspb.Code_INVALID_ARGUMENT.Enum(),
Message: ptr.Ref(fmt.Sprintf("invalid header: %s", e.Header)),
}
case endpoint.InvalidPathParamError:
return &humuspb.Status{
Code: humuspb.Code_INVALID_ARGUMENT,
Message: fmt.Sprintf("invalid path param: %s", e.Param),
Code: humuspb.Code_INVALID_ARGUMENT.Enum(),
Message: ptr.Ref(fmt.Sprintf("invalid path param: %s", e.Param)),
}
case endpoint.InvalidQueryParamError:
return &humuspb.Status{
Code: humuspb.Code_INVALID_ARGUMENT,
Message: fmt.Sprintf("invalid query param: %s", e.Param),
Code: humuspb.Code_INVALID_ARGUMENT.Enum(),
Message: ptr.Ref(fmt.Sprintf("invalid query param: %s", e.Param)),
}
case endpoint.MissingRequiredHeaderError:
return &humuspb.Status{
Code: humuspb.Code_FAILED_PRECONDITION,
Message: fmt.Sprintf("missing required header: %s", e.Header),
Code: humuspb.Code_FAILED_PRECONDITION.Enum(),
Message: ptr.Ref(fmt.Sprintf("missing required header: %s", e.Header)),
}
case endpoint.MissingRequiredPathParamError:
return &humuspb.Status{
Code: humuspb.Code_FAILED_PRECONDITION,
Message: fmt.Sprintf("missing required path param: %s", e.Param),
Code: humuspb.Code_FAILED_PRECONDITION.Enum(),
Message: ptr.Ref(fmt.Sprintf("missing required path param: %s", e.Param)),
}
case endpoint.MissingRequiredQueryParamError:
return &humuspb.Status{
Code: humuspb.Code_FAILED_PRECONDITION,
Message: fmt.Sprintf("missing required query param: %s", e.Param),
Code: humuspb.Code_FAILED_PRECONDITION.Enum(),
Message: ptr.Ref(fmt.Sprintf("missing required query param: %s", e.Param)),
}
default:
return &humuspb.Status{
Code: humuspb.Code_INTERNAL,
Message: "internal error",
Code: humuspb.Code_INTERNAL.Enum(),
Message: ptr.Ref("internal error"),
}
}
}
19 changes: 10 additions & 9 deletions rest/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_INTERNAL, status.Code) {
if !assert.Equal(t, humuspb.Code_INTERNAL, status.GetCode()) {
return
}
})

t.Run("if the status code is not recognized by humuspb", func(t *testing.T) {
statusCode := humuspb.Code(-1)
h := errorEndpointHandler{
err: &humuspb.Status{Code: -1},
err: &humuspb.Status{Code: &statusCode},
}

app := New(
Expand Down Expand Up @@ -260,7 +261,7 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code(-1), status.Code) {
if !assert.Equal(t, statusCode, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -371,7 +372,7 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.Code) {
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -478,7 +479,7 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.Code) {
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -587,7 +588,7 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.Code) {
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -696,7 +697,7 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.Code) {
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -803,7 +804,7 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.Code) {
if !assert.Equal(t, humuspb.Code_INVALID_ARGUMENT, status.GetCode()) {
return
}
})
Expand Down Expand Up @@ -910,7 +911,7 @@ func TestErrHandler_HandleError(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.Code) {
if !assert.Equal(t, humuspb.Code_FAILED_PRECONDITION, status.GetCode()) {
return
}
})
Expand Down
4 changes: 2 additions & 2 deletions rest/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestReadinessEndpoint(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_UNAVAILABLE, status.Code) {
if !assert.Equal(t, humuspb.Code_UNAVAILABLE, status.GetCode()) {
return
}
if !assert.NotEmpty(t, status.Message) {
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestLivenessEndpoint(t *testing.T) {
if !assert.Nil(t, err) {
return
}
if !assert.Equal(t, humuspb.Code_UNAVAILABLE, status.Code) {
if !assert.Equal(t, humuspb.Code_UNAVAILABLE, status.GetCode()) {
return
}
if !assert.NotEmpty(t, status.Message) {
Expand Down