...

Source file src/github.com/go-kivik/kivik/v4/couchdb/options.go

Documentation: github.com/go-kivik/kivik/v4/couchdb

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package couchdb
    14  
    15  import (
    16  	"fmt"
    17  	"net/http"
    18  	"path"
    19  	"strings"
    20  
    21  	kivik "github.com/go-kivik/kivik/v4"
    22  	"github.com/go-kivik/kivik/v4/couchdb/chttp"
    23  )
    24  
    25  type optionHTTPClient struct {
    26  	*http.Client
    27  }
    28  
    29  func (c optionHTTPClient) Apply(target interface{}) {
    30  	if client, ok := target.(*http.Client); ok {
    31  		*client = *c.Client
    32  	}
    33  }
    34  
    35  func (optionHTTPClient) String() string { return "custom *http.Client" }
    36  
    37  // OptionHTTPClient may be passed as an option when creating a CouchDB client
    38  // to specify an custom [net/http.Client] to be used when making all API calls.
    39  // Only honored by [github.com/go-kivik/kivik/v4.New].
    40  func OptionHTTPClient(client *http.Client) kivik.Option {
    41  	return optionHTTPClient{Client: client}
    42  }
    43  
    44  // OptionNoRequestCompression instructs the CouchDB client not to use gzip
    45  // compression for request bodies sent to the server. Only honored by
    46  // [github.com/go-kivik/kivik/v4.New].
    47  func OptionNoRequestCompression() kivik.Option {
    48  	return chttp.OptionNoRequestCompression()
    49  }
    50  
    51  // OptionUserAgent may be passed as an option when creating a client object,
    52  // to append to the default User-Agent header sent on all requests.
    53  func OptionUserAgent(ua string) kivik.Option {
    54  	return chttp.OptionUserAgent(ua)
    55  }
    56  
    57  // OptionFullCommit is the option key used to set the `X-Couch-Full-Commit`
    58  // header in the request when set to true.
    59  func OptionFullCommit() kivik.Option {
    60  	return chttp.OptionFullCommit()
    61  }
    62  
    63  // OptionIfNoneMatch is an option key to set the `If-None-Match` header on
    64  // the request.
    65  func OptionIfNoneMatch(value string) kivik.Option {
    66  	return chttp.OptionIfNoneMatch(value)
    67  }
    68  
    69  type partitionedPath struct {
    70  	path string
    71  	part string
    72  }
    73  
    74  func partPath(path string) *partitionedPath {
    75  	return &partitionedPath{
    76  		path: path,
    77  	}
    78  }
    79  
    80  func (pp partitionedPath) String() string {
    81  	if pp.part == "" {
    82  		return pp.path
    83  	}
    84  	return path.Join("_partition", pp.part, pp.path)
    85  }
    86  
    87  type optionPartition string
    88  
    89  func (o optionPartition) Apply(target interface{}) {
    90  	if ppath, ok := target.(*partitionedPath); ok {
    91  		ppath.part = string(o)
    92  	}
    93  }
    94  
    95  func (o optionPartition) String() string {
    96  	return fmt.Sprintf("[partition:%s]", string(o))
    97  }
    98  
    99  // OptionPartition instructs supporting methods to limit the query to the
   100  // specified partition. Supported methods are: Query, AllDocs, Find, and
   101  // Explain. Only supported by CouchDB 3.0.0 and newer.
   102  //
   103  // See the [CouchDB documentation].
   104  //
   105  // [CouchDB documentation]: https://docs.couchdb.org/en/stable/api/partitioned-dbs.html
   106  func OptionPartition(partition string) kivik.Option {
   107  	return optionPartition(partition)
   108  }
   109  
   110  type optionNoMultipartPut struct{}
   111  
   112  func (optionNoMultipartPut) Apply(target interface{}) {
   113  	if putOpts, ok := target.(*putOptions); ok {
   114  		putOpts.NoMultipartPut = true
   115  	}
   116  }
   117  
   118  func (optionNoMultipartPut) String() string {
   119  	return "[NoMultipartPut]"
   120  }
   121  
   122  // OptionNoMultipartPut instructs [github.com/go-kivik/kivik/v4.DB.Put] not
   123  // to use CouchDB's multipart/related upload capabilities. This is only honored
   124  // by calls to [github.com/go-kivik/kivik/v4.DB.Put] that also include
   125  // attachments.
   126  func OptionNoMultipartPut() kivik.Option {
   127  	return optionNoMultipartPut{}
   128  }
   129  
   130  type optionNoMultipartGet struct{}
   131  
   132  func (optionNoMultipartGet) Apply(target interface{}) {
   133  	if getOpts, ok := target.(*getOptions); ok {
   134  		getOpts.noMultipartGet = true
   135  	}
   136  }
   137  
   138  func (optionNoMultipartGet) String() string {
   139  	return "[NoMultipartGet]"
   140  }
   141  
   142  // OptionNoMultipartGet instructs [github.com/go-kivik/kivik/v4.DB.Get] not
   143  // to use CouchDB's ability to download attachments with the
   144  // multipart/related media type. This is only honored by calls to
   145  // [github.com/go-kivik/kivik/v4.DB.Get] that request attachments.
   146  func OptionNoMultipartGet() kivik.Option {
   147  	return optionNoMultipartGet{}
   148  }
   149  
   150  type multiOptions []kivik.Option
   151  
   152  var _ kivik.Option = (multiOptions)(nil)
   153  
   154  func (o multiOptions) Apply(t interface{}) {
   155  	for _, opt := range o {
   156  		if opt != nil {
   157  			opt.Apply(t)
   158  		}
   159  	}
   160  }
   161  
   162  func (o multiOptions) String() string {
   163  	parts := make([]string, 0, len(o))
   164  	for _, opt := range o {
   165  		if part := fmt.Sprintf("%s", opt); part != "" {
   166  			parts = append(parts, part)
   167  		}
   168  	}
   169  	return strings.Join(parts, ",")
   170  }
   171  

View as plain text