-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
48 lines (43 loc) · 1021 Bytes
/
Copy pathexample_test.go
File metadata and controls
48 lines (43 loc) · 1021 Bytes
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
package protogofakeit_test
import (
"bytes"
"encoding/json"
"fmt"
"github.com/brianvoe/gofakeit/v6"
"github.com/rodaine/protogofakeit"
"github.com/rodaine/protogofakeit/gen/gofakeit/example"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func Example() {
faker := gofakeit.New(3)
protoFaker := protogofakeit.New(faker)
user := &example.User{}
_ = protoFaker.FakeProto(user)
fmt.Println(toJSON(user))
// Output:
// {
// "userId": "15864040051628833669",
// "firstName": "Philip",
// "lastName": "Casper",
// "email": "tyreekstroman@mitchell.biz",
// "location": {
// "latitude": 1.383491,
// "longitude": -62.968324
// },
// "hobbies": [
// "Curling",
// "Slot car",
// "Fishkeeping"
// ],
// "pets": {
// "Archie": "PET_TYPE_DOG"
// }
// }
}
func toJSON(msg proto.Message) string {
data, _ := protojson.Marshal(msg)
buf := &bytes.Buffer{}
_ = json.Indent(buf, data, "", " ")
return buf.String()
}