Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Initial WRP Validation Framework #80

Merged
merged 26 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dfeee98
Add VSCode to .gitignore
denopink May 24, 2022
7f8bad0
Initial WRP Validation Framework
denopink May 25, 2022
9319c12
formatting
denopink May 25, 2022
209ffdb
formatting
denopink May 25, 2022
ddd0acf
convert alwaysInvalidMsg to literal func
denopink May 25, 2022
a2a4093
Formating and exported alwaysInvalidMsg
denopink May 25, 2022
f5a2e90
Decouple error from api
denopink May 25, 2022
eb5bf9b
Updates based on PR review
denopink May 26, 2022
afa08cb
removed duplicated test
denopink May 26, 2022
8cb0fa7
Fix type test name
denopink May 26, 2022
10afaa2
Fix type test name
denopink May 26, 2022
1923ec7
Add missing test/edge cases for Validators
denopink May 26, 2022
98c4b4a
Updated defaultValidator to work as a variadic for multi-default supp…
denopink May 26, 2022
3197568
rename test var `name` to `description`
denopink May 26, 2022
1ee6c2e
Merge branch 'denopink/FR-WRPValidationFramework' of https://github.c…
denopink May 26, 2022
793d6f4
Squash: examples, docs, multierr updates
denopink May 26, 2022
22ca097
formatting
denopink Jun 3, 2022
5ff49e3
Add missing test for `AlwaysValid` func
denopink Jun 3, 2022
1f1aa67
Updates based on PR feedback
denopink Jun 7, 2022
df7e2ff
Update based on PR review
denopink Jun 8, 2022
ffd531f
Move exported fields to the top
denopink Jun 8, 2022
13a9a57
update tests to include correct mac length
denopink Jun 8, 2022
3ac1d02
`testAlwaysValid` & `testAlwaysInvalid` ensure a non-existing message…
denopink Jun 8, 2022
e553e7e
`testTypeValidatorFactory` swap `assert.Zero/NotZero`
denopink Jun 8, 2022
8e6882b
Add missing scheme to source fields & add missing `assert.ErrorIs` to…
denopink Jun 8, 2022
fc099bf
Update based on PR feedback
denopink Jun 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
denopink marked this conversation as resolved.
Show resolved Hide resolved

// 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 {
denopink marked this conversation as resolved.
Show resolved Hide resolved
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