Skip to content

Commit

Permalink
Merge pull request #85 from denopink/denopink/feature/SimpleMsgTypesV…
Browse files Browse the repository at this point in the history
…alidators

FR: Add simple message types validators for the WRP messages
  • Loading branch information
denopink authored Jun 27, 2022
2 parents 3719b16 + f0c26e2 commit dcfd4b0
Show file tree
Hide file tree
Showing 8 changed files with 1,046 additions and 130 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Add simple message types WRP validators [#85](https://github.com/xmidt-org/wrp-go/pull/85)
- Add basic WRP spec validators [#84](https://github.com/xmidt-org/wrp-go/pull/84)
- Introduce WRP Validation Framework [#80](https://github.com/xmidt-org/wrp-go/pull/80)
- Fix unmarshalling error due to missig metadata fields [#79](https://github.com/xmidt-org/wrp-go/pull/79)
- Deprecated the concrete message structs, e.g. SimpleEvent
- Added support for the new qos field.

Expand Down
4 changes: 3 additions & 1 deletion messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package wrp

import "regexp"
import (
"regexp"
)

//go:generate go install github.com/ugorji/go/codec/codecgen@latest
//go:generate codecgen -st "json" -o messages_codec.go messages.go
Expand Down
111 changes: 111 additions & 0 deletions simpleMessageTypes_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* 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"
"fmt"
"strconv"

"go.uber.org/multierr"
)

var (
ErrorNotSimpleResponseRequestType = NewValidatorError(errors.New("not simple response request message type"), "", []string{"Type"})
ErrorNotSimpleEventType = NewValidatorError(errors.New("not simple event message type"), "", []string{"Type"})
ErrorInvalidSpanLength = NewValidatorError(errors.New("invalid span length"), "", []string{"Spans"})
ErrorInvalidSpanFormat = NewValidatorError(errors.New("invalid span format"), "", []string{"Spans"})
)

// spanFormat is a simple map of allowed span format.
var spanFormat = map[int]string{
// parent is the root parent for the spans below to link to
0: "parent",
// name is the name of the operation
1: "name",
// start time of the operation.
2: "start time",
// duration is how long the operation took.
3: "duration",
// status of the operation
4: "status",
}

// SimpleEventValidators ensures messages are valid based on
// each validator in the list. SimpleEventValidators validates the following:
// UTF8 (all string fields), MessageType is valid, Source, Destination, MessageType is of SimpleEventMessageType.
func SimpleEventValidators() Validators {
return Validators{SpecValidators()}.AddFunc(SimpleEventTypeValidator)
}

// SimpleResponseRequestValidators ensures messages are valid based on
// each validator in the list. SimpleResponseRequestValidators validates the following:
// UTF8 (all string fields), MessageType is valid, Source, Destination, Spans, MessageType is of
// SimpleRequestResponseMessageType.
func SimpleResponseRequestValidators() Validators {
return Validators{SpecValidators()}.AddFunc(SimpleResponseRequestTypeValidator, SpansValidator)
}

// SimpleResponseRequestTypeValidator takes messages and validates their Type is of SimpleRequestResponseMessageType.
func SimpleResponseRequestTypeValidator(m Message) error {
if m.Type != SimpleRequestResponseMessageType {
return ErrorNotSimpleResponseRequestType
}

return nil
}

// TODO Do we want to include SpanParentValidator? SpanParent currently doesn't exist in the Message Struct

// SpansValidator takes messages and validates their Spans.
func SpansValidator(m Message) error {
var err error
// Spans consist of individual Span(s), arrays of timing values.
for _, s := range m.Spans {
if len(s) != len(spanFormat) {
err = multierr.Append(err, ErrorInvalidSpanLength)
continue
}

for i, j := range spanFormat {
switch j {
// Any nonempty string is valid
case "parent", "name":
if len(s[i]) == 0 {
err = multierr.Append(err, fmt.Errorf("%w %v: invalid %v component '%v'", ErrorInvalidSpanFormat, s, j, s[i]))
}
// Must be an integer
case "start time", "duration", "status":
if _, atoiErr := strconv.Atoi(s[i]); atoiErr != nil {
err = multierr.Append(err, fmt.Errorf("%w %v: invalid %v component '%v': %v", ErrorInvalidSpanFormat, s, j, s[i], atoiErr))
}
}
}
}

return err
}

// SimpleEventTypeValidator takes messages and validates their Type is of SimpleEventMessageType.
func SimpleEventTypeValidator(m Message) error {
if m.Type != SimpleEventMessageType {
return ErrorNotSimpleEventType
}

return nil
}
Loading

0 comments on commit dcfd4b0

Please sign in to comment.