-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
89 lines (68 loc) · 1.74 KB
/
example_test.go
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
package secret_test
import (
"encoding/json"
"fmt"
"github.com/rsjethani/secret/v3"
)
func ExampleNew() {
s := secret.New("$ecre!")
fmt.Println(s, s.Secret())
// Output: ***** $ecre!
}
func ExampleRedactAs() {
s := secret.New("$ecre!", secret.RedactAs(secret.FiveX))
fmt.Println(s, s.Secret())
s = secret.New("$ecre!", secret.RedactAs(secret.Redacted))
fmt.Println(s, s.Secret())
s = secret.New("$ecre!", secret.RedactAs("my redact hint"))
fmt.Println(s, s.Secret())
// Output:
// XXXXX $ecre!
// [REDACTED] $ecre!
// my redact hint $ecre!
}
func ExampleText_MarshalText() {
login := struct {
User string
Password secret.Text
}{
User: "John",
Password: secret.New("shh!"),
}
bytes, err := json.Marshal(&login)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
// Output: {"User":"John","Password":"*****"}
}
func ExampleText_UnmarshalText() {
login := struct {
User string
Password secret.Text
}{}
err := json.Unmarshal([]byte(`{"User":"John","Password":"$ecre!"}`), &login)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", login)
fmt.Println(login.Password.Secret())
// Output:
// {User:John Password:*****}
// $ecre!
}
func ExampleEqual() {
// Empty Texts are equal.
fmt.Println(secret.Equal(secret.Text{}, secret.Text{}))
// Initialsed Text is not equal to an empty one.
fmt.Println(secret.Equal(secret.New("hello"), secret.Text{}))
// Texts with different secret strings are not equal.
fmt.Println(secret.Equal(secret.New("hello"), secret.New("world")))
// Texts with different redact strings but same secret string are equal.
fmt.Println(secret.Equal(secret.New("hello"), secret.New("hello", secret.RedactAs(secret.FiveX))))
// Output:
// true
// false
// false
// true
}