Skip to content

Commit

Permalink
Merge pull request #9 from pquerna/pq/deleted_at
Browse files Browse the repository at this point in the history
Add deleted_at -> deleted:true|false support
  • Loading branch information
pquerna authored Jan 6, 2021
2 parents bb4eb39 + e210a02 commit 627e157
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions examplepb/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 16 additions & 2 deletions internal/pgd/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 627e157

Please sign in to comment.