...

Text file src/go.einride.tech/aip/README.md

Documentation: go.einride.tech/aip

     1[![Go Reference](https://pkg.go.dev/badge/go.einride.tech/aip.svg)](https://pkg.go.dev/go.einride.tech/aip)
     2
     3# AIP Go
     4
     5Go SDK for implementing [Google API Improvement Proposals](https://aip.dev/)
     6(AIP).
     7
     8## Documentation
     9
    10See [https://aip.dev](https://aip.dev/) for the full AIP documentation.
    11
    12## Installing
    13
    14```bash
    15$ go get -u go.einride.tech/aip
    16```
    17
    18## Examples
    19
    20### [AIP-132](https://google.aip.dev/132) (Standard method: List)
    21
    22- Use [`pagination.PageToken`](./pagination/pagetoken.go) to implement
    23  offset-based pagination.
    24
    25  ```go
    26  package examplelibrary
    27
    28  import (
    29      "context"
    30
    31      "go.einride.tech/aip/pagination"
    32      "google.golang.org/genproto/googleapis/example/library/v1"
    33      "google.golang.org/grpc/codes"
    34      "google.golang.org/grpc/status"
    35  )
    36
    37  func (s *Server) ListShelves(
    38      ctx context.Context,
    39      request *library.ListShelvesRequest,
    40  ) (*library.ListShelvesResponse, error) {
    41      // Handle request constraints.
    42      const (
    43  ยง           maxPageSize     = 1000
    44          defaultPageSize = 100
    45      )
    46      switch {
    47      case request.PageSize < 0:
    48          return nil, status.Errorf(codes.InvalidArgument, "page size is negative")
    49      case request.PageSize == 0:
    50          request.PageSize = defaultPageSize
    51      case request.PageSize > maxPageSize:
    52          request.PageSize = maxPageSize
    53      }
    54      // Use pagination.PageToken for offset-based page tokens.
    55      pageToken, err := pagination.ParsePageToken(request)
    56      if err != nil {
    57          return nil, status.Errorf(codes.InvalidArgument, "invalid page token")
    58      }
    59      // Query the storage.
    60      result, err := s.Storage.ListShelves(ctx, &ListShelvesQuery{
    61          Offset:   pageToken.Offset,
    62          PageSize: request.GetPageSize(),
    63      })
    64      if err != nil {
    65          return nil, err
    66      }
    67      // Build the response.
    68      response := &library.ListShelvesResponse{
    69          Shelves: result.Shelves,
    70      }
    71      // Set the next page token.
    72      if result.HasNextPage {
    73          response.NextPageToken = pageToken.Next(request).String()
    74      }
    75      // Respond.
    76      return response, nil
    77  }
    78  ```

View as plain text