MCPcopy Create free account
hub / github.com/WICG/webpackage / entriesInPossibleKeyOrder

Function entriesInPossibleKeyOrder

go/bundle/encoder.go:231–272  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
231func 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
274func parseListOfStringLists(s string) ([][]string, error) {
275 ll, err := structuredheader.ParseListOfLists(s)

Callers 1

FinalizeMethod · 0.85

Calls 5

parseVariantsFunction · 0.85
parseListOfStringListsFunction · 0.85
numberOfPossibleKeysMethod · 0.80
indexInPossibleKeysMethod · 0.80
possibleKeyAtMethod · 0.80

Tested by

no test coverage detected