github.com/apex/up@v1.7.1/internal/header/header.go (about) 1 // Package header provides path-matched header injection rules. 2 package header 3 4 import ( 5 "github.com/fanyang01/radix" 6 ) 7 8 // Fields map. 9 type Fields map[string]string 10 11 // Rules map of paths to fields. 12 type Rules map[string]Fields 13 14 // Matcher for header lookup. 15 type Matcher struct { 16 t *radix.PatternTrie 17 } 18 19 // Lookup returns fields for the given path. 20 func (m *Matcher) Lookup(path string) Fields { 21 v, ok := m.t.Lookup(path) 22 if !ok { 23 return nil 24 } 25 26 return v.(Fields) 27 } 28 29 // Compile the given rules to a trie. 30 func Compile(rules Rules) (*Matcher, error) { 31 t := radix.NewPatternTrie() 32 m := &Matcher{t} 33 34 for path, fields := range rules { 35 t.Add(path, fields) 36 } 37 38 return m, nil 39 } 40 41 // Merge returns a new rules set giving precedence to `b`. 42 func Merge(a, b Rules) Rules { 43 r := make(Rules) 44 45 for path, fields := range a { 46 r[path] = fields 47 } 48 49 for path, fields := range b { 50 if _, ok := r[path]; !ok { 51 r[path] = make(Fields) 52 } 53 54 for name, val := range fields { 55 r[path][name] = val 56 } 57 } 58 59 return r 60 }