spanner: Allow decoding Spanner values to custom types.

This change adds support for decoding result set values from Spanner
into custom types that point back to supported types. This allows
decoding a result set into for example a struct like this:

type blobField []byte
type dbProductInfo struct {
    ProductID  int64     `spanner:"ProductID"`
    UpdateDate time.Time `spanner:"UpdateDate"`
    Data       blobField `spanner:"Data"`
}

Decoding Spanner column values to one of the known types
(string, []byte, int64, float64, time.Timestamp, civil.Date) and
their known Null<type> versions is done using a type switch
statement. Decoding to a custom type is done with reflection. The
reason for not using reflection for all types, including the known
base types, is the performance difference. Benchmark results executed
locally showing the difference between the two are shown below based
on decoding a single string value to a standard string or a custom
string type.

goos: linux
goarch: amd64
pkg: cloud.google.com/go/spanner
BenchmarkDecodeString-8         200000000   8.21 ns/op
BenchmarkDecodeCustomString-8   10000000    135  ns/op

Updates #1610.
Fixes #853.

Change-Id: Iad689b3704acadb83809375c762a7e0b564a7b2f
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/48090
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Tyler Bui-Palsulich <tbp@google.com>
Reviewed-by: Hengfeng Li <hengfeng@google.com>
4 files changed
tree: 10c25de8d479ea4c2d7da3a0264d35465fd34230
  1. asset/
  2. automl/
  3. bigquery/
  4. bigtable/
  5. civil/
  6. cloudbuild/
  7. cloudtasks/
  8. cmd/
  9. compute/
  10. container/
  11. containeranalysis/
  12. dataproc/
  13. datastore/
  14. debugger/
  15. dialogflow/
  16. dlp/
  17. errorreporting/
  18. expr/
  19. firestore/
  20. functions/
  21. grafeas/
  22. httpreplay/
  23. iam/
  24. internal/
  25. iot/
  26. irm/
  27. kms/
  28. language/
  29. logging/
  30. longrunning/
  31. monitoring/
  32. oslogin/
  33. phishingprotection/
  34. profiler/
  35. pubsub/
  36. recaptchaenterprise/
  37. recommender/
  38. redis/
  39. rpcreplay/
  40. scheduler/
  41. securitycenter/
  42. spanner/
  43. speech/
  44. storage/
  45. talent/
  46. texttospeech/
  47. trace/
  48. translate/
  49. videointelligence/
  50. vision/
  51. webrisk/
  52. CHANGES.md
  53. CODE_OF_CONDUCT.md
  54. CONTRIBUTING.md
  55. doc.go
  56. examples_test.go
  57. go.mod
  58. go.sum
  59. issue_template.md
  60. LICENSE
  61. license_test.go
  62. README.md
  63. RELEASING.md
  64. tools.go
README.md

Google Cloud Client Libraries for Go

GoDoc

Go packages for Google Cloud Platform services.

import "cloud.google.com/go"

To install the packages on your system, do not clone the repo. Instead:

  1. Change to your project directory:

    cd /my/cloud/project
    
  2. Get the package you want to use. Some products have their own module, so it's best to go get the package(s) you want to use:

    $ go get cloud.google.com/go/firestore # Replace with the package you want to use.
    

NOTE: Some of these packages are under development, and may occasionally make backwards-incompatible changes.

NOTE: Github repo is a mirror of https://code.googlesource.com/gocloud.

Supported APIs

Google APIStatusPackage
Assetalphacloud.google.com/go/asset/v1beta
Automlstablecloud.google.com/go/automl/apiv1
BigQuerystablecloud.google.com/go/bigquery
Bigtablestablecloud.google.com/go/bigtable
Cloudbuildstablecloud.google.com/go/cloudbuild/apiv1
Cloudtasksstablecloud.google.com/go/cloudtasks/apiv2
Containerstablecloud.google.com/go/container/apiv1
ContainerAnalysisbetacloud.google.com/go/containeranalysis/apiv1
Dataprocstablecloud.google.com/go/dataproc/apiv1
Datastorestablecloud.google.com/go/datastore
Debuggerstablecloud.google.com/go/debugger/apiv2
Dialogflowstablecloud.google.com/go/dialogflow/apiv2
Data Loss Preventionstablecloud.google.com/go/dlp/apiv2
ErrorReportingalphacloud.google.com/go/errorreporting
Firestorestablecloud.google.com/go/firestore
IAMstablecloud.google.com/go/iam
IoTstablecloud.google.com/go/iot/apiv1
IRMalphacloud.google.com/go/irm/apiv1alpha2
KMSstablecloud.google.com/go/kms
Natural Languagestablecloud.google.com/go/language/apiv1
Loggingstablecloud.google.com/go/logging
Memorystorealphacloud.google.com/go/redis/apiv1
Monitoringalphacloud.google.com/go/monitoring/apiv3
OS Loginalphacloud.google.com/go/oslogin/apiv1
Pub/Substablecloud.google.com/go/pubsub
Phishing Protectionalphacloud.google.com/go/phishingprotection/apiv1beta1
reCAPTCHA Enterprisealphacloud.google.com/go/recaptchaenterprise/apiv1beta1
Recommenderbetacloud.google.com/go/recommender/apiv1beta1
Schedulerstablecloud.google.com/go/scheduler/apiv1
Securitycenteralphacloud.google.com/go/securitycenter/apiv1
Spannerstablecloud.google.com/go/spanner
Speechstablecloud.google.com/go/speech/apiv1
Storagestablecloud.google.com/go/storage
Talentalphacloud.google.com/go/talent/apiv4beta1
Text To Speechalphacloud.google.com/go/texttospeech/apiv1
Tracealphacloud.google.com/go/trace/apiv2
Translatestablecloud.google.com/go/translate
Video Intelligencealphacloud.google.com/go/videointelligence/apiv1beta1
Visionstablecloud.google.com/go/vision/apiv1
Webriskalphacloud.google.com/go/webrisk/apiv1beta1

Alpha status: the API is still being actively developed. As a result, it might change in backward-incompatible ways and is not recommended for production use.

Beta status: the API is largely complete, but still has outstanding features and bugs to be addressed. There may be minor backwards-incompatible changes where necessary.

Stable status: the API is mature and ready for production use. We will continue addressing bugs and feature requests.

Documentation and examples are available at godoc.org/cloud.google.com/go

Go Versions Supported

We support the two most recent major versions of Go. If Google App Engine uses an older version, we support that as well.

Authorization

By default, each API will use Google Application Default Credentials for authorization credentials used in calling the API endpoints. This will allow your application to run in many environments without requiring explicit configuration.

client, err := storage.NewClient(ctx)

To authorize using a JSON key file, pass option.WithCredentialsFile to the NewClient function of the desired package. For example:

client, err := storage.NewClient(ctx, option.WithCredentialsFile("path/to/keyfile.json"))

You can exert more control over authorization by using the golang.org/x/oauth2 package to create an oauth2.TokenSource. Then pass option.WithTokenSource to the NewClient function: snip:# (auth-ts)

tokenSource := ...
client, err := storage.NewClient(ctx, option.WithTokenSource(tokenSource))

Contributing

Contributions are welcome. Please, see the CONTRIBUTING document for details. We‘re using Gerrit for our code reviews. Please don’t open pull requests against this repo, new pull requests will be automatically closed.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Contributor Code of Conduct for more information.