| 1 |
package types |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"go.yaml.in/yaml/v3" |
| 11 |
) |
| 12 |
|
| 13 |
type StringSlice []string |
| 14 |
|
| 15 |
// String returns the string representation of the URL. |
| 16 |
// |
| 17 |
//goland:noinspection GoMixedReceiverTypes |
| 18 |
func (s StringSlice) String() string { |
| 19 |
return strings.Join(s, ",") |
| 20 |
} |
| 21 |
|
| 22 |
// MarshalText implements [encoding.TextMarshaler] interface. |
| 23 |
// |
| 24 |
//goland:noinspection GoMixedReceiverTypes |
| 25 |
func (s StringSlice) MarshalText() ([]byte, error) { |
| 26 |
return []byte(s.String()), nil |
| 27 |
} |
| 28 |
|
| 29 |
// UnmarshalText implements the [encoding.TextUnmarshaler] interface. |
| 30 |
// |
| 31 |
//goland:noinspection GoMixedReceiverTypes |
| 32 |
func (s *StringSlice) UnmarshalText(text []byte) error { |
| 33 |
*s = strings.Split(string(text), ",") |
| 34 |
|
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
// UnmarshalJSON implements the [json.Unmarshaler] interface. |
| 39 |
// |
| 40 |
//goland:noinspection GoMixedReceiverTypes |
| 41 |
func (s *StringSlice) UnmarshalJSON(jsonBytes []byte) error { |
| 42 |
var stringList []string |
| 43 |
|
| 44 |
err := json.NewDecoder(bytes.NewReader(jsonBytes)).Decode(&stringList) |
| 45 |
if err != nil { |
| 46 |
//nolint:wrapcheck |
| 47 |
return err |
| 48 |
} |
| 49 |
|
| 50 |
*s = stringList |
| 51 |
|
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
// UnmarshalYAML implements the [yaml.Unmarshaler] interface. |
| 56 |
// |
| 57 |
//goland:noinspection GoMixedReceiverTypes |
| 58 |
func (s *StringSlice) UnmarshalYAML(data *yaml.Node) error { |
| 59 |
var stringList []string |
| 60 |
|
| 61 |
err := data.Decode(&stringList) |
| 62 |
if err != nil { |
| 63 |
//nolint:wrapcheck |
| 64 |
return err |
| 65 |
} |
| 66 |
|
| 67 |
*s = stringList |
| 68 |
|
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
type RegexpSlice []*regexp.Regexp |
| 73 |
|
| 74 |
// String returns the string representation of the [RegexpSlice]. |
| 75 |
// |
| 76 |
//goland:noinspection GoMixedReceiverTypes |
| 77 |
func (s RegexpSlice) String() string { |
| 78 |
stringList := make([]string, 0, len(s)) |
| 79 |
for _, r := range s { |
| 80 |
stringList = append(stringList, r.String()) |
| 81 |
} |
| 82 |
|
| 83 |
return strings.Join(stringList, ",") |
| 84 |
} |
| 85 |
|
| 86 |
// MarshalText implements [encoding.TextMarshaler] interface. |
| 87 |
// |
| 88 |
//goland:noinspection GoMixedReceiverTypes |
| 89 |
func (s RegexpSlice) MarshalText() ([]byte, error) { |
| 90 |
return []byte(s.String()), nil |
| 91 |
} |
| 92 |
|
| 93 |
// UnmarshalText implements the [encoding.TextUnmarshaler] interface. |
| 94 |
// |
| 95 |
//goland:noinspection GoMixedReceiverTypes |
| 96 |
func (s *RegexpSlice) UnmarshalText(text []byte) error { |
| 97 |
return s.fromSlice(strings.Split(string(text), ",")) |
| 98 |
} |
| 99 |
|
| 100 |
// UnmarshalJSON implements the [json.Unmarshaler] interface. |
| 101 |
// |
| 102 |
//goland:noinspection GoMixedReceiverTypes |
| 103 |
func (s *RegexpSlice) UnmarshalJSON(jsonBytes []byte) error { |
| 104 |
var stringList []string |
| 105 |
|
| 106 |
err := json.NewDecoder(bytes.NewReader(jsonBytes)).Decode(&stringList) |
| 107 |
if err != nil { |
| 108 |
//nolint:wrapcheck |
| 109 |
return err |
| 110 |
} |
| 111 |
|
| 112 |
return s.fromSlice(stringList) |
| 113 |
} |
| 114 |
|
| 115 |
// UnmarshalYAML implements the [yaml.Unmarshaler] interface. |
| 116 |
// |
| 117 |
//goland:noinspection GoMixedReceiverTypes |
| 118 |
func (s *RegexpSlice) UnmarshalYAML(data *yaml.Node) error { |
| 119 |
var stringList []string |
| 120 |
|
| 121 |
err := data.Decode(&stringList) |
| 122 |
if err != nil { |
| 123 |
//nolint:wrapcheck |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
return s.fromSlice(stringList) |
| 128 |
} |
| 129 |
|
| 130 |
//goland:noinspection GoMixedReceiverTypes |
| 131 |
func (s *RegexpSlice) fromSlice(stringList []string) error { |
| 132 |
regexList := make(RegexpSlice, 0, len(stringList)) |
| 133 |
for _, str := range stringList { |
| 134 |
regexPattern, err := regexp.Compile(fmt.Sprintf("^(?:%s)$", str)) |
| 135 |
if err != nil { |
| 136 |
//nolint:wrapcheck |
| 137 |
return err |
| 138 |
} |
| 139 |
|
| 140 |
regexList = append(regexList, regexPattern) |
| 141 |
} |
| 142 |
|
| 143 |
*s = regexList |
| 144 |
|
| 145 |
return nil |
| 146 |
} |