-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: AIP-124 – Resource association #18
Open
lukesneeringer
wants to merge
1
commit into
main
Choose a base branch
from
aip-124
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Resource association | ||
|
||
APIs sometimes have resource hierarchies that can not be cleanly expressed in | ||
the usual tree structure. For example, a resource may have a many-to-one | ||
relationship with two other resource types instead of just one. Alternatively, | ||
a resource may have a many-to-many relationship with another resource type. | ||
|
||
## Guidance | ||
|
||
A resource **must** have at most one canonical parent, and `GET` operations to | ||
read collections **must not** require two distinct "parents" to work. | ||
|
||
### Multiple many-to-one associations | ||
|
||
If a resource has a many-to-one relationship with multiple resource types, it | ||
**must** choose at most one of them to be the canonical parent. The resource | ||
**may** be associated with other resources through other fields on the | ||
resource. | ||
|
||
{% tab proto -%} | ||
|
||
{% sample 'many_to_one.proto', 'message Book' %} | ||
|
||
{% endtabs %} | ||
|
||
When listing resources (see AIP-132) with multiple associations in this way, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can AIP references, here and elsewhere, be linkified? |
||
the RPC **must** treat the `string parent` field, and **must not** add | ||
additional required arguments; additionally, the operation **should** include a | ||
`string filter` field (see AIP-160) that allows users to filter by other | ||
resource associations. | ||
|
||
### Many-to-many associations | ||
|
||
Many-to-many associations are less common in APIs than they are in relational | ||
databases, in part because they are more difficult to model and present over | ||
network interfaces. | ||
|
||
An API **may** contain many-to-many relationships, and **should** use a | ||
repeated field containing a list of resource names, following the principles | ||
described for repeated fields in AIP-144. | ||
|
||
{% tab proto -%} | ||
|
||
{% sample 'many_to_many.proto', 'message Book' %} | ||
|
||
{% endtabs %} | ||
|
||
**Note:** See AIP-144 for more information on repeated fields, including how to | ||
handle common issues such as atomic changes. | ||
|
||
If the use of a repeated field is too restrictive, or if more metadata is | ||
required along with the association, an API **may** model a many-to-many | ||
relationship using a sub-resource with two one-to-many associations. | ||
|
||
{% tab proto -%} | ||
|
||
{% sample 'subresource.proto', 'message BookAuthor' %} | ||
|
||
{% endtabs %} | ||
|
||
**Note:** Using subresources to model an association between resources is only | ||
recommended if additional metadata is required in the relationship, or if the | ||
restrictions around the use of a repeated field preclude the use of that | ||
approach. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
id: 124 | ||
state: approved | ||
created: 2020-03-20 | ||
placement: | ||
category: resource-design | ||
order: 40 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
syntax = "proto3"; | ||
|
||
import "google/api/resource.proto"; | ||
|
||
// A representation of an individual person who writes a book. | ||
message Author { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Author" | ||
pattern: "publishers/{publisher}/authors/{author}" | ||
}; | ||
|
||
// The name of the author. | ||
// Format: publishers/{publisher}/authors/{author} | ||
string name = 1; | ||
|
||
// The author's given name (in Western culture: "first name"). | ||
string given_name = 2; | ||
|
||
// The author's family name (in Western culture: "last name"). | ||
string family_name = 3; | ||
} | ||
|
||
// A representation of a single book. | ||
message Book { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Book" | ||
pattern: "publishers/{publisher}/books/{book}" | ||
}; | ||
|
||
// The name of the book. | ||
// Format: publishers/{publisher}/books/{book} | ||
string name = 1; | ||
|
||
// The resource names for the authors of the book. | ||
// Format: publishers/{publisher}/authors/{author} | ||
repeated string authors = 2 [(google.api.resource_reference) = { | ||
type: "library.googleapis.com/Author" | ||
}]; | ||
|
||
// The rating assigned to the book. | ||
float rating = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
syntax = "proto3"; | ||
|
||
import "google/api/resource.proto"; | ||
|
||
// A representation of an individual person who writes a book. | ||
message Author { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Author" | ||
pattern: "publishers/{publisher}/authors/{author}" | ||
}; | ||
|
||
// The name of the author. | ||
// Format: publishers/{publisher}/authors/{author} | ||
string name = 1; | ||
|
||
// The author's given name (in Western culture: "first name"). | ||
string given_name = 2; | ||
|
||
// The author's family name (in Western culture: "last name"). | ||
string family_name = 3; | ||
} | ||
|
||
// A representation of a single book. | ||
message Book { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Book" | ||
pattern: "publishers/{publisher}/books/{book}" | ||
}; | ||
|
||
// The name of the book. | ||
// Format: publishers/{publisher}/books/{book} | ||
string name = 1; | ||
|
||
// The resource name for the author of the book. | ||
// Format: publishers/{publisher}/authors/{author} | ||
string author = 2 [(google.api.resource_reference) = { | ||
type: "library.googleapis.com/Author" | ||
}]; | ||
|
||
// The rating assigned to the book. | ||
float rating = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
syntax = "proto3"; | ||
|
||
import "google/api/resource.proto"; | ||
|
||
// A representation of an individual person who writes a book. | ||
message Author { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Author" | ||
pattern: "publishers/{publisher}/authors/{author}" | ||
}; | ||
|
||
// The name of the author. | ||
// Format: publishers/{publisher}/authors/{author} | ||
string name = 1; | ||
|
||
// The author's given name (in Western culture: "first name"). | ||
string given_name = 2; | ||
|
||
// The author's family name (in Western culture: "last name"). | ||
string family_name = 3; | ||
} | ||
|
||
// A representation of a single book. | ||
message Book { | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/Book" | ||
pattern: "publishers/{publisher}/books/{book}" | ||
}; | ||
|
||
// The name of the book. | ||
// Format: publishers/{publisher}/books/{book} | ||
string name = 1; | ||
|
||
// The rating assigned to the book. | ||
float rating = 2; | ||
} | ||
|
||
// A representation of the link between a particular book and a | ||
// particular author. | ||
message BookAuthor { | ||
// The resource pattern for BookAuthor indicates that Book is the | ||
// canonical parent. | ||
option (google.api.resource) = { | ||
type: "library.googleapis.com/BookAuthor" | ||
pattern: "publishers/{publisher}/books/{book}/authors/{book_author}" | ||
}; | ||
|
||
// The resource name for the book-author association. | ||
// Format: publishers/{publisher}/books/{book}/authors/{book_author} | ||
string name = 1; | ||
|
||
// The resource name for the author. | ||
// Format: publishers/{publisher}/authors/{author} | ||
string author = 2 [(google.api.resource_reference) = { | ||
type: "library.googleapis.com/Author" | ||
}]; | ||
|
||
// Other fields... | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.