diff options
author | Yorhel <git@yorhel.nl> | 2012-04-17 16:24:58 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2012-04-17 16:24:58 +0200 |
commit | 4e2ec08a97edc08be9214c7f0063f0c44f46400a (patch) | |
tree | cf669740b14cfa1284457e8107cfbf25dec6a85a | |
parent | 7b6252e2fdec1d48a21f2ea4db6ca547424d5b48 (diff) |
-rw-r--r-- | rpc.go | 20 | ||||
-rw-r--r-- | rpc_test.go | 8 |
2 files changed, 20 insertions, 8 deletions
@@ -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) } |