github.com/SaurabhDubey-Groww/go-cloud@v0.0.0-20221124105541-b26c29285fd8/docstore/memdocstore/example_test.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     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  //     https://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 memdocstore_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"gocloud.dev/docstore"
    22  	"gocloud.dev/docstore/memdocstore"
    23  )
    24  
    25  func ExampleOpenCollection() {
    26  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    27  
    28  	coll, err := memdocstore.OpenCollection("keyField", nil)
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	defer coll.Close()
    33  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    34  	// Output:
    35  }
    36  
    37  func ExampleOpenCollectionWithKeyFunc() {
    38  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    39  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    40  	type HighScore struct {
    41  		Game   string
    42  		Player string
    43  	}
    44  
    45  	// The name of a document is constructed from the Game and Player fields.
    46  	nameFromDocument := func(doc docstore.Document) interface{} {
    47  		hs := doc.(*HighScore)
    48  		return hs.Game + "|" + hs.Player
    49  	}
    50  
    51  	coll, err := memdocstore.OpenCollectionWithKeyFunc(nameFromDocument, nil)
    52  	if err != nil {
    53  		log.Fatal(err)
    54  	}
    55  	defer coll.Close()
    56  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    57  	// Output:
    58  }
    59  
    60  func Example_openCollectionFromURL() {
    61  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    62  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/docstore/memdocstore"
    63  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    64  	ctx := context.Background()
    65  
    66  	// docstore.OpenCollection creates a *docstore.Collection from a URL.
    67  	coll, err := docstore.OpenCollection(ctx, "mem://collection/keyField")
    68  	if err != nil {
    69  		log.Fatal(err)
    70  	}
    71  	defer coll.Close()
    72  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    73  	// Output:
    74  }