summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rpc.go20
-rw-r--r--rpc_test.go8
2 files changed, 20 insertions, 8 deletions
diff --git a/rpc.go b/rpc.go
index 546367d..2b447ee 100644
--- a/rpc.go
+++ b/rpc.go
@@ -1,6 +1,9 @@
package tanja
-import "reflect"
+import (
+ "reflect"
+ "strings"
+)
// Registers a pattern for each of the methods of 'obj'. The method will be
// called from ses.Dispatch() (and thus ses.Run() if you use that) whenever a
@@ -156,3 +159,18 @@ func regRPCGenCallback(mt reflect.Type, mv reflect.Value, offset int, willReply
}
}
}
+
+// Handy wrapper for RegRPC(), where each method-to-be-registered has a common
+// prefix in the name, and where the pattern consists of another prefix + the
+// rest of the method name. (The source code is easier to understand than this
+// explanation, really)
+func (s *Session) RegPrefix(obj interface{}, pat Tuple, prefix string, tolower bool) []*Registration {
+ return s.RegRPC(obj, func(n string) Tuple {
+ if !strings.HasPrefix(n, prefix) {
+ return nil
+ } else if tolower {
+ return append(pat, Element{strings.ToLower(n[len(prefix):])})
+ }
+ return append(pat, Element{n[len(prefix):]})
+ })
+}
diff --git a/rpc_test.go b/rpc_test.go
index 75347fd..16ef126 100644
--- a/rpc_test.go
+++ b/rpc_test.go
@@ -2,7 +2,6 @@ package tanja
import (
"reflect"
- "strings"
"testing"
)
@@ -88,12 +87,7 @@ func TestRPC(tst *testing.T) {
ses := node.Session()
obj := &sT{tst: tst, ses: ses}
- lst := ses.RegRPC(obj, func(s string) Tuple {
- if strings.HasPrefix(s, "Export") {
- return Tup("prefix", 1, strings.ToLower(s[6:]))
- }
- return nil
- })
+ lst := ses.RegPrefix(obj, Tup("prefix", 1), "Export", true)
if len(lst) != 4 {
tst.Fatalf("Number of exported methods is %d, expected %d", len(lst), 4)
}