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

Avoid nil pointer when looking up user #12

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions instruqt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ type userInfoQuery struct {
// User represents the data structure for an Instruqt user.
type User struct {
Id string
Details struct { // Detailed user information associated with a specific team.
FirstName graphql.String // The first name of the user.
LastName graphql.String // The last name of the user.
Email graphql.String // The email of the user.
} `graphql:"details(teamSlug: $teamSlug)"`
Profile struct { // Profile-level information for the user.
Display_Name graphql.String // The display name of the user.
Email graphql.String // The email of the user.
}
Details *UserDetails `graphql:"details(teamSlug: $teamSlug)"`
Profile *UserProfile
}

// Detailed user information associated with a specific team.
type UserDetails struct {
FirstName graphql.String // The first name of the user.
LastName graphql.String // The last name of the user.
Email graphql.String // The email of the user.
}

// Profile-level information for the user.
type UserProfile struct {
Display_Name graphql.String // The display name of the user.
Email graphql.String // The email of the user.
}

// UserInfo represents a simplified user information structure.
Expand Down Expand Up @@ -66,7 +72,7 @@ func (c *Client) GetUserInfo(userId string) (u UserInfo, err error) {
return u, fmt.Errorf("[GetUserInfo] Failed to retrieve user info: %v", err)
}

if q.User.Details.Email != "" {
if q.User.Details != nil && q.User.Details.Email != "" {
c.InfoLogger.Printf("[Instruqt][GetUserInfo][%s] Found user info from instruqt user details", userId)
u = UserInfo{
FirstName: string(q.User.Details.FirstName),
Expand All @@ -76,7 +82,7 @@ func (c *Client) GetUserInfo(userId string) (u UserInfo, err error) {
return u, nil
}

if q.User.Profile.Email != "" {
if q.User.Profile != nil && q.User.Profile.Email != "" {
c.InfoLogger.Printf("[Instruqt][GetUserInfo][%s] Found user info from instruqt user profile", userId)
nameParts := strings.Fields(string(q.User.Profile.Display_Name))
u = UserInfo{
Expand Down
11 changes: 2 additions & 9 deletions instruqt/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ func TestGetUserInfo_Details(t *testing.T) {

queryResult := userInfoQuery{
User: User{
Details: struct {
FirstName graphql.String
LastName graphql.String
Email graphql.String
}{
Details: &UserDetails{
FirstName: graphql.String("John"),
LastName: graphql.String("Doe"),
Email: graphql.String("[email protected]"),
Expand Down Expand Up @@ -80,10 +76,7 @@ func TestGetUserInfo_Profile(t *testing.T) {

queryResult := userInfoQuery{
User: User{
Profile: struct {
Display_Name graphql.String
Email graphql.String
}{
Profile: &UserProfile{
Display_Name: graphql.String("Jane Smith"),
Email: graphql.String("[email protected]"),
},
Expand Down
Loading