From 68b593afcaa24a9b1d28589306b43156bf588af5 Mon Sep 17 00:00:00 2001 From: Sho Hirose Date: Thu, 20 May 2021 22:54:21 +0900 Subject: [PATCH] edit package name and add filter function --- client/client.go | 7 +++---- client/handler.go | 13 +++++++------ example/handler/test.go | 8 ++++++-- example/main.go | 10 +++++----- go.mod | 2 +- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/client/client.go b/client/client.go index 18554dd..3e5ab3b 100644 --- a/client/client.go +++ b/client/client.go @@ -12,9 +12,9 @@ import ( // Client julius client structure type Client struct { - conn net.Conn + conn net.Conn connClose func() - channel chan string + channel chan string } // NewClient create new Client @@ -57,8 +57,7 @@ func (c *Client) handle(ctx context.Context) { log.Println(err) } for _, handler := range RegisteredHandlers { - fmt.Println(result.Details[0].Word, handler.CallMessage) - if result.Details[0].Word == handler.CallMessage { + if handler.Filter(ctx, result) { if err := handler.Do(ctx, result); err != nil { log.Println(err) } diff --git a/client/handler.go b/client/handler.go index 9eb9493..56662d1 100644 --- a/client/handler.go +++ b/client/handler.go @@ -6,20 +6,21 @@ import ( // Handler structure for handling type Handler struct { - CallMessage string // CallMessage call message - Do CallFunc // Do called function + Filter Filter // ShouldExec Returns whether the handler should be executed or not. + Do CallFunc // Do called function } - type CallFunc func(ctx context.Context, result *Result) error +type Filter func(ctx context.Context, result *Result) bool + // RegisteredHandlers Array containing all Handlers var RegisteredHandlers []Handler // RegisterHandler Called at initialization -func RegisterHandler(msg string, do CallFunc) { +func RegisterHandler(filter Filter, do CallFunc) { RegisteredHandlers = append(RegisteredHandlers, Handler{ - CallMessage: msg, - Do: do, + Filter: filter, + Do: do, }) } diff --git a/example/handler/test.go b/example/handler/test.go index 18af37d..6627704 100644 --- a/example/handler/test.go +++ b/example/handler/test.go @@ -3,15 +3,19 @@ package handler import ( "context" "fmt" - "sho0126hiro/julius-go-client/client" + "github.com/sho0126hiro/julius-go-client/client" ) func init() { - client.RegisterHandler(testMessage, test) + client.RegisterHandler(filter, test) } const testMessage = "Some Message (language: ja only(maybe))" +func filter(_ context.Context, result *client.Result) bool { + return result.Details[0].Word == testMessage +} + func test(_ context.Context, result *client.Result) error { fmt.Println("Some Message") fmt.Println("result: ", result) diff --git a/example/main.go b/example/main.go index e78a74d..c724fb8 100644 --- a/example/main.go +++ b/example/main.go @@ -1,17 +1,17 @@ package main import ( - "sho0126hiro/julius-go-client/client" - _ "sho0126hiro/julius-go-client/example/handler" + "github.com/sho0126hiro/julius-go-client/client" + _ "github.com/sho0126hiro/julius-go-client/example/handler" ) -func main(){ +func main() { network := "tcp" ip := "localhost" port := "10500" c, err := client.NewClient(network, ip+":"+port) if err != nil { - panic (err) + panic(err) } c.Run() -} \ No newline at end of file +} diff --git a/go.mod b/go.mod index 23a666d..0549ad4 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module sho0126hiro/julius-go-client +module github.com/sho0126hiro/julius-go-client go 1.15