gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

match.go (2813B)


      1 package dbus
      2 
      3 import (
      4 	"strconv"
      5 	"strings"
      6 )
      7 
      8 // MatchOption specifies option for dbus routing match rule. Options can be constructed with WithMatch* helpers.
      9 // For full list of available options consult
     10 // https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
     11 type MatchOption struct {
     12 	key   string
     13 	value string
     14 }
     15 
     16 func formatMatchOptions(options []MatchOption) string {
     17 	items := make([]string, 0, len(options))
     18 	for _, option := range options {
     19 		items = append(items, option.key+"='"+option.value+"'")
     20 	}
     21 	return strings.Join(items, ",")
     22 }
     23 
     24 // WithMatchOption creates match option with given key and value
     25 func WithMatchOption(key, value string) MatchOption {
     26 	return MatchOption{key, value}
     27 }
     28 
     29 // doesn't make sense to export this option because clients can only
     30 // subscribe to messages with signal type.
     31 func withMatchType(typ string) MatchOption {
     32 	return WithMatchOption("type", typ)
     33 }
     34 
     35 // WithMatchSender sets sender match option.
     36 func WithMatchSender(sender string) MatchOption {
     37 	return WithMatchOption("sender", sender)
     38 }
     39 
     40 // WithMatchSender sets interface match option.
     41 func WithMatchInterface(iface string) MatchOption {
     42 	return WithMatchOption("interface", iface)
     43 }
     44 
     45 // WithMatchMember sets member match option.
     46 func WithMatchMember(member string) MatchOption {
     47 	return WithMatchOption("member", member)
     48 }
     49 
     50 // WithMatchObjectPath creates match option that filters events based on given path
     51 func WithMatchObjectPath(path ObjectPath) MatchOption {
     52 	return WithMatchOption("path", string(path))
     53 }
     54 
     55 // WithMatchPathNamespace sets path_namespace match option.
     56 func WithMatchPathNamespace(namespace ObjectPath) MatchOption {
     57 	return WithMatchOption("path_namespace", string(namespace))
     58 }
     59 
     60 // WithMatchDestination sets destination match option.
     61 func WithMatchDestination(destination string) MatchOption {
     62 	return WithMatchOption("destination", destination)
     63 }
     64 
     65 // WithMatchArg sets argN match option, range of N is 0 to 63.
     66 func WithMatchArg(argIdx int, value string) MatchOption {
     67 	if argIdx < 0 || argIdx > 63 {
     68 		panic("range of argument index is 0 to 63")
     69 	}
     70 	return WithMatchOption("arg"+strconv.Itoa(argIdx), value)
     71 }
     72 
     73 // WithMatchArgPath sets argN path match option, range of N is 0 to 63.
     74 func WithMatchArgPath(argIdx int, path string) MatchOption {
     75 	if argIdx < 0 || argIdx > 63 {
     76 		panic("range of argument index is 0 to 63")
     77 	}
     78 	return WithMatchOption("arg"+strconv.Itoa(argIdx)+"path", path)
     79 }
     80 
     81 // WithMatchArg0Namespace sets arg0namespace match option.
     82 func WithMatchArg0Namespace(arg0Namespace string) MatchOption {
     83 	return WithMatchOption("arg0namespace", arg0Namespace)
     84 }
     85 
     86 // WithMatchEavesdrop sets eavesdrop match option.
     87 func WithMatchEavesdrop(eavesdrop bool) MatchOption {
     88 	return WithMatchOption("eavesdrop", strconv.FormatBool(eavesdrop))
     89 }