use the goverter:ignore directive to ignore fields in the generated code. This is useful when you want to ignore certain fields in the generated code.

This commit is contained in:
Tigor Hutasuhut 2024-08-04 20:53:51 +07:00
parent fb44efec56
commit 97e29ff94d
3 changed files with 58 additions and 6 deletions

View file

@ -9,10 +9,11 @@ import (
// goverter:extend BoolToInt32
type Converter interface {
// goverter:map Nsfw NSFW
// goverter:ignore CreatedAt
// goverter:ignore UpdatedAt
// goverter:ignore R
ConvertCreateDeviceRequestToModelsDevice(source device.CreateDeviceRequest) *models.Device
// goverter:ignore CreatedAt UpdatedAt R
CreateDeviceRequestToModelsDevice(source *device.CreateDeviceRequest) *models.Device
// goverter:ignore state sizeCache unknownFields
ModelsDeviceToCreateDeviceResponse(source *models.Device) *device.CreateDeviceResponse
}
func BoolToInt32(input bool) int32 {

View file

@ -3,6 +3,8 @@ package errs
import (
"errors"
"net/http"
"connectrpc.com/connect"
)
func FindCodeOrDefault(err error, def int) int {
@ -25,6 +27,29 @@ func FindCodeOrDefault(err error, def int) int {
return def
}
func ExtractConnectCode(err error) connect.Code {
code := FindCodeOrDefault(err, 500)
if code >= 500 {
return connect.CodeInternal
}
switch code {
case http.StatusNotFound:
return connect.CodeNotFound
case http.StatusConflict:
return connect.CodeAlreadyExists
case http.StatusBadRequest:
return connect.CodeInvalidArgument
case http.StatusForbidden:
return connect.CodePermissionDenied
case http.StatusFailedDependency:
return connect.CodeUnavailable
case http.StatusTooManyRequests:
return connect.CodeResourceExhausted
default:
return connect.CodeUnknown
}
}
func FindMessage(err error) string {
if messager, ok := err.(interface{ GetMessage() string }); ok {
message := messager.GetMessage()

View file

@ -4,12 +4,25 @@ import (
"context"
"connectrpc.com/connect"
"github.com/bufbuild/protovalidate-go"
"github.com/tigorlazuardi/redmage/api"
"github.com/tigorlazuardi/redmage/gen/converter"
device "github.com/tigorlazuardi/redmage/gen/proto/device/v1"
deviceConnect "github.com/tigorlazuardi/redmage/gen/proto/device/v1/v1connect"
"github.com/tigorlazuardi/redmage/pkg/errs"
)
var _ deviceConnect.DeviceServiceHandler = (*Device)(nil)
var (
_ deviceConnect.DeviceServiceHandler = (*Device)(nil)
convert converter.ConverterImpl
validator = func() *protovalidate.Validator {
v, err := protovalidate.New()
if err != nil {
panic(err)
}
return v
}()
)
type Device struct {
deviceConnect.UnimplementedDeviceServiceHandler
@ -21,5 +34,18 @@ func (de *Device) CreateDevice(ctx context.Context, req *connect.Request[device.
ctx, span := tracer.Start(ctx, "Device.CreateDevice")
defer span.End()
panic("not implemented") // TODO: Implement
if err := validator.Validate(req.Msg); err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
}
requestDao := convert.CreateDeviceRequestToModelsDevice(req.Msg)
dev, err := de.API.DevicesCreate(ctx, requestDao)
if err != nil {
code := errs.ExtractConnectCode(err)
return nil, connect.NewError(code, err)
}
createResponse := convert.ModelsDeviceToCreateDeviceResponse(dev)
return connect.NewResponse(createResponse), nil
}