Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
94 lines (85 loc) · 2.29 KB
/
Copy pathexample_test.go
File metadata and controls
94 lines (85 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package imdb_test
import (
"context"
"errors"
"fmt"
imdb "github.com/odwrtw/libimdb"
)
func ExampleCatalog_Candidates() {
catalog, err := imdb.Open(context.Background(), "imdb.sqlite")
if err != nil {
return
}
defer func() { _ = catalog.Close() }()
result, err := catalog.Candidates(context.Background(), imdb.Query{
Title: "matrix", Kind: imdb.Movie,
}, imdb.SearchOptions{Limit: 20})
if err != nil {
return
}
for _, match := range result.Matches {
fmt.Println(match.Title.ID, match.Title.PrimaryTitle, match.Method)
}
// These are suggestions, never proof of a unique identity.
fmt.Println("limit clipped:", result.LimitTruncated, "budget exhausted:", result.BudgetExhausted)
}
func ExampleCatalog_Resolve() {
// Populate this catalog with imdb.Update before opening it.
catalog, err := imdb.Open(context.Background(), "imdb.sqlite")
if err != nil {
return
}
defer func() { _ = catalog.Close() }()
match, err := catalog.Resolve(context.Background(), imdb.Query{
Title: "The Matrix", Kind: imdb.Movie, StartYear: 1999,
})
var ambiguity *imdb.AmbiguousError
switch {
case errors.As(err, &ambiguity):
for _, title := range ambiguity.Titles {
fmt.Println(title.ID, title.PrimaryTitle)
}
fmt.Println("more conflicts:", ambiguity.Truncated)
case errors.Is(err, imdb.ErrNotFound):
fmt.Println("no safe identity match")
case err != nil:
fmt.Println("catalog failure:", err)
default:
fmt.Println(match.Title.ID, match.Generation)
}
}
func ExampleCatalog_View() {
ctx := context.Background()
catalog, err := imdb.Open(ctx, "imdb.sqlite")
if err != nil {
return
}
defer func() { _ = catalog.Close() }()
err = catalog.View(ctx, func(view *imdb.View) error {
resolved, err := view.Resolve(ctx, imdb.Query{Title: "The Matrix"})
if err != nil {
return err
}
names, err := view.Names(ctx, resolved.Title.ID)
if err != nil {
return err
}
// Both results come from one generation, even across a refresh.
fmt.Println(resolved.Generation, names)
return nil
})
if err != nil {
fmt.Println(err)
}
}
func ExampleValidationError() {
_, err := imdb.Open(context.Background(), "")
var invalid *imdb.ValidationError
if errors.As(err, &invalid) {
fmt.Println(invalid.Field)
}
fmt.Println(errors.Is(err, imdb.ErrInvalidInput))
// Output:
// path
// true
}