File: match.go

package info (click to toggle)
golang-goji 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 990 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package pat

import (
	"context"
	"sort"

	"goji.io/internal"
	"goji.io/pattern"
)

type match struct {
	context.Context
	pat     *Pattern
	matches []string
}

func (m match) Value(key interface{}) interface{} {
	switch key {
	case pattern.AllVariables:
		var vs map[pattern.Variable]interface{}
		if vsi := m.Context.Value(key); vsi == nil {
			if len(m.pat.pats) == 0 {
				return nil
			}
			vs = make(map[pattern.Variable]interface{}, len(m.matches))
		} else {
			vs = vsi.(map[pattern.Variable]interface{})
		}

		for _, p := range m.pat.pats {
			vs[p.name] = m.matches[p.idx]
		}
		return vs
	case internal.Path:
		if len(m.matches) == len(m.pat.pats)+1 {
			return m.matches[len(m.matches)-1]
		}
		return ""
	}

	if k, ok := key.(pattern.Variable); ok {
		i := sort.Search(len(m.pat.pats), func(i int) bool {
			return m.pat.pats[i].name >= k
		})
		if i < len(m.pat.pats) && m.pat.pats[i].name == k {
			return m.matches[m.pat.pats[i].idx]
		}
	}

	return m.Context.Value(key)
}