From a89a2e3ecb296c0943ca7027f98aa0975f51e4e1 Mon Sep 17 00:00:00 2001 From: Boris Ershov Date: Thu, 22 Jul 2021 23:22:11 +0700 Subject: [PATCH] Changed func arg list in users module --- users.go | 87 ++++++++++++++++++++++++++++++++------------------- users_test.go | 12 ++++--- 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/users.go b/users.go index f6fc1a5..bf7dab0 100644 --- a/users.go +++ b/users.go @@ -6,40 +6,30 @@ import ( "strconv" ) +// UserStatus defines user status type +type UserStatus int + +// UserNotification defines user notification type +type UserNotification string + // UserStatus const const ( - UserStatusActive = 1 - UserStatusRegistered = 2 - UserStatusLocked = 3 + UserStatusAnonymous UserStatus = 0 + UserStatusActive UserStatus = 1 + UserStatusRegistered UserStatus = 2 + UserStatusLocked UserStatus = 3 ) // UserNotification const const ( - UserNotificationAll = 1 - UserNotificationSelected = 2 - UserNotificationOnlyMyEvents = 3 - UserNotificationOnlyAssigned = 4 - UserNotificationOnlyOwner = 5 - UserNotificationOnlyNone = 6 + UserNotificationAll UserNotification = "all" + UserNotificationSelected UserNotification = "selected" + UserNotificationOnlyMyEvents UserNotification = "only_my_events" + UserNotificationOnlyAssigned UserNotification = "only_assigned" + UserNotificationOnlyOwner UserNotification = "only_owner" + UserNotificationOnlyNone UserNotification = "none" ) -// UserStatus names in Redmine -var UserStatus = map[int]string{ - UserStatusActive: "active", - UserStatusRegistered: "registered", - UserStatusLocked: "locked", -} - -// UserNotification names in Redmine -var UserNotification = map[int]string{ - UserNotificationAll: "all", - UserNotificationSelected: "selected", - UserNotificationOnlyMyEvents: "only_my_events", - UserNotificationOnlyAssigned: "only_assigned", - UserNotificationOnlyOwner: "only_owner", - UserNotificationOnlyNone: "none", -} - /* Get */ // UserObject struct used for users get operations @@ -52,7 +42,7 @@ type UserObject struct { CreatedOn string `json:"created_on"` LastLoginOn string `json:"last_login_on"` APIKey string `json:"api_key"` // used only: get single user - Status int `json:"status"` // used only: get single user + Status UserStatus `json:"status"` // used only: get single user CustomFields []CustomFieldGetObject `json:"custom_fields"` Groups []IDName `json:"groups"` // used only: get single user Memberships []UserMembershipObject `json:"memberships"` // used only: get single user @@ -113,9 +103,19 @@ type UserMultiGetRequest struct { Limit int } +// UserSingleGetRequest contains data for making request to get specified user +type UserSingleGetRequest struct { + Includes []string +} + +// UserCurrentGetRequest contains data for making request to get current user +type UserCurrentGetRequest struct { + Includes []string +} + // UserGetRequestFilters contains data for making users get request type UserGetRequestFilters struct { - Status int + Status UserStatus Name string GroupID int } @@ -144,6 +144,27 @@ type userUpdate struct { User UserUpdateObject `json:"user"` } +func (u UserStatus) String() string { + + status := map[UserStatus]string{ + UserStatusAnonymous: "anonymous", + UserStatusActive: "active", + UserStatusRegistered: "registered", + UserStatusLocked: "locked", + } + + s, b := status[u] + if b == false { + return "unknown" + } + + return s +} + +func (u UserNotification) String() string { + return string(u) +} + // UserAllGet gets info for all users satisfying specified filters // // see: http://www.redmine.org/projects/redmine/wiki/Rest_Users#GET @@ -222,14 +243,14 @@ func (r *Context) UserMultiGet(request UserMultiGetRequest) (UserResult, int, er // Available includes: // * groups // * memberships -func (r *Context) UserSingleGet(id int, includes []string) (UserObject, int, error) { +func (r *Context) UserSingleGet(id int, request UserSingleGetRequest) (UserObject, int, error) { var u userSingleResult urlParams := url.Values{} // Preparing includes - urlIncludes(&urlParams, includes) + urlIncludes(&urlParams, request.Includes) ur := url.URL{ Path: "/users/" + strconv.Itoa(id) + ".json", @@ -248,14 +269,14 @@ func (r *Context) UserSingleGet(id int, includes []string) (UserObject, int, err // Available includes: // * groups // * memberships -func (r *Context) UserCurrentGet(includes []string) (UserObject, int, error) { +func (r *Context) UserCurrentGet(request UserCurrentGetRequest) (UserObject, int, error) { var u userSingleResult urlParams := url.Values{} // Preparing includes - urlIncludes(&urlParams, includes) + urlIncludes(&urlParams, request.Includes) ur := url.URL{ Path: "/users/current.json", @@ -317,7 +338,7 @@ func userURLFilters(urlParams *url.Values, filters UserGetRequestFilters) { filters.Status = 1 } - urlParams.Add("status", strconv.Itoa(filters.Status)) + urlParams.Add("status", strconv.Itoa(int(filters.Status))) if len(filters.Name) > 0 { urlParams.Add("name", filters.Name) diff --git a/users_test.go b/users_test.go index 1873f1e..bc4698a 100644 --- a/users_test.go +++ b/users_test.go @@ -42,7 +42,7 @@ func testUserCreate(t *testing.T, r Context) UserObject { FirstName: testUserFirstName, LastName: testUserLastName, Mail: testUserMail, - MailNotification: UserNotification[UserNotificationOnlyAssigned], + MailNotification: UserNotificationOnlyAssigned.String(), MustChangePasswd: true, GeneratePassword: true, }) @@ -60,7 +60,7 @@ func testUserUpdate(t *testing.T, r Context, id int) { _, err := r.UserUpdate(id, UserUpdateObject{ FirstName: testUserFirstName2, LastName: testUserLastName2, - MailNotification: UserNotification[UserNotificationOnlyNone], + MailNotification: UserNotificationOnlyNone.String(), }) if err != nil { t.Fatal("User update error:", err) @@ -104,7 +104,9 @@ func testUserAllGet(t *testing.T, r Context) { func testUserSingleGet(t *testing.T, r Context, id int) { - _, _, err := r.UserSingleGet(id, []string{"groups", "memberships"}) + _, _, err := r.UserSingleGet(id, UserSingleGetRequest{ + Includes: []string{"groups", "memberships"}, + }) if err != nil { t.Fatal("User get error:", err) } @@ -114,7 +116,9 @@ func testUserSingleGet(t *testing.T, r Context, id int) { func testUserCurrentGet(t *testing.T, r Context) { - _, _, err := r.UserCurrentGet([]string{"groups", "memberships"}) + _, _, err := r.UserCurrentGet(UserCurrentGetRequest{ + Includes: []string{"groups", "memberships"}, + }) if err != nil { t.Fatal("Current user get error:", err) }