-
Notifications
You must be signed in to change notification settings - Fork 2
/
regexp.go
35 lines (30 loc) · 731 Bytes
/
regexp.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
package openapi
import (
"encoding/json"
"regexp"
)
// Regexp is a wrapper around *regexp.Regexp to allow for marshinaling/unmarshaling
type Regexp struct {
*regexp.Regexp
}
// MarshalJSON unmarshals data into sr
func (sr Regexp) MarshalJSON() ([]byte, error) {
if sr.IsNil() {
return []byte{}, nil
}
return json.Marshal(sr.Regexp.String())
}
// UnmarshalJSON unmarshals data into sr
func (sr *Regexp) UnmarshalJSON(data []byte) error {
var expr string
var err error
if err = json.Unmarshal(data, &expr); err != nil {
return err
}
sr.Regexp, err = regexp.Compile(expr)
return err
}
// IsNil returns true if either sr or sr.Regexp is nil
func (sr *Regexp) IsNil() bool {
return sr == nil || sr.Regexp == nil
}