Skip to content

Commit

Permalink
Merge fc099bf into 840a19e
Browse files Browse the repository at this point in the history
  • Loading branch information
denopink authored Jun 8, 2022
2 parents 840a19e + fc099bf commit 0218fb9
Show file tree
Hide file tree
Showing 6 changed files with 547 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ report.json

# Images
*.png

# VSCode
*.code-workspace
.vscode/*
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ require (
github.com/ugorji/go/codec v1.2.6
github.com/xmidt-org/httpaux v0.3.0
github.com/xmidt-org/webpa-common v1.3.2
go.uber.org/multierr v1.8.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ github.com/xmidt-org/httpaux v0.3.0 h1:JdV4QceiE8EMA1Qf5rnJzHdkIPzQV12ddARHLU8hr
github.com/xmidt-org/httpaux v0.3.0/go.mod h1:mviIlg5fHGb3lAv3l0sbiwVG/q9rqvXaudEYxVrzXdE=
github.com/xmidt-org/webpa-common v1.3.2 h1:dE1Fi+XVnkt3tMGMjH7/hN/UGcaQ/ukKriXuMDyCWnM=
github.com/xmidt-org/webpa-common v1.3.2/go.mod h1:oCpKzOC+9h2vYHVzAU/06tDTQuBN4RZz+rhgIXptpOI=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
6 changes: 0 additions & 6 deletions header_wrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

package wrp

import (
"errors"
)

// Constant HTTP header strings representing WRP fields
const (
MsgTypeHeader = "X-Midt-Msg-Type"
Expand All @@ -34,8 +30,6 @@ const (
SourceHeader = "X-Midt-Source"
)

var ErrInvalidMsgType = errors.New("Invalid Message Type")

// Map string to MessageType int
/*
func StringToMessageType(str string) MessageType {
Expand Down
101 changes: 101 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2022 Comcast Cable Communications Management, LLC
*
* 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.
*
*/

package wrp

import (
"errors"

"go.uber.org/multierr"
)

var (
ErrInvalidTypeValidator = errors.New("invalid TypeValidator")
ErrInvalidValidator = errors.New("invalid WRP message type validator")
ErrInvalidMsgType = errors.New("invalid WRP message type")
)

// AlwaysInvalid doesn't validate anything about the message and always returns an error.
var AlwaysInvalid ValidatorFunc = func(m Message) error { return ErrInvalidMsgType }

// AlwaysValid doesn't validate anything about the message and always returns nil.
var AlwaysValid ValidatorFunc = func(msg Message) error { return nil }

// Validator is a WRP validator that allows access to the Validate function.
type Validator interface {
Validate(m Message) error
}

// Validators is a WRP validator that ensures messages are valid based on
// message type and each validator in the list.
type Validators []Validator

// Validate runs messages through each validator in the validators list.
// It returns as soon as the message is considered invalid, otherwise returns nil if valid.
func (vs Validators) Validate(m Message) error {
var err error
for _, v := range vs {
if v != nil {
err = multierr.Append(err, v.Validate(m))
}
}

return err
}

// ValidatorFunc is a WRP validator that takes messages and validates them
// against functions.
type ValidatorFunc func(Message) error

// Validate executes its own ValidatorFunc receiver and returns the result.
func (vf ValidatorFunc) Validate(m Message) error {
return vf(m)
}

// TypeValidator is a WRP validator that validates based on message type
// or using the defaultValidator if message type is unfound.
type TypeValidator struct {
m map[MessageType]Validator
defaultValidator Validator
}

// Validate validates messages based on message type or using the defaultValidator
// if message type is unfound.
func (m TypeValidator) Validate(msg Message) error {
vs := m.m[msg.MessageType()]
if vs == nil {
return m.defaultValidator.Validate(msg)
}

return vs.Validate(msg)
}

// NewTypeValidator is a TypeValidator factory.
func NewTypeValidator(m map[MessageType]Validator, defaultValidator Validator) (TypeValidator, error) {
if m == nil {
return TypeValidator{}, ErrInvalidValidator
}

if defaultValidator == nil {
defaultValidator = AlwaysInvalid
}

return TypeValidator{
m: m,
defaultValidator: defaultValidator,
}, nil
}
Loading

0 comments on commit 0218fb9

Please sign in to comment.