| 1 |
package syntax |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
type Validator interface { |
| 12 |
ValidForTLS() error |
| 13 |
} |
| 14 |
|
| 15 |
var ( |
| 16 |
validatorType = reflect.TypeOf(new(Validator)).Elem() |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
type fieldOptions struct { |
| 22 |
omitHeader bool |
| 23 |
varintHeader bool |
| 24 |
headerSize int |
| 25 |
minSize int |
| 26 |
maxSize int |
| 27 |
|
| 28 |
varint bool |
| 29 |
optional bool |
| 30 |
omit bool |
| 31 |
} |
| 32 |
|
| 33 |
func mutuallyExclusive(vals []bool) bool { |
| 34 |
set := 0 |
| 35 |
for _, val := range vals { |
| 36 |
if val { |
| 37 |
set += 1 |
| 38 |
} |
| 39 |
} |
| 40 |
return set <= 1 |
| 41 |
} |
| 42 |
|
| 43 |
func (opts fieldOptions) Consistent() bool { |
| 44 |
|
| 45 |
headerPaths := []bool{opts.omitHeader, opts.varintHeader, opts.headerSize > 1} |
| 46 |
if !mutuallyExclusive(headerPaths) { |
| 47 |
return false |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
if opts.maxSize > 0 && opts.minSize > opts.maxSize { |
| 52 |
return false |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
headerOpts := (opts.omitHeader || opts.varintHeader || opts.headerSize > 1 || opts.maxSize > 0 || opts.minSize > 0) |
| 57 |
encodePaths := []bool{headerOpts, opts.varint, opts.optional} |
| 58 |
if !mutuallyExclusive(encodePaths) { |
| 59 |
return false |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
otherThanOmit := (headerOpts || opts.varint || opts.optional) |
| 64 |
if !mutuallyExclusive([]bool{opts.omit, otherThanOmit}) { |
| 65 |
return false |
| 66 |
} |
| 67 |
|
| 68 |
return true |
| 69 |
} |
| 70 |
|
| 71 |
func (opts fieldOptions) ValidForType(t reflect.Type) bool { |
| 72 |
headerType := t.Kind() == reflect.Slice || t.Kind() == reflect.Map |
| 73 |
headerTags := opts.omitHeader || opts.varintHeader || (opts.headerSize != 0) || |
| 74 |
(opts.minSize != 0) || (opts.maxSize != 0) |
| 75 |
if headerTags && !headerType { |
| 76 |
return false |
| 77 |
} |
| 78 |
|
| 79 |
uintRequired := opts.varint |
| 80 |
if uintRequired { |
| 81 |
switch t.Kind() { |
| 82 |
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 83 |
default: |
| 84 |
return false |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
ptrRequired := opts.optional |
| 89 |
if ptrRequired && t.Kind() != reflect.Ptr { |
| 90 |
return false |
| 91 |
} |
| 92 |
|
| 93 |
return true |
| 94 |
} |
| 95 |
|
| 96 |
var ( |
| 97 |
varintOption = "varint" |
| 98 |
optionalOption = "optional" |
| 99 |
omitOption = "omit" |
| 100 |
|
| 101 |
headOptionNone = "none" |
| 102 |
headOptionVarint = "varint" |
| 103 |
headValueNoHead = uint(255) |
| 104 |
headValueVarint = uint(254) |
| 105 |
|
| 106 |
optionalFlagAbsent uint8 = 0 |
| 107 |
optionalFlagPresent uint8 = 1 |
| 108 |
) |
| 109 |
|
| 110 |
func atoi(a string) int { |
| 111 |
i, err := strconv.Atoi(a) |
| 112 |
if err != nil { |
| 113 |
panic(fmt.Errorf("Invalid header size: %v", err)) |
| 114 |
} |
| 115 |
return i |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func parseTag(tag string) fieldOptions { |
| 122 |
opts := fieldOptions{} |
| 123 |
for _, token := range strings.Split(tag, ",") { |
| 124 |
parts := strings.Split(token, "=") |
| 125 |
|
| 126 |
|
| 127 |
if len(parts) == 1 { |
| 128 |
switch parts[0] { |
| 129 |
case varintOption: |
| 130 |
opts.varint = true |
| 131 |
case optionalOption: |
| 132 |
opts.optional = true |
| 133 |
case omitOption: |
| 134 |
opts.omit = true |
| 135 |
default: |
| 136 |
|
| 137 |
} |
| 138 |
continue |
| 139 |
} |
| 140 |
|
| 141 |
if len(parts) != 2 || len(parts[0]) == 0 || len(parts[1]) == 0 { |
| 142 |
panic(fmt.Errorf("Malformed tag")) |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
switch parts[0] { |
| 147 |
case "head": |
| 148 |
switch { |
| 149 |
case parts[1] == headOptionNone: |
| 150 |
opts.omitHeader = true |
| 151 |
case parts[1] == headOptionVarint: |
| 152 |
opts.varintHeader = true |
| 153 |
default: |
| 154 |
opts.headerSize = atoi(parts[1]) |
| 155 |
} |
| 156 |
|
| 157 |
case "min": |
| 158 |
opts.minSize = atoi(parts[1]) |
| 159 |
|
| 160 |
case "max": |
| 161 |
opts.maxSize = atoi(parts[1]) |
| 162 |
|
| 163 |
default: |
| 164 |
|
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if !opts.Consistent() { |
| 169 |
panic(fmt.Errorf("Inconsistent options")) |
| 170 |
} |
| 171 |
|
| 172 |
return opts |
| 173 |
} |