entriesInPossibleKeyOrder reorders es by VariantKey, in the order they should appear in the index section; the row-major order of possible keys for Variants. All entries in es must have the same Variants value. For example, if the Variants value is "Accept-Language;en;fr, Accept-Encoding:gzip;br",
(es []*indexEntry)
| 229 | // If entries in es do not cover all combination of possible keys or two entries |
| 230 | // have overwrapping possible keys, this returns an error. |
| 231 | func entriesInPossibleKeyOrder(es []*indexEntry) ([]*indexEntry, error) { |
| 232 | if es[0].Variants == "" { |
| 233 | return nil, errors.New("no Variants header") |
| 234 | } |
| 235 | variants, err := parseVariants(es[0].Variants) |
| 236 | if err != nil { |
| 237 | return nil, fmt.Errorf("cannot parse Variants header value %q: %v", es[0].Variants, err) |
| 238 | } |
| 239 | numPossibleKeys, err := variants.numberOfPossibleKeys() |
| 240 | if err != nil { |
| 241 | return nil, fmt.Errorf("invalid Variants header value %q: %v", es[0].Variants, err) |
| 242 | } |
| 243 | |
| 244 | result := make([]*indexEntry, numPossibleKeys) |
| 245 | for _, e := range es { |
| 246 | // TODO: Compare Variants values as lists |
| 247 | // (e.g. "Accept;foo;bar" == "Accept; foo; bar"). |
| 248 | if e.Variants != es[0].Variants { |
| 249 | return nil, fmt.Errorf("inconsistent Variants value. %q != %q", e.Variants, es[0].Variants) |
| 250 | } |
| 251 | vks, err := parseListOfStringLists(e.VariantKey) |
| 252 | if err != nil { |
| 253 | return nil, fmt.Errorf("cannot parse Variant-Key header %q: %v", e.VariantKey, err) |
| 254 | } |
| 255 | for _, vk := range vks { |
| 256 | i := variants.indexInPossibleKeys(vk) |
| 257 | if i == -1 { |
| 258 | return nil, fmt.Errorf("Variant-Key %q is not covered by variants %q", e.VariantKey, e.Variants) |
| 259 | } |
| 260 | if result[i] != nil { |
| 261 | return nil, fmt.Errorf("duplicated entries with Variant-Key %q", vk) |
| 262 | } |
| 263 | result[i] = e |
| 264 | } |
| 265 | } |
| 266 | for i, e := range result { |
| 267 | if e == nil { |
| 268 | return nil, fmt.Errorf("no entry for Variant-Key %v", variants.possibleKeyAt(i)) |
| 269 | } |
| 270 | } |
| 271 | return result, nil |
| 272 | } |
| 273 | |
| 274 | func parseListOfStringLists(s string) ([][]string, error) { |
| 275 | ll, err := structuredheader.ParseListOfLists(s) |
no test coverage detected