Skip to content

Commit

Permalink
feat: Extend User struct by adding Data, Name and Segment (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
cleptric committed Oct 28, 2022
1 parent af05cee commit e78f722
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# Changelog

- feat: Extend User inteface by adding Data, Name and Segment (#483)

## 0.14.0

- feat: Add function to continue from trace string (#434)
Expand Down
43 changes: 39 additions & 4 deletions interfaces.go
Expand Up @@ -93,10 +93,45 @@ func (b *Breadcrumb) MarshalJSON() ([]byte, error) {
// User describes the user associated with an Event. If this is used, at least
// an ID or an IP address should be provided.
type User struct {
Email string `json:"email,omitempty"`
ID string `json:"id,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
Username string `json:"username,omitempty"`
ID string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
Username string `json:"username,omitempty"`
Name string `json:"name,omitempty"`
Segment string `json:"segment,omitempty"`
Data map[string]string `json:"data,omitempty"`
}

func (u User) IsEmpty() bool {
if len(u.ID) > 0 {
return false
}

if len(u.Email) > 0 {
return false
}

if len(u.IPAddress) > 0 {
return false
}

if len(u.Username) > 0 {
return false
}

if len(u.Name) > 0 {
return false
}

if len(u.Segment) > 0 {
return false
}

if len(u.Data) > 0 {
return false
}

return true
}

// Request contains information on a HTTP request related to the event.
Expand Down
46 changes: 46 additions & 0 deletions interfaces_test.go
Expand Up @@ -19,6 +19,52 @@ var (
generate = flag.Bool("gen", false, "generate missing .golden files")
)

func TestUserIsEmpty(t *testing.T) {
tests := []struct {
input User
want bool
}{
{input: User{}, want: true},
{input: User{ID: "foo"}, want: false},
{input: User{Email: "foo@example.com"}, want: false},
{input: User{IPAddress: "127.0.0.1"}, want: false},
{input: User{Username: "My Username"}, want: false},
{input: User{Name: "My Name"}, want: false},
{input: User{Segment: "My Segment"}, want: false},
{input: User{Data: map[string]string{"foo": "bar"}}, want: false},
{input: User{ID: "foo", Email: "foo@example.com", IPAddress: "127.0.0.1", Username: "My Username", Name: "My Name", Segment: "My Segment", Data: map[string]string{"foo": "bar"}}, want: false},
}

for _, test := range tests {
assertEqual(t, test.input.IsEmpty(), test.want)
}
}

func TestUserMarshalJson(t *testing.T) {
tests := []struct {
input User
want string
}{
{input: User{}, want: `{}`},
{input: User{ID: "foo"}, want: `{"id":"foo"}`},
{input: User{Email: "foo@example.com"}, want: `{"email":"foo@example.com"}`},
{input: User{IPAddress: "127.0.0.1"}, want: `{"ip_address":"127.0.0.1"}`},
{input: User{Username: "My Username"}, want: `{"username":"My Username"}`},
{input: User{Name: "My Name"}, want: `{"name":"My Name"}`},
{input: User{Segment: "My Segment"}, want: `{"segment":"My Segment"}`},
{input: User{Data: map[string]string{"foo": "bar"}}, want: `{"data":{"foo":"bar"}}`},
}

for _, test := range tests {
got, err := json.Marshal(test.input)
if err != nil {
t.Fatal(err)
}

assertEqual(t, string(got), test.want)
}
}

func TestNewRequest(t *testing.T) {
const payload = `{"test_data": true}`
got := NewRequest(httptest.NewRequest("POST", "/test/?q=sentry", strings.NewReader(payload)))
Expand Down
3 changes: 1 addition & 2 deletions scope.go
Expand Up @@ -379,8 +379,7 @@ func (scope *Scope) ApplyToEvent(event *Event, hint *EventHint) *Event {
}
}

var emptyUser User
if event.User == emptyUser {
if event.User.IsEmpty() {
event.User = scope.user
}

Expand Down
5 changes: 3 additions & 2 deletions scope_test.go
Expand Up @@ -37,8 +37,9 @@ func fillEventWithData(event *Event) *Event {

func TestScopeSetUser(t *testing.T) {
scope := NewScope()
scope.SetUser(User{ID: "foo"})
assertEqual(t, User{ID: "foo"}, scope.user)
scope.SetUser(User{ID: "foo", Email: "foo@example.com", IPAddress: "127.0.0.1", Username: "My Username", Name: "My Name", Segment: "My Segment", Data: map[string]string{"foo": "bar"}})

assertEqual(t, User{ID: "foo", Email: "foo@example.com", IPAddress: "127.0.0.1", Username: "My Username", Name: "My Name", Segment: "My Segment", Data: map[string]string{"foo": "bar"}}, scope.user)
}

func TestScopeSetUserOverrides(t *testing.T) {
Expand Down

0 comments on commit e78f722

Please sign in to comment.