Skip to content

Commit

Permalink
Add error if a read is attempted when no primary key (id) is set (#110)
Browse files Browse the repository at this point in the history
* Add error if a read is attempted when no primary key (id) is set

* Regen protos with upstream changes
  • Loading branch information
Calebjh authored Aug 7, 2018
1 parent f205804 commit f2e770d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions example/feature_demo/demo_multi_file.pb.gorm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions example/feature_demo/demo_service.pb.gorm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions example/feature_demo/demo_types.pb.gorm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions example/user/user.pb.gorm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions plugin/handlergen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, `{}`)
Expand Down

0 comments on commit f2e770d

Please sign in to comment.