forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignalSchema.go
96 lines (83 loc) · 2.69 KB
/
signalSchema.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package plugin
import (
"encoding/json"
"regexp"
"strings"
)
// SignalSchema represents a given signal/signal group's schema
type SignalSchema struct {
signalSchema
regex *regexp.Regexp
}
// Name returns the signal/signal group's name
func (s *SignalSchema) Name() string {
return s.signalSchema.Name
}
// SetName sets the signal/signal group's name. This should only
// be called by the tests.
func (s *SignalSchema) SetName(name string) *SignalSchema {
s.signalSchema.Name = name
return s
}
// Description returns the signal/signal group's name
func (s *SignalSchema) Description() string {
return s.signalSchema.Description
}
// SetDescription sets the signal/signal group's description. This should
// only be called by the tests.
func (s *SignalSchema) SetDescription(description string) *SignalSchema {
s.signalSchema.Description = description
return s
}
// Regex returns a regex describing an arbitrary signal in the signal
// group.
func (s *SignalSchema) Regex() *regexp.Regexp {
return s.regex
}
// SetRegex sets the signal group's regex. This should only be called by the
// tests.
func (s *SignalSchema) SetRegex(regex *regexp.Regexp) *SignalSchema {
s.regex = regex
s.signalSchema.Regex = regex.String()
return s
}
// IsGroup returns true if s is a signal group's schema. This is
// true if s.Regex() != nil
func (s *SignalSchema) IsGroup() bool {
return s.Regex() != nil
}
// MarshalJSON marshals the signal schema to JSON. It takes
// a value receiver so that the entry schema's still marshalled
// when it's referenced as an interface{} object. See
// https://stackoverflow.com/a/21394657 for more details.
func (s SignalSchema) MarshalJSON() ([]byte, error) {
return json.Marshal(s.signalSchema)
}
// UnmarshalJSON unmarshals the signal schema JSON.
func (s *SignalSchema) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, &s.signalSchema)
if err != nil {
return err
}
return s.normalize()
}
// normalizes s by downcasing its name, making its regex case-insensitive,
// and compiling the latter. Returns an error if the regex fails compilation.
func (s *SignalSchema) normalize() error {
s.signalSchema.Name = strings.ToLower(s.signalSchema.Name)
var err error
if len(s.signalSchema.Regex) > 0 {
// (?i) tells Go that the regex is case-insensitive
s.signalSchema.Regex = "(?i)" + s.signalSchema.Regex
s.regex, err = regexp.Compile(s.signalSchema.Regex)
}
return err
}
// This is to implement JSON Marshal/Unmarshal. The main reason
// for implementing the Marshaler/Unmarshaler interfaces is to
// validate the marshalled Regex.
type signalSchema struct {
Name string `json:"name"`
Description string `json:"description"`
Regex string `json:"regex,omitempty"`
}