-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
50 lines (42 loc) · 1.72 KB
/
Copy pathexample_test.go
File metadata and controls
50 lines (42 loc) · 1.72 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
package spannerplan_test
import (
"errors"
"fmt"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"google.golang.org/protobuf/types/known/structpb"
"github.com/apstndb/spannerplan"
)
// ExampleNew_invalidPlan shows how to detect a malformed plan without pinning
// the error message text: errors.Is against ErrInvalidPlan matches any
// validation failure, and errors.As exposes the structured *ValidationError.
func ExampleNew_invalidPlan() {
// A child link references a node index that does not exist.
_, err := spannerplan.New([]*sppb.PlanNode{
{Index: 0, ChildLinks: []*sppb.PlanNode_ChildLink{{ChildIndex: 99}}},
{Index: 1},
})
fmt.Println("is ErrInvalidPlan:", errors.Is(err, spannerplan.ErrInvalidPlan))
fmt.Println("is ErrChildLinkIndexOutOfRange:", errors.Is(err, spannerplan.ErrChildLinkIndexOutOfRange))
var verr *spannerplan.ValidationError
if errors.As(err, &verr) {
fmt.Printf("node %d, child link %d\n", verr.NodeIndex, verr.ChildIndex)
}
// Output:
// is ErrInvalidPlan: true
// is ErrChildLinkIndexOutOfRange: true
// node 0, child link 0
}
func ExampleWithConciseMetadata() {
node := &sppb.PlanNode{DisplayName: "Filter Scan", Metadata: &structpb.Struct{Fields: map[string]*structpb.Value{
"seekable_key_size": structpb.NewStringValue("0"),
"scan_method": structpb.NewStringValue("Automatic"),
"execution_method": structpb.NewStringValue("Row"),
}}}
fmt.Println(spannerplan.NodeTitle(node))
fmt.Println(spannerplan.NodeTitle(node, spannerplan.WithConciseMetadata(true)))
fmt.Println(node.Metadata.Fields["scan_method"].GetStringValue())
// Output:
// Filter Scan (execution_method: Row, scan_method: Automatic, seekable_key_size: 0)
// Filter Scan (execution_method: Row)
// Automatic
}