Skip to content

Commit

Permalink
introduced the nullable struct to support the CUD and validating requ…
Browse files Browse the repository at this point in the history
…ired fields
  • Loading branch information
dewrich committed Jan 18, 2018
1 parent fb951f1 commit 7f17c9a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 35 deletions.
8 changes: 7 additions & 1 deletion lib/go-tc/deliveryservices.go
Expand Up @@ -15,11 +15,17 @@ package tc
limitations under the License.
*/

// Deprecated: GetDeliveryServiceResponse is deprecated use DeliveryServicesResponse
// GetDeliveryServiceResponse ...
type GetDeliveryServiceResponse struct {
Response []DeliveryService `json:"response"`
}

// DeliveryServicesResponse ...
type DeliveryServicesResponse struct {
Response []DeliveryService `json:"response"`
}

// CreateDeliveryServiceResponse ...
type CreateDeliveryServiceResponse struct {
Response []DeliveryService `json:"response"`
Expand Down Expand Up @@ -78,7 +84,7 @@ type DeliveryService struct {
MissLat float64 `json:"missLat"`
MissLong float64 `json:"missLong"`
CheckPath string `json:"checkPath"`
LastUpdated string `json:"lastUpdated"`
LastUpdated Time `json:"lastUpdated" db:"last_updated"`
Protocol int `json:"protocol"`
IPV6RoutingEnabled bool `json:"ipv6RoutingEnabled"`
RangeRequestHandling int `json:"rangeRequestHandling"`
Expand Down
@@ -1,4 +1,4 @@
package v13
package tc

/*
Expand All @@ -14,19 +14,8 @@ package v13
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"github.com/apache/incubator-trafficcontrol/lib/go-tc"
)

// This struct supports /api/1.3

// DeliveryServicesResponse ...
type DeliveryServicesResponse struct {
Response []DeliveryService `json:"response"`
}

// DeliveryService ...
type DeliveryService struct {
// DeliveryServiceNullable - a version of the deliveryservice that allows for fields to be nullable for field level existence checks
type DeliveryServiceNullable struct {
// NOTE: Fields that are pointers (with an asterisk '*') are required
// for existence checking
//
Expand Down Expand Up @@ -56,7 +45,7 @@ type DeliveryService struct {
InfoURL string `json:"infoUrl" db:"info_url"`
InitialDispersion *int `json:"initialDispersion" db:"initial_dispersion"`
IPV6RoutingEnabled bool `json:"ipv6RoutingEnabled" db:"ipv6_routing_enabled"`
LastUpdated tc.Time `json:"lastUpdated" db:"last_updated""`
LastUpdated Time `json:"lastUpdated" db:"last_updated""`
LogsEnabled *bool `json:"logsEnabled" db:"logs_enabled"`
LongDesc string `json:"longDesc" db:"long_desc"`
LongDesc1 string `json:"longDesc1" db:"long_desc_1"`
Expand Down Expand Up @@ -89,10 +78,3 @@ type DeliveryService struct {
TypeID *int `json:"typeId" db:"type"`
XMLID *string `json:"xmlId" db:"xml_id"`
}

// DeliveryServiceMatch ...
type DeliveryServiceMatch struct {
Type string `json:"type"`
SetNumber int `json:"setNumber"`
Pattern string `json:"pattern"`
}
19 changes: 14 additions & 5 deletions traffic_ops/traffic_ops_golang/deliveryservice/deliveryservices.go
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/apache/incubator-trafficcontrol/lib/go-log"
"github.com/apache/incubator-trafficcontrol/lib/go-tc"
tcapi "github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
"github.com/asaskevich/govalidator"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
Expand All @@ -40,22 +39,32 @@ import (
)

//we need a type alias to define functions on
type TODeliveryService tcapi.DeliveryService
type TODeliveryService tc.DeliveryService

//the refType is passed into the handlers where a copy of its type is used to decode the json.
var refType = TODeliveryService(tcapi.DeliveryService{})
var refType = TODeliveryService(tc.DeliveryService{})

func GetRefType() *TODeliveryService {
return &refType
}

// Nullables are only used for the Create Update and Delete(s)
type TODeliveryServiceNullable tc.DeliveryServiceNullable

//the refType is passed into the handlers where a copy of its type is used to decode the json.
var refTypeNullable = TODeliveryServiceNullable(tc.DeliveryServiceNullable{})

func GetRefTypeNullable() *TODeliveryServiceNullable {
return &refTypeNullable
}

//Implementation of the Identifier, Validator interface functions
func (ds *TODeliveryService) GetID() int {
return ds.ID
}

func (ds *TODeliveryService) GetAuditName() string {
xmlId := *ds.XMLID
xmlId := ds.XMLID
return xmlId
}

Expand All @@ -67,7 +76,7 @@ func (ds *TODeliveryService) SetID(i int) {
ds.ID = i
}

func (ds *TODeliveryService) Validate(db *sqlx.DB) []error {
func (ds *TODeliveryServiceNullable) Validate(db *sqlx.DB) []error {

noSpaces := validation.Match(regexp.MustCompile("^\\S*$"))
noSpaces.Error("cannot contain spaces")
Expand Down
11 changes: 5 additions & 6 deletions traffic_ops/traffic_ops_golang/deliveryservice/handlers.go
Expand Up @@ -26,7 +26,6 @@ import (
"net/url"

"github.com/apache/incubator-trafficcontrol/lib/go-tc"
tcapi "github.com/apache/incubator-trafficcontrol/lib/go-tc/v13"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -68,19 +67,19 @@ func Handler(db *sqlx.DB) http.HandlerFunc {
}
}

func getDeliveryServicesResponse(q url.Values, db *sqlx.DB) (*tcapi.DeliveryServicesResponse, error) {
func getDeliveryServicesResponse(q url.Values, db *sqlx.DB) (*tc.DeliveryServicesResponse, error) {
dses, err := getDeliveryServices(q, db)
if err != nil {
return nil, fmt.Errorf("getting DeliveryServices response: %v", err)
}

resp := tcapi.DeliveryServicesResponse{
resp := tc.DeliveryServicesResponse{
Response: dses,
}
return &resp, nil
}

func getDeliveryServices(v url.Values, db *sqlx.DB) ([]tcapi.DeliveryService, error) {
func getDeliveryServices(v url.Values, db *sqlx.DB) ([]tc.DeliveryService, error) {
var rows *sqlx.Rows
var err error

Expand All @@ -100,9 +99,9 @@ func getDeliveryServices(v url.Values, db *sqlx.DB) ([]tcapi.DeliveryService, er
}
defer rows.Close()

dses := []tcapi.DeliveryService{}
dses := []tc.DeliveryService{}
for rows.Next() {
var s tcapi.DeliveryService
var s tc.DeliveryService
if err = rows.StructScan(&s); err != nil {
return nil, fmt.Errorf("getting Delivery Services: %v", err)
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ import (
// TestValidateErrors ...
func TestValidateErrors(t *testing.T) {

ds := GetRefType()
ds := GetRefTypeNullable()
if err := json.Unmarshal([]byte(errorTestCase()), &ds); err != nil {
fmt.Printf("err ---> %v\n", err)
return
Expand Down

0 comments on commit 7f17c9a

Please sign in to comment.