Skip to content

Commit

Permalink
Merge pull request #96 from rollbar/pawel/extra_person
Browse files Browse the repository at this point in the history
feat: Add support for extra fields on Person details
  • Loading branch information
pawelsz-rb authored Jun 17, 2022
2 parents 474033b + 01152f5 commit ea8fda4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rollbar.test
.env
cover.out
cover.html
.idea
16 changes: 15 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,25 @@ func (c *client) SetCustom(custom map[string]interface{}) {
// SetPerson information for identifying a user associated with
// any subsequent errors or messages. Only id is required to be
// non-empty.
func (c *client) SetPerson(id, username, email string) {

type personOption func(*Person)

func WithPersonExtra(extra map[string]string) personOption {
return func(p *Person) {
p.Extra = extra
}
}
func (c *client) SetPerson(id, username, email string, opts ...personOption) {
person := Person{
Id: id,
Username: username,
Email: email,
}
for _, opt := range opts {
// Call the option giving the instantiated
// *Person as the argument
opt(&person)
}

c.configuration.person = person
}
Expand Down Expand Up @@ -634,6 +647,7 @@ type Person struct {
Id string
Username string
Email string
Extra map[string]string
}

type pkey int
Expand Down
6 changes: 5 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,9 @@ func TestSetPerson(t *testing.T) {
client := testClient()
id, username, email := "42", "bork", "bork@foobar.com"

client.SetPerson(id, username, email)
client.SetPerson(id, username, email, WithPersonExtra(map[string]string{
"person_extra1": "value1", "person_extra2": "value2", "id": "43"}))

client.ErrorWithLevel(ERR, errors.New("Person Bork"))

if transport, ok := client.Transport.(*TestTransport); ok {
Expand All @@ -554,6 +556,8 @@ func TestSetPerson(t *testing.T) {
errorIfNotEqual(id, person["id"], t)
errorIfNotEqual(username, person["username"], t)
errorIfNotEqual(email, person["email"], t)
errorIfNotEqual("value1", person["person_extra1"], t)
errorIfNotEqual("value2", person["person_extra2"], t)

configuredOptions := configuredOptionsFromData(data)
configuredPerson := configuredOptions["person"].(map[string]string)
Expand Down
4 changes: 2 additions & 2 deletions rollbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ func SetCheckIgnore(checkIgnore func(string) bool) {
// SetPerson information for identifying a user associated with
// any subsequent errors or messages. Only id is required to be
// non-empty.
func SetPerson(id, username, email string) {
std.SetPerson(id, username, email)
func SetPerson(id, username, email string, opts ...personOption) {
std.SetPerson(id, username, email, opts...)
}

// ClearPerson clears any previously set person information. See `SetPerson` for more information.
Expand Down
2 changes: 2 additions & 0 deletions rollbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestErrorClass(t *testing.T) {
func TestEverything(t *testing.T) {
SetToken(os.Getenv("TOKEN"))
SetEnvironment("test")
SetPerson("1", "user", "email")
if Token() != os.Getenv("TOKEN") {
t.Error("Token should be as set")
}
Expand Down Expand Up @@ -106,6 +107,7 @@ type someNonstandardTypeForLogFailing struct{}
func TestEverythingGeneric(t *testing.T) {
SetToken(os.Getenv("TOKEN"))
SetEnvironment("test")
SetPerson("1", "user", "email", WithPersonExtra(map[string]string{"v": "k"}))
SetCaptureIp(CaptureIpAnonymize)
if Token() != os.Getenv("TOKEN") {
t.Error("Token should be as set")
Expand Down
10 changes: 9 additions & 1 deletion transforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ func buildBody(ctx context.Context, configuration configuration, diagnostic diag
person = &configuration.person
}
if person.Id != "" {
data["person"] = map[string]string{
personData := map[string]string{
"id": person.Id,
"username": person.Username,
"email": person.Email,
}
for key, value := range person.Extra {
// If the field on the extra map is already specified then skip it.
// This will prevent the extra map from overwriting fields like ID, Username or Email.
if _, ok := personData[key]; !ok {
personData[key] = value
}
}
data["person"] = personData
}

return map[string]interface{}{
Expand Down

0 comments on commit ea8fda4

Please sign in to comment.