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:
eleksir
2025-10-27 20:28:27 +03:00
parent 521ac9abc0
commit f2948d705a
9 changed files with 102 additions and 86 deletions

View File

@@ -7,6 +7,8 @@ BINARY=${MYNAME}
UNIX_BINARY=${MYNAME} UNIX_BINARY=${MYNAME}
WINDOWS_BINARY=${MYNAME}.exe WINDOWS_BINARY=${MYNAME}.exe
RMCMD=rm -rf RMCMD=rm -rf
EXAMPLE01=01-LoadJsonFile
EXAMPLE02=02-TestLuaScript
# На windows имя бинарника может зависеть не только от платформы, но и от выбранной цели, для linux-а суффикс .exe # На windows имя бинарника может зависеть не только от платформы, но и от выбранной цели, для linux-а суффикс .exe
# не нужен # не нужен
@@ -58,19 +60,19 @@ clean:
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
ifeq ($(SHELL),sh.exe) ifeq ($(SHELL),sh.exe)
# # Vanilla cmd.exe / powershell. # # Vanilla cmd.exe / powershell.
if exist 01-LoadJsonFile.exe ${RMCMD} 01-LoadJsonFile.exe if exist ${EXAMPLE01}.exe ${RMCMD} ${EXAMPLE01}.exe
if exist 01-LoadJsonFile ${RMCMD} 01-LoadJsonFile if exist ${EXAMPLE01} ${RMCMD} ${EXAMPLE01}
if exist 02-TestLuaScript.exe ${RMCMD} 02-TestLuaScript.exe if exist ${EXAMPLE02}.exe ${RMCMD} ${EXAMPLE02}.exe
if exist 02-TestLuaScript ${RMCMD} 02-TestLuaScript if exist ${EXAMPLE02} ${RMCMD} ${EXAMPLE02}
else # not ifeq ($(SHELL),sh.exe) else # not ifeq ($(SHELL),sh.exe)
${RMCMD} ./01-LoadJsonFile.exe ${RMCMD} ./${EXAMPLE01}.exe
${RMCMD} ./01-LoadJsonFile ${RMCMD} ./${EXAMPLE01}
${RMCMD} ./02-TestLuaScript.exe ${RMCMD} ./${EXAMPLE02}.exe
${RMCMD} ./02-TestLuaScript ${RMCMD} ./${EXAMPLE02}
endif # ifeq ($(SHELL),sh.exe) endif # ifeq ($(SHELL),sh.exe)
else # not ifeq ($(OS),Windows_NT) else # not ifeq ($(OS),Windows_NT)
${RMCMD} ./01-LoadJsonFile ${RMCMD} ${EXAMPLE01}
${RMCMD} ./02-TestLuaScript ${RMCMD} ${EXAMPLE02}
endif endif
@@ -80,23 +82,23 @@ upgrade:
go mod vendor go mod vendor
01-LoadJsonFile: clean ${EXAMPLE01}: clean
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
# Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set. # Looks like on windows gnu make explicitly set SHELL to sh.exe, if it was not set.
ifeq ($(SHELL), sh.exe) ifeq ($(SHELL), sh.exe)
# # Vanilla cmd.exe / powershell. # # Vanilla cmd.exe / powershell.
SET "CGO_ENABLED=0" SET "CGO_ENABLED=0"
go build ${BUILDOPTS} -o 01-LoadJsonFile.exe ./cmd/01-LoadJsonFile go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./cmd/${EXAMPLE01}
else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # git-bash # # git-bash
CGO_ENABLED=0 go build ${BUILDOPTS} -o 01-LoadJsonFile.exe ./cmd/01-LoadJsonFile CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01}.exe ./cmd/${EXAMPLE01}
else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # Some other shell. # # Some other shell.
# # TODO: handle it. # # TODO: handle it.
$(info "-- Dunno how to handle this shell: ${SHELL}") $(info "-- Dunno how to handle this shell: ${SHELL}")
endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
else # not ($(OS),Windows_NT) else # not ($(OS),Windows_NT)
CGO_ENABLED=0 go build ${BUILDOPTS} -o 01-LoadJsonFile ./cmd/01-LoadJsonFile CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE01} ./cmd/${EXAMPLE01}
endif # ifeq ($(OS),Windows_NT) endif # ifeq ($(OS),Windows_NT)
@@ -106,17 +108,18 @@ ifeq ($(OS),Windows_NT)
ifeq ($(SHELL), sh.exe) ifeq ($(SHELL), sh.exe)
# # Vanilla cmd.exe / powershell. # # Vanilla cmd.exe / powershell.
SET "CGO_ENABLED=0" SET "CGO_ENABLED=0"
go build ${BUILDOPTS} -o 02-TestLuaScript.exe ./cmd/02-TestLuaScript go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./cmd/${EXAMPLE02}
else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # git-bash # # git-bash
CGO_ENABLED=0 go build ${BUILDOPTS} -o 02-TestLuaScript.exe ./cmd/02-TestLuaScript CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02}.exe ./cmd/${EXAMPLE02}
else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) else # not ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
# # Some other shell. # # Some other shell.
# # TODO: handle it. # # TODO: handle it.
$(info "-- Dunno how to handle this shell: ${SHELL}") $(info "-- Dunno how to handle this shell: ${SHELL}")
endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL)))) endif # ifeq (,$(findstring(Git/usr/bin/sh.exe, $(SHELL))))
else # not ($(OS),Windows_NT) else # not ($(OS),Windows_NT)
CGO_ENABLED=0 go build ${BUILDOPTS} -o 02-TestLuaScript ./cmd/02-TestLuaScript CGO_ENABLED=0 go build ${BUILDOPTS} -o ${EXAMPLE02} ./cmd/${EXAMPLE02}
endif # ifeq ($(OS),Windows_NT) endif # ifeq ($(OS),Windows_NT)
# vim: set ft=make noet ai ts=4 sw=4 sts=4: # vim: set ft=make noet ai ts=4 sw=4 sts=4:

View File

@@ -1,21 +0,0 @@
package main
import (
"doll/internal/doll"
"doll/internal/log"
"os"
"github.com/davecgh/go-spew/spew"
)
func main() {
log.Init("info", os.Stderr)
log.Info("loading config.json")
data := doll.LoadJsonFile("test_data/config.json")
log.Info("data fron config.json is")
spew.Dump(data)
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */

View File

@@ -1,34 +0,0 @@
package main
import (
"doll/internal/log"
"os"
lua "github.com/yuin/gopher-lua"
)
func main() {
log.Init("info", os.Stderr)
L := lua.NewState()
defer L.Close()
// Load the Lua script file.
log.Info("Loading ./test_data/01-test.lua")
luaCode, err := os.ReadFile("./test_data/01-test.lua")
if err != nil {
log.Errorf("Error reading Lua file: %s", err)
os.Exit(1)
}
if err := L.DoString(string(luaCode)); err != nil {
log.Errorf("Error evaluating Lua code: %s", err)
os.Exit(1)
}
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */

3
example_data/02-test.lua Normal file
View File

@@ -0,0 +1,3 @@
print ("Hello World!")
-- vim: set ft=lua noet ai ts=4 sw=4 sts=4:

View 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: */

View 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: */

View File

@@ -1,30 +1,30 @@
package doll package doll
import ( import (
"doll/internal/log"
"encoding/json" "encoding/json"
"fmt"
"os" "os"
) )
// LoadJsonFile loads data from json to Instance global variable map. // LoadJsonFile loads data from json to Instance global variable map.
func LoadJsonFile(file string) map[string]any { func LoadJsonFile(file string) (map[string]any, error) {
var ( var (
err error err error
data []byte data []byte
myMap map[string]any
) )
data, err = os.ReadFile(file) data, err = os.ReadFile(file)
if err != nil { if err != nil {
log.Fatalf("Unable to load %s: %s", file, err) return myMap, fmt.Errorf("Unable to load %s: %w", file, err)
} }
err = json.Unmarshal([]byte(data), &Instance) if err = json.Unmarshal([]byte(data), &myMap); err != nil {
if err != nil { return myMap, fmt.Errorf("Unable to unmarshal JSON data from %s: %w", file, err)
log.Fatalf("Unable to marshal JSON data from %s: %s", file, err)
} }
return Instance return myMap, err
} }
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */ /* vim: set ft=go noet ai ts=4 sw=4 sts=4: */

View File

@@ -1,3 +0,0 @@
print ("Hello World!")
-- vim: set ft=go noet ai ts=4 sw=4 sts=4: