github.com/thiagoyeds/go-cloud@v0.26.0/docstore/gcpfirestore/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 gcpfirestore_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"gocloud.dev/docstore"
    22  	"gocloud.dev/docstore/gcpfirestore"
    23  	"gocloud.dev/gcp"
    24  )
    25  
    26  func ExampleOpenCollection() {
    27  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    28  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    29  	ctx := context.Background()
    30  
    31  	creds, err := gcp.DefaultCredentials(ctx)
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  	client, _, err := gcpfirestore.Dial(ctx, creds.TokenSource)
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	resourceID := gcpfirestore.CollectionResourceID("my-project", "my-collection")
    40  	coll, err := gcpfirestore.OpenCollection(client, resourceID, "userID", nil)
    41  	if err != nil {
    42  		log.Fatal(err)
    43  	}
    44  	defer coll.Close()
    45  }
    46  
    47  func ExampleOpenCollectionWithNameFunc() {
    48  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    49  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    50  	ctx := context.Background()
    51  	type HighScore struct {
    52  		Game   string
    53  		Player string
    54  	}
    55  
    56  	creds, err := gcp.DefaultCredentials(ctx)
    57  	if err != nil {
    58  		log.Fatal(err)
    59  	}
    60  	client, _, err := gcpfirestore.Dial(ctx, creds.TokenSource)
    61  	if err != nil {
    62  		log.Fatal(err)
    63  	}
    64  
    65  	// The name of a document is constructed from the Game and Player fields.
    66  	nameFromDocument := func(doc docstore.Document) string {
    67  		hs := doc.(*HighScore)
    68  		return hs.Game + "|" + hs.Player
    69  	}
    70  
    71  	resourceID := gcpfirestore.CollectionResourceID("my-project", "my-collection")
    72  	coll, err := gcpfirestore.OpenCollectionWithNameFunc(client, resourceID, nameFromDocument, nil)
    73  	if err != nil {
    74  		log.Fatal(err)
    75  	}
    76  	defer coll.Close()
    77  }
    78  
    79  func Example_openCollectionFromURL() {
    80  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    81  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/docstore/gcpfirestore"
    82  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    83  	ctx := context.Background()
    84  
    85  	// docstore.OpenCollection creates a *docstore.Collection from a URL.
    86  	const url = "firestore://projects/my-project/databases/(default)/documents/my-collection?name_field=userID"
    87  	coll, err := docstore.OpenCollection(ctx, url)
    88  	if err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	defer coll.Close()
    92  }