Debian Codegraph @

runtimevar/constantvar/example_test.go

raw
1 // Copyright 2018 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 constantvar_test
16
17 import (
18 "context"
19 "errors"
20 "fmt"
21 "log"
22 "os"
23
24 "gocloud.dev/runtimevar"
25 "gocloud.dev/runtimevar/constantvar"
26 )
27
28 func ExampleNew() {
29 // Construct a *runtimevar.Variable that always returns "hello world".
30 v := constantvar.New("hello world")
31 defer v.Close()
32
33 // We can now read the current value of the variable from v.
34 snapshot, err := v.Latest(context.Background())
35 if err != nil {
36 log.Fatal(err)
37 }
38 fmt.Println(snapshot.Value.(string))
39
40 // Output:
41 // hello world
42 }
43
44 func ExampleNewBytes() {
45 // Construct a *runtimevar.Variable with a []byte.
46 v := constantvar.NewBytes([]byte(`hello world`), runtimevar.BytesDecoder)
47 defer v.Close()
48
49 // We can now read the current value of the variable from v.
50 snapshot, err := v.Latest(context.Background())
51 if err != nil {
52 log.Fatal(err)
53 }
54 fmt.Printf("byte slice of length %d\n", len(snapshot.Value.([]byte)))
55
56 // Output:
57 // byte slice of length 11
58 }
59
60 func ExampleNewFromEnv() {
61 // Construct a *runtimevar.Variable with an environment variable name.
62 os.Setenv("MY_ENVIRONMENT_VARIABLE", "hello world")
63 v := constantvar.NewFromEnv("MY_ENVIRONMENT_VARIABLE", runtimevar.BytesDecoder)
64 defer v.Close()
65
66 // We can now read the value from v.
67 snapshot, err := v.Latest(context.Background())
68 if err != nil {
69 log.Fatal(err)
70 }
71 fmt.Printf("byte slice of length %d\n", len(snapshot.Value.([]byte)))
72
73 // Output:
74 // byte slice of length 11
75 }
76
77 func ExampleNewError() {
78 // Construct a runtimevar.Variable that always returns errFake.
79 errFake := errors.New("my error")
80 v := constantvar.NewError(errFake)
81 defer v.Close()
82
83 // We can now use Watch to read the current value of the variable
84 // from v. Note that Latest would block here since it waits for
85 // a "good" value, and v will never get one.
86 _, err := v.Watch(context.Background())
87 if err == nil {
88 log.Fatal("Expected an error!")
89 }
90 fmt.Println(err)
91
92 // Output:
93 // runtimevar (code=Unknown): my error
94 }
95
96 func Example_openVariableFromURL() {
97 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
98 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/runtimevar/constantvar"
99 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
100 ctx := context.Background()
101
102 // runtimevar.OpenVariable creates a *runtimevar.Variable from a URL.
103 // The constant value is in the URL param "val".
104 v, err := runtimevar.OpenVariable(ctx, "constant://?val=hello+world&decoder=string")
105 if err != nil {
106 log.Fatal(err)
107 }
108 defer v.Close()
109
110 // The constant value is read from an environment variable specified in "envvar".
111 v2, err := runtimevar.OpenVariable(ctx, "constant://?envvar=MY_ENVIRONMENT_VARIABLE&decoder=string")
112 if err != nil {
113 log.Fatal(err)
114 }
115 defer v2.Close()
116 // PRAGMA: On gocloud.dev, hide the rest of the function.
117 snapshot, err := v.Latest(ctx)
118 if err != nil {
119 log.Fatal(err)
120 }
121 fmt.Println(snapshot.Value.(string))
122
123 // Output
124 // hello world
125 }