From e210a02401978a631e81b556ba7c981338caa533 Mon Sep 17 00:00:00 2001 From: Paul Querna Date: Wed, 6 Jan 2021 09:36:52 -0800 Subject: [PATCH] Add deleted_at -> deleted:true|false support --- Makefile | 2 +- examplepb/example.proto | 7 ++++--- internal/pgd/module.go | 18 ++++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index b38442d..9244cdf 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ generate: dynamo/*.proto example: - DEBUG_PGD=true protoc example.proto --proto_path=. --proto_path=examplepb --go_out="paths=source_relative:examplepb" --dynamo_out="lang=go,paths=source_relative:examplepb" + DEBUG_PGD=true protoc example.proto --proto_path=. --proto_path=examplepb --go_out="plugins=grpc,paths=source_relative:examplepb" --dynamo_out="lang=go,paths=source_relative:examplepb" .PHONY: adddep adddep: diff --git a/examplepb/example.proto b/examplepb/example.proto index c33ce85..1ccbcec 100644 --- a/examplepb/example.proto +++ b/examplepb/example.proto @@ -104,7 +104,8 @@ message User { string id = 2; google.protobuf.Timestamp created_at = 3; google.protobuf.Timestamp updated_at = 4; - string idp_id = 5; - string display_name = 6; - string email = 7; + google.protobuf.Timestamp deleted_at = 5; + string idp_id = 6; + string display_name = 7; + string email = 8; } diff --git a/internal/pgd/module.go b/internal/pgd/module.go index ce92073..357d8f6 100644 --- a/internal/pgd/module.go +++ b/internal/pgd/module.go @@ -356,8 +356,9 @@ func generateKeyStringer(msg pgs.Message, stmts []jen.Code, ck *dynamopb.Key, st } const ( - valueField = "value" - typeField = "typ" + valueField = "value" + deletedField = "deleted" + typeField = "typ" ) func (m *Module) applyMarshal(f *jen.File, in pgs.File) error { @@ -465,6 +466,19 @@ func (m *Module) applyMarshal(f *jen.File, in pgs.File) error { stmts = append(stmts, jen.Id(vname).Dot("S").Op("=").Qual(awsPkg, "String").Call(jen.Lit(typeName))) d[jen.Lit(typeField)] = jen.Id(vname) + for _, field := range msg.Fields() { + fieldDescriptorName := field.Descriptor().GetTypeName() + if strings.HasSuffix(fieldDescriptorName, "google.protobuf.Timestamp") && + field.Name().LowerSnakeCase().String() == "deleted_at" { + srcName := field.Name().UpperCamelCase().String() + refId++ + vname = fmt.Sprintf("v%d", refId) + stmts = append(stmts, jen.Id(vname).Op(":=").Op("&").Qual(dynamoPkg, "AttributeValue").Values()) + stmts = append(stmts, jen.Id(vname).Dot("BOOL").Op("=").Qual(awsPkg, "Bool").Call(jen.Id("p").Dot(srcName).Dot("IsValid").Call())) + d[jen.Lit(deletedField)] = jen.Id(vname) + } + } + for _, field := range msg.Fields() { fext := dynamopb.DynamoFieldOptions{} ok, err := field.Extension(dynamopb.E_Field, &fext)