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:
@@ -1,30 +1,30 @@
|
||||
package doll
|
||||
|
||||
import (
|
||||
"doll/internal/log"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// 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 (
|
||||
err error
|
||||
data []byte
|
||||
err error
|
||||
data []byte
|
||||
myMap map[string]any
|
||||
)
|
||||
|
||||
data, err = os.ReadFile(file)
|
||||
|
||||
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 != nil {
|
||||
log.Fatalf("Unable to marshal JSON data from %s: %s", file, err)
|
||||
if err = json.Unmarshal([]byte(data), &myMap); err != nil {
|
||||
return myMap, fmt.Errorf("Unable to unmarshal JSON data from %s: %w", file, err)
|
||||
}
|
||||
|
||||
return Instance
|
||||
return myMap, err
|
||||
}
|
||||
|
||||
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|
||||
|
||||
Reference in New Issue
Block a user