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

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


The following commit(s) were added to refs/heads/master by this push:
     new ad7ab10  validation checks for divisions
ad7ab10 is described below

commit ad7ab10852e9831e75c5ca665174c8902c4a809e
Author: Dan Kirkwood <dang...@apache.org>
AuthorDate: Thu Mar 1 21:56:03 2018 -0700

    validation checks for divisions
---
 lib/go-tc/divisions.go                             |  6 ++++
 .../traffic_ops_golang/division/divisions.go       | 40 ++++++++++++++--------
 .../traffic_ops_golang/division/divisions_test.go  | 11 ++++++
 3 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/lib/go-tc/divisions.go b/lib/go-tc/divisions.go
index fd782d8..e3150e1 100644
--- a/lib/go-tc/divisions.go
+++ b/lib/go-tc/divisions.go
@@ -28,3 +28,9 @@ type Division struct {
        LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"`
        Name        string    `json:"name" db:"name"`
 }
+
+type DivisionNullable struct {
+       ID          *int       `json:"id" db:"id"`
+       LastUpdated *TimeNoMod `json:"lastUpdated" db:"last_updated"`
+       Name        *string    `json:"name" db:"name"`
+}
diff --git a/traffic_ops/traffic_ops_golang/division/divisions.go 
b/traffic_ops/traffic_ops_golang/division/divisions.go
index f548912..43caa78 100644
--- a/traffic_ops/traffic_ops_golang/division/divisions.go
+++ b/traffic_ops/traffic_ops_golang/division/divisions.go
@@ -22,48 +22,58 @@ package division
 import (
        "errors"
        "fmt"
+       "strconv"
 
        "github.com/apache/incubator-trafficcontrol/lib/go-log"
        "github.com/apache/incubator-trafficcontrol/lib/go-tc"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
        
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+       
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tovalidate"
+       validation "github.com/go-ozzo/ozzo-validation"
        "github.com/jmoiron/sqlx"
        "github.com/lib/pq"
 )
 
 //we need a type alias to define functions on
-type TODivision tc.Division
+type TODivision tc.DivisionNullable
 
 //the refType is passed into the handlers where a copy of its type is used to 
decode the json.
-var refType = TODivision(tc.Division{})
+var refType = TODivision(tc.DivisionNullable{})
 
 func GetRefType() *TODivision {
        return &refType
 }
 
-func (division *TODivision) GetAuditName() string {
-       return division.Name
+func (division TODivision) GetAuditName() string {
+       if division.Name == nil {
+               id, _ := division.GetID()
+               return strconv.Itoa(id)
+       }
+       return *division.Name
 }
 
 //Implementation of the Identifier, Validator interface functions
-func (division *TODivision) GetID() (int, bool) {
-       return division.ID, true
+func (division TODivision) GetID() (int, bool) {
+       if division.ID == nil {
+               return 0, false
+       }
+       return *division.ID, true
 }
 
 func (division *TODivision) SetID(i int) {
-       division.ID = i
+       division.ID = &i
 }
-func (division *TODivision) GetType() string {
+
+func (division TODivision) GetType() string {
        return "division"
 }
 
-func (division *TODivision) Validate(db *sqlx.DB) []error {
-       errs := []error{}
-       if len(division.Name) < 1 {
-               errs = append(errs, errors.New(`Division 'name' is required.`))
+func (division TODivision) Validate(db *sqlx.DB) []error {
+       errs := validation.Errors{
+               "name": validation.Validate(division.Name, validation.NotNil, 
validation.Required),
        }
-       return errs
+       return tovalidate.ToErrors(errs)
 }
 
 //The TODivision implementation of the Creator interface
@@ -125,7 +135,7 @@ func (division *TODivision) Create(db *sqlx.DB, user 
auth.CurrentUser) (error, t
                return tc.DBError, tc.SystemError
        }
        division.SetID(id)
-       division.LastUpdated = lastUpdated
+       division.LastUpdated = &lastUpdated
        err = tx.Commit()
        if err != nil {
                log.Errorln("Could not commit transaction: ", err)
@@ -218,7 +228,7 @@ func (division *TODivision) Update(db *sqlx.DB, user 
auth.CurrentUser) (error, t
                }
        }
        log.Debugf("lastUpdated: %++v", lastUpdated)
-       division.LastUpdated = lastUpdated
+       division.LastUpdated = &lastUpdated
        if rowsAffected != 1 {
                if rowsAffected < 1 {
                        return errors.New("no division found with this id"), 
tc.DataMissingError
diff --git a/traffic_ops/traffic_ops_golang/division/divisions_test.go 
b/traffic_ops/traffic_ops_golang/division/divisions_test.go
index c783838..dccf31b 100644
--- a/traffic_ops/traffic_ops_golang/division/divisions_test.go
+++ b/traffic_ops/traffic_ops_golang/division/divisions_test.go
@@ -20,6 +20,7 @@ package division
  */
 
 import (
+       "reflect"
        "testing"
        "time"
 
@@ -102,3 +103,13 @@ func TestInterfaces(t *testing.T) {
                t.Errorf("division must be Identifier")
        }
 }
+
+func TestValidation(t *testing.T) {
+       div := TODivision{}
+       errs := test.SortErrors(div.Validate(nil))
+       expected := []error{}
+
+       if reflect.DeepEqual(expected, errs) {
+               t.Errorf(`expected %v,  got %v`, expected, errs)
+       }
+}

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

Reply via email to