Skip to content

Commit 0566e48

Browse files
authored
Merge pull request #169 from segmentio/jeremy/remove-internal-message
Remove message internal-only restriction and add ValidateFields
2 parents efd445f + 2eeda24 commit 0566e48

File tree

11 files changed

+416
-35
lines changed

11 files changed

+416
-35
lines changed

alias.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ type Alias struct {
1919
Integrations Integrations `json:"integrations,omitempty"`
2020
}
2121

22-
func (msg Alias) internal() {
23-
panic(unimplementedError)
24-
}
25-
2622
func (msg Alias) Validate() error {
2723
if len(msg.UserId) == 0 {
2824
return FieldError{

analytics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"io/ioutil"
7+
"strconv"
78
"sync"
89

910
"bytes"
@@ -14,7 +15,6 @@ import (
1415

1516
// Version of the client.
1617
const Version = "3.0.0"
17-
const unimplementedError = "not implemented"
1818

1919
// This interface is the main API exposed by the analytics package.
2020
// Values that satsify this interface are returned by the client constructors
@@ -291,7 +291,7 @@ func (c *client) upload(b []byte) error {
291291

292292
req.Header.Add("User-Agent", "analytics-go (version: "+Version+")")
293293
req.Header.Add("Content-Type", "application/json")
294-
req.Header.Add("Content-Length", string(len(b)))
294+
req.Header.Add("Content-Length", strconv.Itoa(len(b)))
295295
req.SetBasicAuth(c.key, "")
296296

297297
res, err := c.http.Do(req)

analytics_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,11 @@ func (l testLogger) Errorf(format string, args ...interface{}) {
6767
}
6868

6969
var _ Message = (*testErrorMessage)(nil)
70+
7071
// Instances of this type are used to force message validation errors in unit
7172
// tests.
7273
type testErrorMessage struct{}
7374

74-
func (m testErrorMessage) internal() {
75-
}
76-
7775
func (m testErrorMessage) Validate() error { return testError }
7876

7977
var (
@@ -336,9 +334,6 @@ var _ Message = (*customMessage)(nil)
336334
type customMessage struct {
337335
}
338336

339-
func (c *customMessage) internal() {
340-
}
341-
342337
func (c *customMessage) Validate() error {
343338
return nil
344339
}

group.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ type Group struct {
2121
Integrations Integrations `json:"integrations,omitempty"`
2222
}
2323

24-
func (msg Group) internal() {
25-
panic(unimplementedError)
26-
}
27-
2824
func (msg Group) Validate() error {
2925
if len(msg.GroupId) == 0 {
3026
return FieldError{

identify.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ type Identify struct {
2020
Integrations Integrations `json:"integrations,omitempty"`
2121
}
2222

23-
func (msg Identify) internal() {
24-
panic(unimplementedError)
25-
}
26-
2723
func (msg Identify) Validate() error {
2824
if len(msg.UserId) == 0 && len(msg.AnonymousId) == 0 {
2925
return FieldError{

message.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ type Message interface {
3636
// Validate validates the internal structure of the message, the method must return
3737
// nil if the message is valid, or an error describing what went wrong.
3838
Validate() error
39-
40-
// internal is an unexposed interface function to ensure only types defined within this package can satisfy the Message interface. Invoking this method will panic.
41-
internal()
4239
}
4340

4441
// Takes a message id as first argument and returns it, unless it's the zero-

page.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ type Page struct {
2121
Integrations Integrations `json:"integrations,omitempty"`
2222
}
2323

24-
func (msg Page) internal() {
25-
panic(unimplementedError)
26-
}
27-
2824
func (msg Page) Validate() error {
2925
if len(msg.UserId) == 0 && len(msg.AnonymousId) == 0 {
3026
return FieldError{

screen.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ type Screen struct {
2121
Integrations Integrations `json:"integrations,omitempty"`
2222
}
2323

24-
func (msg Screen) internal() {
25-
panic(unimplementedError)
26-
}
27-
2824
func (msg Screen) Validate() error {
2925
if len(msg.UserId) == 0 && len(msg.AnonymousId) == 0 {
3026
return FieldError{

track.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ type Track struct {
2121
Integrations Integrations `json:"integrations,omitempty"`
2222
}
2323

24-
func (msg Track) internal() {
25-
panic(unimplementedError)
26-
}
27-
2824
func (msg Track) Validate() error {
2925
if len(msg.Event) == 0 {
3026
return FieldError{

validate.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)