Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

entproto/cmd/protoc-gen-entgrpc - infra for gRPC field validation #55

Merged
merged 10 commits into from
Apr 4, 2021

Conversation

rotemtam
Copy link
Collaborator

@rotemtam rotemtam commented Apr 1, 2021

Adding infra for validation on the gRPC layer + UUID field support (normal field, edge id, id)

Fixes ent/ent#1402

@a8m
Copy link
Member

a8m commented Apr 1, 2021

Please use "squash and merge" when you land this and replace the - in the commit message with :.

Great work, and thanks for adding this 🙏

@rotemtam rotemtam force-pushed the feat/entgrpc-validation-infra branch from 9d8be96 to d654d65 Compare April 1, 2021 20:06
@rotemtam rotemtam changed the title entproto/cmd/protoc-gen-entgrpc - infra for gRPC field validation [WIP] entproto/cmd/protoc-gen-entgrpc - infra for gRPC field validation Apr 4, 2021
@rotemtam rotemtam requested a review from a8m April 4, 2021 10:45
Copy link
Member

@a8m a8m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Minor comments.

Comment on lines 88 to 100
checkIDFlag := "true"
if op == "create" {
checkIDFlag = "false"
}

if typeNeedsValidator(g.fieldMap) {
g.Tmpl(`if err := validate%(typeName)(%(reqVar), %(checkIDFlag)); err != nil {
return nil, %(statusErrf)(%(invalidArgument), "invalid argument: %s", err)
}`, g.withGlobals(tmplValues{
"reqVar": reqVar,
"checkIDFlag": checkIDFlag,
}))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
checkIDFlag := "true"
if op == "create" {
checkIDFlag = "false"
}
if typeNeedsValidator(g.fieldMap) {
g.Tmpl(`if err := validate%(typeName)(%(reqVar), %(checkIDFlag)); err != nil {
return nil, %(statusErrf)(%(invalidArgument), "invalid argument: %s", err)
}`, g.withGlobals(tmplValues{
"reqVar": reqVar,
"checkIDFlag": checkIDFlag,
}))
}
if typeNeedsValidator(g.fieldMap) {
g.Tmpl(`if err := validate%(typeName)(%(reqVar), %(checkIDFlag)); err != nil {
return nil, %(statusErrf)(%(invalidArgument), "invalid argument: %s", err)
}`, g.withGlobals(tmplValues{
"reqVar": reqVar,
"checkIDFlag": strconv.FormatBool(op == "create"),
}))
}

Comment on lines 33 to 36
if d.IsEdgeField {
return d.EdgeIDType() == field.TypeUUID
}
return d.EntField.IsUUID()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you use the EdgeIDType method only here? Maybe you can do instead:

Suggested change
if d.IsEdgeField {
return d.EdgeIDType() == field.TypeUUID
}
return d.EntField.IsUUID()
f := d.EntField
if d.IsEdgeField {
f = d.EntEdge.Type.ID
}
return f.IsUUID()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

res, err := svc.client.User.Create().
SetBanned(bool(user.GetBanned())).
SetCrmID(runtime.MustBytesToUUID(user.GetCrmId())).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you prefer to panic here instead of returning an error? Maybe it is worth mentioning this in the doc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is guaranteed not to panic b/c a program that reaches here passed the validation code. In one of the next versions I will remove this "setter chain" and treat each field separately for supporting per field nullability etc. In that PR it might be easy to make it nicer as you suggest

@rotemtam rotemtam merged commit b090bf5 into master Apr 4, 2021
@rotemtam rotemtam deleted the feat/entgrpc-validation-infra branch April 4, 2021 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants