Skip to content

Commit f9359ce

Browse files
John Blåberg Kristofferssonblaberg
authored andcommitted
feat(get): return resource even if soft-deleted
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). ```
1 parent ab29361 commit f9359ce

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ Sample skips:
126126

127127
### Get
128128

129-
| Name | Description | Only if |
130-
| -------------- | ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- |
131-
| 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> |
132-
| 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> |
133-
| exists | Resource should be returned without errors if it exists. | Generated only if all are true: <ul><li>has Get method</li></ul> |
134-
| 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> |
135-
| 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> |
129+
| Name | Description | Only if |
130+
| -------------- | ------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
131+
| 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> |
132+
| 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> |
133+
| exists | Resource should be returned without errors if it exists. | Generated only if all are true: <ul><li>has Get method</li></ul> |
134+
| 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> |
135+
| 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> |
136+
| 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> |
136137

137138
### BatchGet
138139

internal/aiptest/get/deleted.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package get
2+
3+
import (
4+
"github.com/einride/protoc-gen-go-aip-test/internal/ident"
5+
"github.com/einride/protoc-gen-go-aip-test/internal/onlyif"
6+
"github.com/einride/protoc-gen-go-aip-test/internal/suite"
7+
"github.com/einride/protoc-gen-go-aip-test/internal/util"
8+
"go.einride.tech/aip/reflect/aipreflect"
9+
"google.golang.org/protobuf/compiler/protogen"
10+
)
11+
12+
//nolint:gochecknoglobals
13+
var softDeleted = suite.Test{
14+
Name: "soft-deleted",
15+
Doc: []string{
16+
"A soft-deleted resource should be returned without errors.",
17+
},
18+
OnlyIf: suite.OnlyIfs(
19+
onlyif.HasMethod(aipreflect.MethodTypeGet),
20+
onlyif.HasMethod(aipreflect.MethodTypeDelete),
21+
onlyif.HasField("delete_time"),
22+
),
23+
Generate: func(f *protogen.GeneratedFile, scope suite.Scope) error {
24+
getMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeGet)
25+
deleteMethod, _ := util.StandardMethod(scope.Service, scope.Resource, aipreflect.MethodTypeDelete)
26+
27+
if util.HasParent(scope.Resource) {
28+
f.P("parent := ", ident.FixtureNextParent, "(t, false)")
29+
f.P("created := fx.create(t, parent)")
30+
} else {
31+
f.P("created := fx.create(t)")
32+
}
33+
util.MethodDelete{
34+
Resource: scope.Resource,
35+
Method: deleteMethod,
36+
ResourceVar: "created",
37+
}.Generate(f, "deleted", "err", ":=")
38+
f.P(ident.AssertNilError, "(t, err)")
39+
util.MethodGet{
40+
Resource: scope.Resource,
41+
Method: getMethod,
42+
Name: "created.Name",
43+
}.Generate(f, "msg", "err", ":=")
44+
f.P(ident.AssertNilError, "(t, err)")
45+
f.P(ident.AssertDeepEqual, "(t, msg, deleted, ", ident.ProtocmpTransform, "())")
46+
return nil
47+
},
48+
}

internal/aiptest/get/get.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ var Suite = suite.Suite{
1515
exists,
1616
notFound,
1717
wildcardName,
18+
softDeleted,
1819
},
1920
}

proto/gen/einride/example/freight/v1/freight_service_aiptest.pb.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)