...

Source file src/github.com/go-kivik/kivik/v4/x/fsdb/get.go

Documentation: github.com/go-kivik/kivik/v4/x/fsdb

     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 fs
    14  
    15  import (
    16  	"bytes"
    17  	"context"
    18  	"encoding/json"
    19  	"errors"
    20  	"io"
    21  	"net/http"
    22  
    23  	"github.com/go-kivik/kivik/v4/driver"
    24  )
    25  
    26  // TODO:
    27  // - atts_since
    28  // - conflicts
    29  // - deleted_conflicts
    30  // - latest
    31  // - local_seq
    32  // - meta
    33  // - open_revs
    34  func (d *db) Get(_ context.Context, docID string, options driver.Options) (*driver.Document, error) {
    35  	opts := map[string]interface{}{}
    36  	options.Apply(opts)
    37  	if docID == "" {
    38  		return nil, statusError{status: http.StatusBadRequest, error: errors.New("no docid specified")}
    39  	}
    40  	docs, err := d.cdb.OpenDocIDOpenRevs(docID, options)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	docs[0].Options = opts
    45  	buf := &bytes.Buffer{}
    46  	if err := json.NewEncoder(buf).Encode(docs[0]); err != nil {
    47  		return nil, err
    48  	}
    49  	attsIter, err := docs[0].Revisions[0].AttachmentsIterator()
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return &driver.Document{
    54  		Rev:         docs[0].Revisions[0].Rev.String(),
    55  		Body:        io.NopCloser(buf),
    56  		Attachments: attsIter,
    57  	}, nil
    58  }
    59  

View as plain text