|
| 1 | +package analytics |
| 2 | + |
| 3 | +type FieldGetter interface { |
| 4 | + GetField(field string) (interface{}, bool) |
| 5 | +} |
| 6 | + |
| 7 | +func getString(msg FieldGetter, field string) string { |
| 8 | + if val, ok := msg.GetField(field); ok { |
| 9 | + if str, ok := val.(string); ok { |
| 10 | + return str |
| 11 | + } |
| 12 | + } |
| 13 | + return "" |
| 14 | +} |
| 15 | + |
| 16 | +func ValidateFields(msg FieldGetter) error { |
| 17 | + typ, _ := msg.GetField("type") |
| 18 | + if str, ok := typ.(string); ok { |
| 19 | + switch str { |
| 20 | + case "alias": |
| 21 | + return Alias{ |
| 22 | + Type: "alias", |
| 23 | + UserId: getString(msg, "userId"), |
| 24 | + PreviousId: getString(msg, "previousId"), |
| 25 | + }.Validate() |
| 26 | + case "group": |
| 27 | + return Group{ |
| 28 | + Type: "group", |
| 29 | + UserId: getString(msg, "userId"), |
| 30 | + AnonymousId: getString(msg, "anonymousId"), |
| 31 | + GroupId: getString(msg, "groupId"), |
| 32 | + }.Validate() |
| 33 | + case "identify": |
| 34 | + return Identify{ |
| 35 | + Type: "identify", |
| 36 | + UserId: getString(msg, "userId"), |
| 37 | + AnonymousId: getString(msg, "anonymousId"), |
| 38 | + }.Validate() |
| 39 | + case "page": |
| 40 | + return Page{ |
| 41 | + Type: "page", |
| 42 | + UserId: getString(msg, "userId"), |
| 43 | + AnonymousId: getString(msg, "anonymousId"), |
| 44 | + }.Validate() |
| 45 | + case "screen": |
| 46 | + return Screen{ |
| 47 | + Type: "screen", |
| 48 | + UserId: getString(msg, "userId"), |
| 49 | + AnonymousId: getString(msg, "anonymousId"), |
| 50 | + }.Validate() |
| 51 | + case "track": |
| 52 | + return Track{ |
| 53 | + Type: "track", |
| 54 | + UserId: getString(msg, "userId"), |
| 55 | + AnonymousId: getString(msg, "anonymousId"), |
| 56 | + Event: getString(msg, "event"), |
| 57 | + }.Validate() |
| 58 | + } |
| 59 | + } |
| 60 | + return FieldError{ |
| 61 | + Type: "analytics.Event", |
| 62 | + Name: "Type", |
| 63 | + Value: typ, |
| 64 | + } |
| 65 | +} |
0 commit comments