From f2e770d4cc211f05e5aab0eb44feebfc4eb0f75f Mon Sep 17 00:00:00 2001 From: Caleb Horst Date: Tue, 7 Aug 2018 16:59:04 -0700 Subject: [PATCH] Add error if a read is attempted when no primary key (id) is set (#110) * Add error if a read is attempted when no primary key (id) is set * Regen protos with upstream changes --- example/feature_demo/demo_multi_file.pb.gorm.go | 3 +++ example/feature_demo/demo_service.pb.gorm.go | 3 +++ example/feature_demo/demo_types.pb.gorm.go | 12 ++++++++++++ example/user/user.pb.gorm.go | 15 +++++++++++++++ plugin/handlergen.go | 8 ++++++++ 5 files changed, 41 insertions(+) diff --git a/example/feature_demo/demo_multi_file.pb.gorm.go b/example/feature_demo/demo_multi_file.pb.gorm.go index bcc20337..91a7838a 100644 --- a/example/feature_demo/demo_multi_file.pb.gorm.go +++ b/example/feature_demo/demo_multi_file.pb.gorm.go @@ -146,6 +146,9 @@ func DefaultReadExternalChild(ctx context.Context, in *ExternalChild, db *gorm1. if err != nil { return nil, err } + if ormParams.Id == "" { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := ExternalChildORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err diff --git a/example/feature_demo/demo_service.pb.gorm.go b/example/feature_demo/demo_service.pb.gorm.go index c0e675c6..d540e735 100644 --- a/example/feature_demo/demo_service.pb.gorm.go +++ b/example/feature_demo/demo_service.pb.gorm.go @@ -119,6 +119,9 @@ func DefaultReadIntPoint(ctx context.Context, in *IntPoint, db *gorm1.DB) (*IntP if err != nil { return nil, err } + if ormParams.Id == 0 { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := IntPointORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err diff --git a/example/feature_demo/demo_types.pb.gorm.go b/example/feature_demo/demo_types.pb.gorm.go index 7da94b5b..e809e1b8 100644 --- a/example/feature_demo/demo_types.pb.gorm.go +++ b/example/feature_demo/demo_types.pb.gorm.go @@ -828,6 +828,9 @@ func DefaultReadTypeWithID(ctx context.Context, in *TypeWithID, db *gorm1.DB) (* if err != nil { return nil, err } + if ormParams.Id == 0 { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := TypeWithIDORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err @@ -1133,6 +1136,9 @@ func DefaultReadMultiaccountTypeWithID(ctx context.Context, in *MultiaccountType if err != nil { return nil, err } + if ormParams.Id == 0 { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := MultiaccountTypeWithIDORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err @@ -1411,6 +1417,9 @@ func DefaultReadPrimaryUUIDType(ctx context.Context, in *PrimaryUUIDType, db *go if err != nil { return nil, err } + if ormParams.Id == nil || *ormParams.Id == go_uuid1.Nil { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := PrimaryUUIDTypeORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err @@ -1638,6 +1647,9 @@ func DefaultReadPrimaryStringType(ctx context.Context, in *PrimaryStringType, db if err != nil { return nil, err } + if ormParams.Id == "" { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := PrimaryStringTypeORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err diff --git a/example/user/user.pb.gorm.go b/example/user/user.pb.gorm.go index 4efa6bdc..1a1c5676 100644 --- a/example/user/user.pb.gorm.go +++ b/example/user/user.pb.gorm.go @@ -874,6 +874,9 @@ func DefaultReadUser(ctx context.Context, in *User, db *gorm1.DB) (*User, error) if err != nil { return nil, err } + if ormParams.Id == "" { + return nil, errors.New("Read requires a non-zero primary key") + } db = db.Preload("Tasks", func(db *gorm1.DB) *gorm1.DB { return db.Order("priority") }) @@ -1286,6 +1289,9 @@ func DefaultReadEmail(ctx context.Context, in *Email, db *gorm1.DB) (*Email, err if err != nil { return nil, err } + if ormParams.Id == "" { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := EmailORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err @@ -1506,6 +1512,9 @@ func DefaultReadAddress(ctx context.Context, in *Address, db *gorm1.DB) (*Addres if err != nil { return nil, err } + if ormParams.Id == 0 { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := AddressORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err @@ -1730,6 +1739,9 @@ func DefaultReadLanguage(ctx context.Context, in *Language, db *gorm1.DB) (*Lang if err != nil { return nil, err } + if ormParams.Id == 0 { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := LanguageORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err @@ -1946,6 +1958,9 @@ func DefaultReadCreditCard(ctx context.Context, in *CreditCard, db *gorm1.DB) (* if err != nil { return nil, err } + if ormParams.Id == 0 { + return nil, errors.New("Read requires a non-zero primary key") + } ormResponse := CreditCardORM{} if err = db.Where(&ormParams).First(&ormResponse).Error; err != nil { return nil, err diff --git a/plugin/handlergen.go b/plugin/handlergen.go index 38bd498b..6620e729 100644 --- a/plugin/handlergen.go +++ b/plugin/handlergen.go @@ -104,6 +104,14 @@ func (p *OrmPlugin) generateReadHandler(message *generator.Descriptor) { p.P(`if err != nil {`) p.P(`return nil, err`) p.P(`}`) + k, f := p.findPrimaryKey(ormable) + if strings.Contains(f.Type, "*") { + p.P(`if ormParams.`, k, ` == nil || *ormParams.`, k, ` == `, p.guessZeroValue(f.Type), ` {`) + } else { + p.P(`if ormParams.`, k, ` == `, p.guessZeroValue(f.Type), ` {`) + } + p.P(`return nil, errors.New("Read requires a non-zero primary key")`) + p.P(`}`) p.sortOrderedHasMany(message) p.P(`ormResponse := `, ormable.Name, `{}`)