option.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "google.golang.org/api/option"
  17. "google.golang.org/api/option/internaloption"
  18. )
  19. // storageConfig contains the Storage client option configuration that can be
  20. // set through storageClientOptions.
  21. type storageConfig struct {
  22. useJSONforReads bool
  23. readAPIWasSet bool
  24. }
  25. // newStorageConfig generates a new storageConfig with all the given
  26. // storageClientOptions applied.
  27. func newStorageConfig(opts ...option.ClientOption) storageConfig {
  28. var conf storageConfig
  29. for _, opt := range opts {
  30. if storageOpt, ok := opt.(storageClientOption); ok {
  31. storageOpt.ApplyStorageOpt(&conf)
  32. }
  33. }
  34. return conf
  35. }
  36. // A storageClientOption is an option for a Google Storage client.
  37. type storageClientOption interface {
  38. option.ClientOption
  39. ApplyStorageOpt(*storageConfig)
  40. }
  41. // WithJSONReads is an option that may be passed to a Storage Client on creation.
  42. // It sets the client to use the JSON API for object reads. Currently, the
  43. // default API used for reads is XML.
  44. // Setting this option is required to use the GenerationNotMatch condition.
  45. //
  46. // Note that when this option is set, reads will return a zero date for
  47. // [ReaderObjectAttrs].LastModified and may return a different value for
  48. // [ReaderObjectAttrs].CacheControl.
  49. func WithJSONReads() option.ClientOption {
  50. return &withReadAPI{useJSON: true}
  51. }
  52. // WithXMLReads is an option that may be passed to a Storage Client on creation.
  53. // It sets the client to use the XML API for object reads.
  54. //
  55. // This is the current default.
  56. func WithXMLReads() option.ClientOption {
  57. return &withReadAPI{useJSON: false}
  58. }
  59. type withReadAPI struct {
  60. internaloption.EmbeddableAdapter
  61. useJSON bool
  62. }
  63. func (w *withReadAPI) ApplyStorageOpt(c *storageConfig) {
  64. c.useJSONforReads = w.useJSON
  65. c.readAPIWasSet = true
  66. }