Skip to content

Commit

Permalink
Merge pull request #169 from segmentio/jeremy/remove-internal-message
Browse files Browse the repository at this point in the history
Remove message internal-only restriction and add ValidateFields
  • Loading branch information
kalamay authored Nov 10, 2020
2 parents efd445f + 2eeda24 commit 0566e48
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 35 deletions.
4 changes: 0 additions & 4 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ type Alias struct {
Integrations Integrations `json:"integrations,omitempty"`
}

func (msg Alias) internal() {
panic(unimplementedError)
}

func (msg Alias) Validate() error {
if len(msg.UserId) == 0 {
return FieldError{
Expand Down
4 changes: 2 additions & 2 deletions analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/ioutil"
"strconv"
"sync"

"bytes"
Expand All @@ -14,7 +15,6 @@ import (

// Version of the client.
const Version = "3.0.0"
const unimplementedError = "not implemented"

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

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

res, err := c.http.Do(req)
Expand Down
7 changes: 1 addition & 6 deletions analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ func (l testLogger) Errorf(format string, args ...interface{}) {
}

var _ Message = (*testErrorMessage)(nil)

// Instances of this type are used to force message validation errors in unit
// tests.
type testErrorMessage struct{}

func (m testErrorMessage) internal() {
}

func (m testErrorMessage) Validate() error { return testError }

var (
Expand Down Expand Up @@ -336,9 +334,6 @@ var _ Message = (*customMessage)(nil)
type customMessage struct {
}

func (c *customMessage) internal() {
}

func (c *customMessage) Validate() error {
return nil
}
Expand Down
4 changes: 0 additions & 4 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type Group struct {
Integrations Integrations `json:"integrations,omitempty"`
}

func (msg Group) internal() {
panic(unimplementedError)
}

func (msg Group) Validate() error {
if len(msg.GroupId) == 0 {
return FieldError{
Expand Down
4 changes: 0 additions & 4 deletions identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ type Identify struct {
Integrations Integrations `json:"integrations,omitempty"`
}

func (msg Identify) internal() {
panic(unimplementedError)
}

func (msg Identify) Validate() error {
if len(msg.UserId) == 0 && len(msg.AnonymousId) == 0 {
return FieldError{
Expand Down
3 changes: 0 additions & 3 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ type Message interface {
// Validate validates the internal structure of the message, the method must return
// nil if the message is valid, or an error describing what went wrong.
Validate() error

// internal is an unexposed interface function to ensure only types defined within this package can satisfy the Message interface. Invoking this method will panic.
internal()
}

// Takes a message id as first argument and returns it, unless it's the zero-
Expand Down
4 changes: 0 additions & 4 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type Page struct {
Integrations Integrations `json:"integrations,omitempty"`
}

func (msg Page) internal() {
panic(unimplementedError)
}

func (msg Page) Validate() error {
if len(msg.UserId) == 0 && len(msg.AnonymousId) == 0 {
return FieldError{
Expand Down
4 changes: 0 additions & 4 deletions screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type Screen struct {
Integrations Integrations `json:"integrations,omitempty"`
}

func (msg Screen) internal() {
panic(unimplementedError)
}

func (msg Screen) Validate() error {
if len(msg.UserId) == 0 && len(msg.AnonymousId) == 0 {
return FieldError{
Expand Down
4 changes: 0 additions & 4 deletions track.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type Track struct {
Integrations Integrations `json:"integrations,omitempty"`
}

func (msg Track) internal() {
panic(unimplementedError)
}

func (msg Track) Validate() error {
if len(msg.Event) == 0 {
return FieldError{
Expand Down
65 changes: 65 additions & 0 deletions validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
}
}
Loading

0 comments on commit 0566e48

Please sign in to comment.