forked from segmentio/analytics-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
65 lines (62 loc) · 1.53 KB
/
validate.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
package analytics
type FieldGetter interface {
GetField(field string) (interface{}, bool)
}
func getString(msg FieldGetter, field string) string {
if val, ok := msg.GetField(field); ok {
if str, ok := val.(string); ok {
return str
}
}
return ""
}
func ValidateFields(msg FieldGetter) error {
typ, _ := msg.GetField("type")
if str, ok := typ.(string); ok {
switch str {
case "alias":
return Alias{
Type: "alias",
UserId: getString(msg, "userId"),
PreviousId: getString(msg, "previousId"),
}.Validate()
case "group":
return Group{
Type: "group",
UserId: getString(msg, "userId"),
AnonymousId: getString(msg, "anonymousId"),
GroupId: getString(msg, "groupId"),
}.Validate()
case "identify":
return Identify{
Type: "identify",
UserId: getString(msg, "userId"),
AnonymousId: getString(msg, "anonymousId"),
}.Validate()
case "page":
return Page{
Type: "page",
UserId: getString(msg, "userId"),
AnonymousId: getString(msg, "anonymousId"),
}.Validate()
case "screen":
return Screen{
Type: "screen",
UserId: getString(msg, "userId"),
AnonymousId: getString(msg, "anonymousId"),
}.Validate()
case "track":
return Track{
Type: "track",
UserId: getString(msg, "userId"),
AnonymousId: getString(msg, "anonymousId"),
Event: getString(msg, "event"),
}.Validate()
}
}
return FieldError{
Type: "analytics.Event",
Name: "Type",
Value: typ,
}
}