Skip to content

Commit

Permalink
feat(get): return resource even if soft-deleted
Browse files Browse the repository at this point in the history
From [AIP-164](https://google.aip.dev/164) documentation:
```
Get ([AIP-131](https://google.aip.dev/131)) requests for soft-deleted resources should return the resource (rather than a NOT_FOUND error).
```
  • Loading branch information
John Blåberg Kristoffersson authored and blaberg committed Aug 1, 2024
1 parent ab29361 commit f9359ce
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ Sample skips:

### Get

| Name | Description | Only if |
| -------------- | ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- |
| missing name | Method should fail with InvalidArgument if no name is provided. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| invalid name | Method should fail with InvalidArgument if the provided name is not valid. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| exists | Resource should be returned without errors if it exists. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| not found | Method should fail with NotFound if the resource does not exist. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| only wildcards | Method should fail with InvalidArgument if the provided name only contains wildcards ('-') | Generated only if all are true: <ul><li>has Get method</li></ul> |
| Name | Description | Only if |
| -------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| missing name | Method should fail with InvalidArgument if no name is provided. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| invalid name | Method should fail with InvalidArgument if the provided name is not valid. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| exists | Resource should be returned without errors if it exists. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| not found | Method should fail with NotFound if the resource does not exist. | Generated only if all are true: <ul><li>has Get method</li></ul> |
| only wildcards | Method should fail with InvalidArgument if the provided name only contains wildcards ('-') | Generated only if all are true: <ul><li>has Get method</li></ul> |
| soft-deleted | A soft-deleted resource should be returned without errors. | Generated only if all are true: <ul><li>has Get method</li><li>has Delete method</li><li>has field 'delete_time'</li></ul> |

### BatchGet

Expand Down
48 changes: 48 additions & 0 deletions internal/aiptest/get/deleted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package get

import (
"github.com/einride/protoc-gen-go-aip-test/internal/ident"
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
"github.com/einride/protoc-gen-go-aip-test/internal/util"
"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/protobuf/compiler/protogen"
)

//nolint:gochecknoglobals
var softDeleted = suite.Test{
Name: "soft-deleted",
Doc: []string{
"A soft-deleted resource should be returned without errors.",
},
OnlyIf: suite.OnlyIfs(
onlyif.HasMethod(aipreflect.MethodTypeGet),
onlyif.HasMethod(aipreflect.MethodTypeDelete),
onlyif.HasField("delete_time"),
),
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
getMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeGet)
deleteMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeDelete)

if util.HasParent(scope.Resource) {
f.P("parent := ", ident.FixtureNextParent, "(t, false)")
f.P("created := fx.create(t, parent)")
} else {
f.P("created := fx.create(t)")
}
util.MethodDelete{
Resource: scope.Resource,
Method: deleteMethod,
ResourceVar: "created",
}.Generate(f, "deleted", "err", ":=")
f.P(ident.AssertNilError, "(t, err)")
util.MethodGet{
Resource: scope.Resource,
Method: getMethod,
Name: "created.Name",
}.Generate(f, "msg", "err", ":=")
f.P(ident.AssertNilError, "(t, err)")
f.P(ident.AssertDeepEqual, "(t, msg, deleted, ", ident.ProtocmpTransform, "())")
return nil
},
}
1 change: 1 addition & 0 deletions internal/aiptest/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ var Suite = suite.Suite{
exists,
notFound,
wildcardName,
softDeleted,
},
}
32 changes: 32 additions & 0 deletions proto/gen/einride/example/freight/v1/freight_service_aiptest.pb.go

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

0 comments on commit f9359ce

Please sign in to comment.