This is an automated email from the ASF dual-hosted git repository.

dangogh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git

commit 4e549d0c987fafff69b4d694aee7572afa7a73c0
Author: Dewayne Richardson <dewr...@apache.org>
AuthorDate: Thu Apr 5 09:23:17 2018 -0600

    updated to use constants to make query parameters more clear
---
 .../traffic_ops_golang/parameter/parameters.go     | 33 +++++++++++++---------
 traffic_ops/traffic_ops_golang/profile/profiles.go | 30 ++++++++++++--------
 2 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/traffic_ops/traffic_ops_golang/parameter/parameters.go 
b/traffic_ops/traffic_ops_golang/parameter/parameters.go
index a75de8c..7d1af69 100644
--- a/traffic_ops/traffic_ops_golang/parameter/parameters.go
+++ b/traffic_ops/traffic_ops_golang/parameter/parameters.go
@@ -36,6 +36,14 @@ import (
        "github.com/lib/pq"
 )
 
+const (
+       NameQueryParam       = "name"
+       SecureQueryParam     = "secure"
+       ConfigFileQueryParam = "configFile"
+       IDQueryParam         = "id"
+       ValueQueryParam      = "value"
+)
+
 //we need a type alias to define functions on
 type TOParameter tc.ParameterNullable
 
@@ -47,19 +55,19 @@ func GetRefType() *TOParameter {
 }
 
 func (parameter TOParameter) GetKeyFieldsInfo() []api.KeyFieldInfo {
-       return []api.KeyFieldInfo{{"id", api.GetIntKey}}
+       return []api.KeyFieldInfo{{IDQueryParam, api.GetIntKey}}
 }
 
 //Implementation of the Identifier, Validator interface functions
 func (parameter TOParameter) GetKeys() (map[string]interface{}, bool) {
        if parameter.ID == nil {
-               return map[string]interface{}{"id": 0}, false
+               return map[string]interface{}{IDQueryParam: 0}, false
        }
-       return map[string]interface{}{"id": *parameter.ID}, true
+       return map[string]interface{}{IDQueryParam: *parameter.ID}, true
 }
 
 func (parameter *TOParameter) SetKeys(keys map[string]interface{}) {
-       i, _ := keys["id"].(int) //this utilizes the non panicking type 
assertion, if the thrown away ok variable is false i will be the zero of the 
type, 0 here.
+       i, _ := keys[IDQueryParam].(int) //this utilizes the non panicking type 
assertion, if the thrown away ok variable is false i will be the zero of the 
type, 0 here.
        parameter.ID = &i
 }
 
@@ -85,9 +93,9 @@ func (parameter TOParameter) Validate(db *sqlx.DB) []error {
        // - Admin rights only
        // - Do not allow duplicate parameters by name+config_file+value
        errs := validation.Errors{
-               "name":       validation.Validate(parameter.Name, 
validation.Required),
-               "configFile": validation.Validate(parameter.ConfigFile, 
validation.Required),
-               "value":      validation.Validate(parameter.Value, 
validation.Required),
+               NameQueryParam:       validation.Validate(parameter.Name, 
validation.Required),
+               ConfigFileQueryParam: validation.Validate(parameter.ConfigFile, 
validation.Required),
+               ValueQueryParam:      validation.Validate(parameter.Value, 
validation.Required),
        }
 
        return tovalidate.ToErrors(errs)
@@ -152,7 +160,7 @@ func (pl *TOParameter) Create(db *sqlx.DB, user 
auth.CurrentUser) (error, tc.Api
                return tc.DBError, tc.SystemError
        }
 
-       pl.SetKeys(map[string]interface{}{"id": id})
+       pl.SetKeys(map[string]interface{}{IDQueryParam: id})
        pl.LastUpdated = &lastUpdated
        err = tx.Commit()
        if err != nil {
@@ -184,11 +192,10 @@ func (parameter *TOParameter) Read(db *sqlx.DB, 
parameters map[string]string, us
        // Query Parameters to Database Query column mappings
        // see the fields mapped in the SQL query
        queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
-               "config_file":  dbhelpers.WhereColumnInfo{"p.config_file", nil},
-               "id":           dbhelpers.WhereColumnInfo{"p.id", api.IsInt},
-               "last_updated": dbhelpers.WhereColumnInfo{"p.last_updated", 
nil},
-               "name":         dbhelpers.WhereColumnInfo{"p.name", nil},
-               "secure":       dbhelpers.WhereColumnInfo{"p.secure", 
api.IsBool},
+               ConfigFileQueryParam: 
dbhelpers.WhereColumnInfo{"p.config_file", nil},
+               IDQueryParam:         dbhelpers.WhereColumnInfo{"p.id", 
api.IsInt},
+               NameQueryParam:       dbhelpers.WhereColumnInfo{"p.name", nil},
+               SecureQueryParam:     dbhelpers.WhereColumnInfo{"p.secure", 
api.IsBool},
        }
 
        where, orderBy, queryValues, errs := 
dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols)
diff --git a/traffic_ops/traffic_ops_golang/profile/profiles.go 
b/traffic_ops/traffic_ops_golang/profile/profiles.go
index 4ead452..189ba54 100644
--- a/traffic_ops/traffic_ops_golang/profile/profiles.go
+++ b/traffic_ops/traffic_ops_golang/profile/profiles.go
@@ -36,6 +36,14 @@ import (
        "github.com/lib/pq"
 )
 
+const (
+       CDNQueryParam         = "cdn"
+       NameQueryParam        = "name"
+       IDQueryParam          = "id"
+       DescriptionQueryParam = "description"
+       TypeQueryParam        = "type"
+)
+
 //we need a type alias to define functions on
 type TOProfile v13.ProfileNullable
 
@@ -47,19 +55,19 @@ func GetRefType() *TOProfile {
 }
 
 func (prof TOProfile) GetKeyFieldsInfo() []api.KeyFieldInfo {
-       return []api.KeyFieldInfo{{"id", api.GetIntKey}}
+       return []api.KeyFieldInfo{{IDQueryParam, api.GetIntKey}}
 }
 
 //Implementation of the Identifier, Validator interface functions
 func (prof TOProfile) GetKeys() (map[string]interface{}, bool) {
        if prof.ID == nil {
-               return map[string]interface{}{"id": 0}, false
+               return map[string]interface{}{IDQueryParam: 0}, false
        }
-       return map[string]interface{}{"id": *prof.ID}, true
+       return map[string]interface{}{IDQueryParam: *prof.ID}, true
 }
 
 func (prof *TOProfile) SetKeys(keys map[string]interface{}) {
-       i, _ := keys["id"].(int) //this utilizes the non panicking type 
assertion, if the thrown away ok variable is false i will be the zero of the 
type, 0 here.
+       i, _ := keys[IDQueryParam].(int) //this utilizes the non panicking type 
assertion, if the thrown away ok variable is false i will be the zero of the 
type, 0 here.
        prof.ID = &i
 }
 
@@ -79,10 +87,10 @@ func (prof *TOProfile) GetType() string {
 
 func (prof *TOProfile) Validate(db *sqlx.DB) []error {
        errs := validation.Errors{
-               "name":        validation.Validate(prof.Name, 
validation.Required),
-               "description": validation.Validate(prof.Description, 
validation.Required),
-               "cdn":         validation.Validate(prof.CDNID, 
validation.Required),
-               "type":        validation.Validate(prof.Type, 
validation.Required),
+               NameQueryParam:        validation.Validate(prof.Name, 
validation.Required),
+               DescriptionQueryParam: validation.Validate(prof.Description, 
validation.Required),
+               CDNQueryParam:         validation.Validate(prof.CDNID, 
validation.Required),
+               TypeQueryParam:        validation.Validate(prof.Type, 
validation.Required),
        }
        if errs != nil {
                return tovalidate.ToErrors(errs)
@@ -96,8 +104,8 @@ func (prof *TOProfile) Read(db *sqlx.DB, parameters 
map[string]string, user auth
        // Query Parameters to Database Query column mappings
        // see the fields mapped in the SQL query
        queryParamsToQueryCols := map[string]dbhelpers.WhereColumnInfo{
-               "name": dbhelpers.WhereColumnInfo{"prof.name", nil},
-               "id":   dbhelpers.WhereColumnInfo{"prof.id", api.IsInt},
+               NameQueryParam: dbhelpers.WhereColumnInfo{"prof.name", nil},
+               IDQueryParam:   dbhelpers.WhereColumnInfo{"prof.id", api.IsInt},
        }
        where, orderBy, queryValues, errs := 
dbhelpers.BuildWhereAndOrderBy(parameters, queryParamsToQueryCols)
        if len(errs) > 0 {
@@ -269,7 +277,7 @@ func (prof *TOProfile) Create(db *sqlx.DB, user 
auth.CurrentUser) (error, tc.Api
                return tc.DBError, tc.SystemError
        }
 
-       prof.SetKeys(map[string]interface{}{"id": id})
+       prof.SetKeys(map[string]interface{}{IDQueryParam: id})
        prof.LastUpdated = &lastUpdated
        err = tx.Commit()
        if err != nil {

-- 
To stop receiving notification emails like this one, please contact
dang...@apache.org.

Reply via email to