...

Source file src/cloud.google.com/go/storage/option.go

Documentation: cloud.google.com/go/storage

     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  
    15  package storage
    16  
    17  import (
    18  	"google.golang.org/api/option"
    19  	"google.golang.org/api/option/internaloption"
    20  )
    21  
    22  // storageConfig contains the Storage client option configuration that can be
    23  // set through storageClientOptions.
    24  type storageConfig struct {
    25  	useJSONforReads bool
    26  	readAPIWasSet   bool
    27  }
    28  
    29  // newStorageConfig generates a new storageConfig with all the given
    30  // storageClientOptions applied.
    31  func newStorageConfig(opts ...option.ClientOption) storageConfig {
    32  	var conf storageConfig
    33  	for _, opt := range opts {
    34  		if storageOpt, ok := opt.(storageClientOption); ok {
    35  			storageOpt.ApplyStorageOpt(&conf)
    36  		}
    37  	}
    38  	return conf
    39  }
    40  
    41  // A storageClientOption is an option for a Google Storage client.
    42  type storageClientOption interface {
    43  	option.ClientOption
    44  	ApplyStorageOpt(*storageConfig)
    45  }
    46  
    47  // WithJSONReads is an option that may be passed to a Storage Client on creation.
    48  // It sets the client to use the JSON API for object reads. Currently, the
    49  // default API used for reads is XML.
    50  // Setting this option is required to use the GenerationNotMatch condition.
    51  //
    52  // Note that when this option is set, reads will return a zero date for
    53  // [ReaderObjectAttrs].LastModified and may return a different value for
    54  // [ReaderObjectAttrs].CacheControl.
    55  func WithJSONReads() option.ClientOption {
    56  	return &withReadAPI{useJSON: true}
    57  }
    58  
    59  // WithXMLReads is an option that may be passed to a Storage Client on creation.
    60  // It sets the client to use the XML API for object reads.
    61  //
    62  // This is the current default.
    63  func WithXMLReads() option.ClientOption {
    64  	return &withReadAPI{useJSON: false}
    65  }
    66  
    67  type withReadAPI struct {
    68  	internaloption.EmbeddableAdapter
    69  	useJSON bool
    70  }
    71  
    72  func (w *withReadAPI) ApplyStorageOpt(c *storageConfig) {
    73  	c.useJSONforReads = w.useJSON
    74  	c.readAPIWasSet = true
    75  }
    76  

View as plain text