From f5b7f31dfefc95733790a499f0a45153ac4cc6dc Mon Sep 17 00:00:00 2001 From: kdevo <10863868+kdevo@users.noreply.github.com> Date: Sat, 3 Aug 2024 14:01:29 +0200 Subject: [PATCH 1/3] entproto: fix service.List pageToken for custom fields (UUID/string) The current List service template generates faulty code if a custom GoType field is used (currently only UUID and string supported). While not tested on a large set of inputs, using a call to the `field_to_ent` template hopefully leads to a more sane behaviour, as it is also used for Get() and Update() function calls. --- entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl b/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl index 053233f89..c6339fdca 100644 --- a/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl +++ b/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl @@ -29,12 +29,9 @@ {{- template "field_to_ent" dict "Field" .G.FieldMap.ID "VarName" "pageToken" "Ident" "token" }} {{- else if .G.EntType.ID.IsUUID }} - pageToken, err := {{ qualify "github.com/google/uuid" "ParseBytes" }}(bytes) - if err != nil { - return nil, status.Errorf(codes.InvalidArgument, "page token is invalid") - } + {{- template "field_to_ent" dict "Field" .G.FieldMap.ID "VarName" "pageToken" "Ident" "bytes" }} {{- else if .G.EntType.ID.IsString }} - pageToken := string(bytes) + {{- template "field_to_ent" dict "Field" .G.FieldMap.ID "VarName" "pageToken" "Ident" "bytes" }} {{- end }} listQuery = listQuery. Where({{ qualify (print (unquote .G.EntPackage.String) "/" .G.EntType.Package) "IDLTE" }}(pageToken)) From 758d579b42687c643ee5e84ccd124209d1344e38 Mon Sep 17 00:00:00 2001 From: kdevo <10863868+kdevo@users.noreply.github.com> Date: Sat, 3 Aug 2024 14:25:52 +0200 Subject: [PATCH 2/3] entproto: regenerate todo example --- .../todo/ent/proto/entpb/entpb_attachment_service.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go b/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go index 407aa4756..0869ba04a 100644 --- a/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go +++ b/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go @@ -203,9 +203,9 @@ func (svc *AttachmentService) List(ctx context.Context, req *ListAttachmentReque if err != nil { return nil, status.Errorf(codes.InvalidArgument, "page token is invalid") } - pageToken, err := uuid.ParseBytes(bytes) - if err != nil { - return nil, status.Errorf(codes.InvalidArgument, "page token is invalid") + var pageToken uuid.UUID + if err := (&pageToken).UnmarshalBinary(bytes); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "invalid argument: %s", err) } listQuery = listQuery. Where(attachment.IDLTE(pageToken)) From 61c53a1515eb401898d209ad06af0fcf896c458d Mon Sep 17 00:00:00 2001 From: kdevo <10863868+kdevo@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:09:32 +0200 Subject: [PATCH 3/3] entproto: workaround for List template to preserve backward-compat ...when using a custom GoType. This should keep the backward-compatibility by first parsing the UUID represented as string (like before) and then calling `field_to_ent`. --- entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl | 6 +++++- .../todo/ent/proto/entpb/entpb_attachment_service.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl b/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl index c6339fdca..d62b5910d 100644 --- a/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl +++ b/entproto/cmd/protoc-gen-entgrpc/template/method_list.tmpl @@ -29,7 +29,11 @@ {{- template "field_to_ent" dict "Field" .G.FieldMap.ID "VarName" "pageToken" "Ident" "token" }} {{- else if .G.EntType.ID.IsUUID }} - {{- template "field_to_ent" dict "Field" .G.FieldMap.ID "VarName" "pageToken" "Ident" "bytes" }} + token, err := {{ qualify "github.com/google/uuid" "ParseBytes" }}(bytes) + if err != nil { + return nil, status.Errorf(codes.InvalidArgument, "page token is invalid") + } + {{- template "field_to_ent" dict "Field" .G.FieldMap.ID "VarName" "pageToken" "Ident" "[]byte(token[:])" }} {{- else if .G.EntType.ID.IsString }} {{- template "field_to_ent" dict "Field" .G.FieldMap.ID "VarName" "pageToken" "Ident" "bytes" }} {{- end }} diff --git a/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go b/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go index 0869ba04a..a474940a2 100644 --- a/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go +++ b/entproto/internal/todo/ent/proto/entpb/entpb_attachment_service.go @@ -203,8 +203,12 @@ func (svc *AttachmentService) List(ctx context.Context, req *ListAttachmentReque if err != nil { return nil, status.Errorf(codes.InvalidArgument, "page token is invalid") } + token, err := uuid.ParseBytes(bytes) + if err != nil { + return nil, status.Errorf(codes.InvalidArgument, "page token is invalid") + } var pageToken uuid.UUID - if err := (&pageToken).UnmarshalBinary(bytes); err != nil { + if err := (&pageToken).UnmarshalBinary([]byte(token[:])); err != nil { return nil, status.Errorf(codes.InvalidArgument, "invalid argument: %s", err) } listQuery = listQuery.