Skip to content
This repository has been archived by the owner on Mar 19, 2023. It is now read-only.

Commit

Permalink
edit package name and add filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
sho0126hiro committed May 20, 2021
1 parent 7ace1e2 commit 68b593a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
7 changes: 3 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 7 additions & 6 deletions client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
8 changes: 6 additions & 2 deletions example/handler/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module sho0126hiro/julius-go-client
module github.com/sho0126hiro/julius-go-client

go 1.15

0 comments on commit 68b593a

Please sign in to comment.