Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/einride/aip-go
Go SDK for implementing resource-oriented gRPC APIs.
https://github.com/einride/aip-go
aip api-management go golang grpc protobufs protocol-buffers
Last synced: 2 months ago
JSON representation
Go SDK for implementing resource-oriented gRPC APIs.
- Host: GitHub
- URL: https://github.com/einride/aip-go
- Owner: einride
- License: mit
- Created: 2020-12-22T19:00:27.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-04-12T08:45:55.000Z (10 months ago)
- Last Synced: 2024-04-13T02:14:19.267Z (9 months ago)
- Topics: aip, api-management, go, golang, grpc, protobufs, protocol-buffers
- Language: Go
- Homepage: https://aip.dev
- Size: 446 KB
- Stars: 148
- Watchers: 17
- Forks: 14
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
[![Go Reference](https://pkg.go.dev/badge/go.einride.tech/aip.svg)](https://pkg.go.dev/go.einride.tech/aip)
# AIP Go
Go SDK for implementing [Google API Improvement Proposals](https://aip.dev/)
(AIP).## Generate AIP support code from proto
```bash
go install go.einride.tech/aip/cmd/protoc-gen-go-aip
```Add to `buf.gen.yaml`:
```yaml
version: v2
plugins:
- local: protoc-gen-go-aip
out: gen
opt:
- paths=source_relative
```Run `buf build` to generate e.g. `your_service_aip.go`.
## Library usage examples
```bash
go get -u go.einride.tech/aip
```### [AIP-132](https://google.aip.dev/132) (Standard method: List)
- Use [`pagination.PageToken`](./pagination/pagetoken.go) to implement
offset-based pagination.```go
package examplelibraryimport (
"context""go.einride.tech/aip/pagination"
"google.golang.org/genproto/googleapis/example/library/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)func (s *Server) ListShelves(
ctx context.Context,
request *library.ListShelvesRequest,
) (*library.ListShelvesResponse, error) {
// Handle request constraints.
const (
§ maxPageSize = 1000
defaultPageSize = 100
)
switch {
case request.PageSize < 0:
return nil, status.Errorf(codes.InvalidArgument, "page size is negative")
case request.PageSize == 0:
request.PageSize = defaultPageSize
case request.PageSize > maxPageSize:
request.PageSize = maxPageSize
}
// Use pagination.PageToken for offset-based page tokens.
pageToken, err := pagination.ParsePageToken(request)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid page token")
}
// Query the storage.
result, err := s.Storage.ListShelves(ctx, &ListShelvesQuery{
Offset: pageToken.Offset,
PageSize: request.GetPageSize(),
})
if err != nil {
return nil, err
}
// Build the response.
response := &library.ListShelvesResponse{
Shelves: result.Shelves,
}
// Set the next page token.
if result.HasNextPage {
response.NextPageToken = pageToken.Next(request).String()
}
// Respond.
return response, nil
}
```