Move examples to examples dir, now LoadJsonFile() also returns err
* Move examples from cmd to examples dir * Update Makefile * LoadJsonFile() dies in-place on read or parse error which is not correct * LoadJsonFile() wastes Instance map, now it returns its own map and error if any
This commit is contained in:
31
examples/01-LoadJsonFile/main.go
Normal file
31
examples/01-LoadJsonFile/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"doll/internal/doll"
|
||||
"doll/internal/log"
|
||||
"os"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Init log subsystem.
|
||||
log.Init("info", os.Stderr)
|
||||
|
||||
file := "example_data/01-config.json"
|
||||
|
||||
// Load the Lua script file.
|
||||
log.Infof("Loading %s", file)
|
||||
|
||||
data, err := doll.LoadJsonFile(file)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to unmarshal %s to map: %s", file, err)
|
||||
}
|
||||
|
||||
log.Infof("Map unmarshalled from %s", file)
|
||||
|
||||
spew.Dump(data)
|
||||
}
|
||||
|
||||
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|
||||
37
examples/02-TestLuaScript/main.go
Normal file
37
examples/02-TestLuaScript/main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"doll/internal/log"
|
||||
"os"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Init log subsystem.
|
||||
log.Init("info", os.Stderr)
|
||||
|
||||
file := "example_data/02-test.lua"
|
||||
|
||||
// Load the Lua script file.
|
||||
log.Infof("Loading %s", file)
|
||||
|
||||
// We can execute script directly via L.DoFile("./example_data/02-test.lua") but in practice we'll get scripts as
|
||||
// strings - snippets from templates - so, testing/checking how it works is not use for project.
|
||||
luaCode, err := os.ReadFile(file)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error reading Lua file %s: %s", file, err)
|
||||
}
|
||||
|
||||
log.Info("Creating new Lua state")
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
log.Info("Evaluating Lua code")
|
||||
if err := L.DoString(string(luaCode)); err != nil {
|
||||
log.Fatalf("Error evaluating Lua code: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|
||||
Reference in New Issue
Block a user