ocket8888 commented on code in PR #7395:
URL: https://github.com/apache/trafficcontrol/pull/7395#discussion_r1159043545


##########
traffic_ops/traffic_ops_golang/test/helpers.go:
##########
@@ -26,16 +26,57 @@ import (
        "strings"
 )
 
-// Extract the tag annotations from a struct into a string array
+// ColsFromStructByTag extracts the tag annotations from a struct into a 
string array.
 func ColsFromStructByTag(tagName string, thing interface{}) []string {
-       cols := []string{}
+       return ColsFromStructByTagExclude(tagName, thing, nil)
+}
+
+// InsertAtStr inserts insertMap (string to insert at -> []insert names) into 
cols non-destructively.
+func InsertAtStr(cols *[]string, insertMap map[string][]string) *[]string {

Review Comment:
   slices are already reference types, so unless a pointer to `nil` is somehow 
meaningfully different than just `nil`, these `*[]string`s should probably just 
be `[]string`s.



##########
lib/go-tc/users.go:
##########
@@ -260,7 +260,7 @@ type UsersResponseV4 struct {
        Alerts
 }
 
-// UserResponseV4 is the type of a response from Traffic Ops to requests made
+// UserResponseV4 is the type of response from Traffic Ops to requests made

Review Comment:
   you don't need to change this, but just for future reference this was more 
appropriate with the `a` IMO, since it's describing a strict typing of a 
particular response rather than the kind or category of a set of related 
responses.



##########
traffic_ops/traffic_ops_golang/user/user_test.go:
##########
@@ -0,0 +1,347 @@
+package user
+
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import (
+       "testing"
+       "time"
+
+       "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/lib/go-util"
+)
+
+var (
+       user = &tc.UserV4{
+               AddressLine1:      util.Ptr("line1"),
+               AddressLine2:      util.Ptr("line2"),
+               ChangeLogCount:    8,
+               City:              util.Ptr("city"),
+               Company:           util.Ptr("company"),
+               Country:           util.Ptr("country"),
+               Email:             util.Ptr("[email protected]"),
+               FullName:          util.Ptr("Testy Mctestface"),
+               GID:               nil,
+               ID:                util.Ptr(1),
+               LastAuthenticated: util.Ptr(time.Now()),
+               LastUpdated:       time.Now(),
+               LocalPassword:     util.Ptr("pw"),
+               NewUser:           false,
+               PhoneNumber:       util.Ptr("999-999-9999"),
+               PostalCode:        util.Ptr("11111-1111"),
+               PublicSSHKey:      nil,
+               RegistrationSent:  util.Ptr(time.Now()),
+               Role:              "role",
+               StateOrProvince:   util.Ptr("state"),
+               Tenant:            nil,
+               TenantID:          0,
+               Token:             nil,
+               UCDN:              "",
+               UID:               nil,
+               Username:          "testy",
+       }
+       oldUser = defineUser()
+)
+
+func defineUser() tc.User {
+       u := tc.User{
+               Username:             util.Ptr(user.Username),
+               RegistrationSent:     nil,
+               LocalPassword:        user.LocalPassword,
+               ConfirmLocalPassword: user.LocalPassword,
+               RoleName:             util.Ptr(user.Role),
+       }
+       u.AddressLine1 = user.AddressLine1
+       u.AddressLine2 = user.AddressLine2
+       u.City = user.City
+       u.Company = user.Company
+       u.Country = user.Country
+       u.Email = user.Email
+       u.FullName = user.FullName
+       u.GID = user.GID
+       u.ID = user.ID
+       u.LastUpdated = tc.TimeNoModFromTime(user.LastUpdated)
+       u.NewUser = util.Ptr(user.NewUser)
+       u.PhoneNumber = user.PhoneNumber
+       u.PostalCode = user.PostalCode
+       u.PublicSSHKey = user.PublicSSHKey
+       u.Role = nil
+       u.StateOrProvince = user.StateOrProvince
+       u.Tenant = user.Tenant
+       u.TenantID = util.Ptr(user.TenantID)
+       u.Token = user.Token
+       u.UID = user.UID
+       return u
+}
+
+func TestDowngrade(t *testing.T) {
+       old := user.Downgrade()
+
+       if *old.FullName != *oldUser.FullName {
+               t.Fatalf("expected FullName to be equal, got %v and %v", 
*old.FullName, *oldUser.FullName)

Review Comment:
   nit: `%v` seems like a bad idea when the type is known. You get more safety 
out of being more specific.



##########
traffic_ops/traffic_ops_golang/test/helpers.go:
##########
@@ -26,16 +26,57 @@ import (
        "strings"
 )
 
-// Extract the tag annotations from a struct into a string array
+// ColsFromStructByTag extracts the tag annotations from a struct into a 
string array.
 func ColsFromStructByTag(tagName string, thing interface{}) []string {
-       cols := []string{}
+       return ColsFromStructByTagExclude(tagName, thing, nil)
+}
+
+// InsertAtStr inserts insertMap (string to insert at -> []insert names) into 
cols non-destructively.
+func InsertAtStr(cols *[]string, insertMap map[string][]string) *[]string {
+       if insertMap == nil {
+               return cols
+       }
+       if cols == nil {
+               return nil
+       }
+
+       colLen := len(*cols)
+       insertLen := 0
+       for _, val := range insertMap {
+               insertLen += len(val)
+       }
+       newColumns := make([]string, colLen+insertLen)
+       oldIndex := 0
+       for newIndex := 0; newIndex < len(newColumns); newIndex++ {
+               newColumns[newIndex] = (*cols)[oldIndex]
+               if inserts, ok := insertMap[newColumns[newIndex]]; ok {
+                       for j, insert := range inserts {
+                               newColumns[newIndex+j+1] = insert
+                       }
+                       newIndex += len(inserts)
+               }
+               oldIndex++
+       }
+       return &newColumns
+}
+
+// ColsFromStructByTagExclude extracts the tag annotations from a struct into 
a string array except for excludedColumns.
+func ColsFromStructByTagExclude(tagName string, thing interface{}, 
excludeColumns *[]string) []string {

Review Comment:
   same as above RE: reference typings



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to