Skip to content

Commit

Permalink
Update README, make default Config work
Browse files Browse the repository at this point in the history
  • Loading branch information
kirsle committed Dec 9, 2016
1 parent 7df064e commit 2b91b22
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ package main

import (
"fmt"
rivescript "github.com/aichaos/rivescript-go"
"github.com/aichaos/rivescript-go"
"github.com/aichaos/rivescript-go/config"
)

func main() {
bot := rivescript.New()
bot := rivescript.New(config.Basic())

// Load a directory full of RiveScript documents (.rive files)
err := bot.LoadDirectory("eg/brain")
Expand All @@ -103,6 +104,35 @@ func main() {
}
```

## Configuration

The constructor takes an optional `Config` struct. Here is a full example with
all the supported options. You only need to provide keys that are different to
the defaults.

```go
bot := rs.New(&config.Config{
Debug: false, // Debug mode, off by default
Strict: false, // No strict syntax checking
UTF8: false, // No UTF-8 support enabled by default
Depth: 50, // Becomes default 50 if Depth is <= 0
SessionManager: memory.New(), // Default in-memory session manager
})
```

For convenience, the `config` package provides two config templates:

```go
// Basic has all the defaults, plus Strict=true
bot := rs.New(config.Basic())

// UTF8 has all of Basic's settings, plus UTF8=true
bot := rs.New(config.UTF8())

// You can also provide a nil configuration, which defaults to Basic()
bot := rs.New(nil)
```

## Object Macros

A common feature in many RiveScript implementations is the object macro, which
Expand Down
15 changes: 8 additions & 7 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package rivescript_test

import (
"fmt"
rivescript "github.com/aichaos/rivescript-go"
js "github.com/aichaos/rivescript-go/lang/javascript"

"github.com/aichaos/rivescript-go"
"github.com/aichaos/rivescript-go/config"
"github.com/aichaos/rivescript-go/lang/javascript"
rss "github.com/aichaos/rivescript-go/src"
)

func ExampleRiveScript() {
bot := rivescript.New()
bot := rivescript.New(config.Basic())

// Load a directory full of RiveScript documents (.rive files)
bot.LoadDirectory("eg/brain")
Expand All @@ -27,11 +29,10 @@ func ExampleRiveScript() {
func ExampleRiveScript_javascript() {
// Example for configuring the JavaScript object macro handler via Otto.

bot := rivescript.New()
bot := rivescript.New(config.Basic())

// Create the JS handler.
jsHandler := js.New(bot)
bot.SetHandler("javascript", jsHandler)
bot.SetHandler("javascript", javascript.New(bot))

// Now we can use object macros written in JS!
bot.Stream(`
Expand Down Expand Up @@ -66,7 +67,7 @@ func ExampleRiveScript_subroutine() {
// Example for defining a Go function as an object macro.
// import rss "github.com/aichaos/rivescript-go/src"

bot := rivescript.New()
bot := rivescript.New(config.Basic())

// Define an object macro named `setname`
bot.SetSubroutine("setname", func(rs *rss.RiveScript, args []string) string {
Expand Down
5 changes: 3 additions & 2 deletions lang/javascript/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ package javascript

import (
"fmt"
rivescript "github.com/aichaos/rivescript-go"
"github.com/robertkrimen/otto"
"strings"

"github.com/aichaos/rivescript-go"
"github.com/robertkrimen/otto"
)

type JavaScriptHandler struct {
Expand Down
37 changes: 20 additions & 17 deletions src/rivescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,28 @@ type RiveScript struct {
* Constructor and Debug Methods *
******************************************************************************/

func New(config *config.Config) *RiveScript {
func New(cfg *config.Config) *RiveScript {
rs := new(RiveScript)
if config != nil {
if config.SessionManager == nil {
rs.say("No SessionManager config: using default MemoryStore")
config.SessionManager = memory.New()
}

if config.Depth <= 0 {
rs.say("No depth config: using default 50")
config.Depth = 50
}

rs.Debug = config.Debug
rs.Strict = config.Strict
rs.UTF8 = config.UTF8
rs.Depth = config.Depth
rs.sessions = config.SessionManager
if cfg == nil {
cfg = config.Basic()
}

if cfg.SessionManager == nil {
rs.say("No SessionManager config: using default MemoryStore")
cfg.SessionManager = memory.New()
}

if cfg.Depth <= 0 {
rs.say("No depth config: using default 50")
cfg.Depth = 50
}

rs.Debug = cfg.Debug
rs.Strict = cfg.Strict
rs.UTF8 = cfg.UTF8
rs.Depth = cfg.Depth
rs.sessions = cfg.SessionManager

rs.UnicodePunctuation = regexp.MustCompile(`[.,!?;:]`)

// Initialize helpers.
Expand Down

0 comments on commit 2b91b22

Please sign in to comment.