-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_test.go
More file actions
171 lines (144 loc) · 4.16 KB
/
Copy pathexample_test.go
File metadata and controls
171 lines (144 loc) · 4.16 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package deidentify_test
import (
"fmt"
"log"
"strings"
"github.com/aliengiraffe/deidentify"
)
// The examples below assert on stable properties rather than on the generated
// values themselves, because the exact replacement chosen for a given input
// depends on the secret key.
func ExampleNewDeidentifier() {
// In production, generate a key once with GenerateSecretKey and store it.
// Reusing the same key keeps replacements consistent across runs.
d := deidentify.NewDeidentifier("example-secret-key")
redacted, err := d.Text("Please email Frodo Baggins at frodo.baggins@shire.me or call (555) 123-4567.")
if err != nil {
log.Fatal(err)
}
// redacted reads, for example:
// "Please email Marlo Cole at undefined6840@private.com or call (555) 777-3471."
fmt.Println(strings.Contains(redacted, "Frodo Baggins"))
fmt.Println(strings.Contains(redacted, "frodo.baggins@shire.me"))
fmt.Println(strings.Contains(redacted, "(555) 123-4567"))
// Output:
// false
// false
// false
}
func ExampleGenerateSecretKey() {
secretKey, err := deidentify.GenerateSecretKey()
if err != nil {
log.Fatal(err)
}
// The key is a hex-encoded 256-bit value.
fmt.Println(len(secretKey))
// Output: 64
}
func ExampleDeidentifier_Email() {
d := deidentify.NewDeidentifier("example-secret-key")
first, err := d.Email("bilbo@bag-end.shire")
if err != nil {
log.Fatal(err)
}
second, err := d.Email("bilbo@bag-end.shire")
if err != nil {
log.Fatal(err)
}
other, err := d.Email("frodo@shire.me")
if err != nil {
log.Fatal(err)
}
// The same address always maps to the same replacement, so records can
// still be joined on the anonymized value.
fmt.Println(first == second)
fmt.Println(first == other)
fmt.Println(strings.Contains(first, "@"))
// Output:
// true
// false
// true
}
func ExampleDeidentifier_Phone() {
d := deidentify.NewDeidentifier("example-secret-key")
redacted, err := d.Phone("(555) 123-4567")
if err != nil {
log.Fatal(err)
}
// Punctuation and area code are preserved; only the subscriber digits change.
fmt.Println(strings.HasPrefix(redacted, "(555) "))
fmt.Println(len(redacted) == len("(555) 123-4567"))
fmt.Println(redacted == "(555) 123-4567")
// Output:
// true
// true
// false
}
func ExampleDeidentifier_Table() {
d := deidentify.NewDeidentifier("example-secret-key")
table := &deidentify.Table{
Columns: []deidentify.Column{
{
Name: "customer_name",
DataType: deidentify.TypeName,
Values: []interface{}{"Gandalf Grey", nil},
},
{
Name: "email",
DataType: deidentify.TypeEmail,
Values: []interface{}{"mithrandir@wizard.com", ""},
},
},
}
result, err := d.Table(table)
if err != nil {
log.Fatal(err)
}
// Nil and empty values pass through untouched.
fmt.Println(result.Columns[0].Values[0] == "Gandalf Grey")
fmt.Println(result.Columns[0].Values[1] == nil)
fmt.Println(result.Columns[1].Values[1] == "")
// Output:
// false
// true
// true
}
func ExampleDeidentifier_Slices() {
d := deidentify.NewDeidentifier("example-secret-key")
data := [][]string{
{"Gandalf Grey", "mithrandir@wizard.com", "555-123-4567"},
{"Aragorn Strider", "ranger@gondor.me", "(555) 987-6543"},
}
// With no column types supplied, each column's type is inferred.
result, err := d.Slices(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(len(result), len(result[0]))
fmt.Println(result[0][1] == "mithrandir@wizard.com")
fmt.Println(strings.Contains(result[0][1], "@"))
// Output:
// 2 3
// false
// true
}
func ExampleDeidentifier_Slices_explicitTypes() {
d := deidentify.NewDeidentifier("example-secret-key")
data := [][]string{
{"Gandalf Grey", "mithrandir@wizard.com"},
{"Aragorn Strider", "ranger@gondor.me"},
}
// Supplying types skips inference; supplying names controls the context
// used for replacement, so the same value in two columns differs.
columnTypes := []deidentify.DataType{deidentify.TypeName, deidentify.TypeEmail}
columnNames := []string{"customer_name", "customer_email"}
result, err := d.Slices(data, columnTypes, columnNames)
if err != nil {
log.Fatal(err)
}
fmt.Println(result[0][0] == "Gandalf Grey")
fmt.Println(len(result))
// Output:
// false
// 2
}