-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
43 lines (34 loc) · 1.52 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2023 The FIT SDK for Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package proto
import (
"fmt"
"github.com/muktihari/fit/profile/basetype"
)
const ErrProtocolViolation = errorString("protocol violation")
// NewValidator creates protocol validator base on given version.
func NewValidator(version Version) *Validator {
return &Validator{version: version}
}
// Validator is protocol validator
type Validator struct{ version Version }
// ValidateMessageDefinition validates whether the message definition contains unsupported data for the targeted version.
func (p *Validator) ValidateMessageDefinition(mesgDef *MessageDefinition) error {
if p.version == V1 {
if len(mesgDef.DeveloperFieldDefinitions) > 0 {
return fmt.Errorf("protocol version 1.0 do not support developer fields: %w", ErrProtocolViolation)
}
for _, fieldDef := range mesgDef.FieldDefinitions {
if fieldDef.BaseType&basetype.BaseTypeNumMask > basetype.Byte&basetype.BaseTypeNumMask { // byte was the last type added in 1.0
return fmt.Errorf("protocol version 1.0 do not support type '%s': %w", fieldDef.BaseType, ErrProtocolViolation)
}
}
return nil
}
return nil
}
// ProtocolVersion returns the protocol version that this validator validates.
func (p *Validator) ProtocolVersion() Version { return p.version }
// SetProtocolVersion sets new protocol version to validate.
func (p *Validator) SetProtocolVersion(version Version) { p.version = version }