Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Extend User struct by adding Data, Name and Segment #483

Merged
merged 7 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"`
cleptric marked this conversation as resolved.
Show resolved Hide resolved
Segment string `json:"segment,omitempty"`
cleptric marked this conversation as resolved.
Show resolved Hide resolved
Data map[string]string `json:"data,omitempty"`
}

func (u User) IsEmpty() bool {
if len(u.ID) > 0 {
return false
cleptric marked this conversation as resolved.
Show resolved Hide resolved
}

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 TestIsEmpty(t *testing.T) {
cleptric marked this conversation as resolved.
Show resolved Hide resolved
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 TestUserMarhsalJson(t *testing.T) {
cleptric marked this conversation as resolved.
Show resolved Hide resolved
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