Initial 2024-01-26
This commit is contained in:
103
cmpSnipper/cmpSnipperConf.go
Normal file
103
cmpSnipper/cmpSnipperConf.go
Normal file
@@ -0,0 +1,103 @@
|
||||
|
||||
package cmpSnipper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func (app *App) IfcGet(ifc interface{}, arr ...string) (interface{}, error) {
|
||||
var cnt = len(arr);
|
||||
var idx string
|
||||
|
||||
for j := 0; j < cnt; j++ {
|
||||
idx = arr[j]
|
||||
|
||||
switch v := ifc.(type) {
|
||||
case string:
|
||||
app.Log("Convert string to ifc")
|
||||
ifc = ifc.(interface{})
|
||||
|
||||
case map[string]interface{}:
|
||||
// app.Log("Valid ifc " + idx)
|
||||
ifc = v[idx];
|
||||
|
||||
default:
|
||||
app.Log("Invalid type variable %s %T", idx, ifc)
|
||||
return nil, errors.New("Invalid type variable");
|
||||
}
|
||||
}
|
||||
|
||||
if ifc == nil {
|
||||
return nil, errors.New("Undefine variable " + idx);
|
||||
}
|
||||
|
||||
// app.Log("Found ifc %s %T", idx, ifc)
|
||||
|
||||
return ifc, nil;
|
||||
}
|
||||
|
||||
func (app *App) ConfGetIfc(arr ...string) (interface{}, error) {
|
||||
return app.IfcGet(app.Conf, arr...);
|
||||
}
|
||||
|
||||
func (app *App) ConfGetStr(arr ...string) (string, error) {
|
||||
ifc, err := app.ConfGetIfc(arr...);
|
||||
|
||||
if err != nil {
|
||||
return "", err;
|
||||
}
|
||||
|
||||
switch ifc.(type) {
|
||||
case string:
|
||||
return ifc.(string), nil;
|
||||
case float64:
|
||||
f := ifc.(float64)
|
||||
u := uint64(f)
|
||||
return strconv.FormatUint(u, 10), nil;
|
||||
default:
|
||||
return "", errors.New("Invalid type value !s");
|
||||
}
|
||||
|
||||
return "", errors.New("Invalid type value");
|
||||
}
|
||||
|
||||
func (app *App) ConfGetArr(arr ...string) ([]interface{}, error) {
|
||||
ifc, err := app.ConfGetIfc(arr...);
|
||||
|
||||
if err != nil {
|
||||
return nil, err;
|
||||
}
|
||||
|
||||
switch v := ifc.(type) {
|
||||
case []interface{}:
|
||||
return v, nil
|
||||
default:
|
||||
fmt.Printf("Invalid type value %+q\n", ifc)
|
||||
return nil, errors.New("Invalid type value !s");
|
||||
}
|
||||
|
||||
return nil, errors.New("Invalid type value");
|
||||
}
|
||||
|
||||
func (app *App) ConfLoadFile(file string) error {
|
||||
data, err := app.FileReadLimit(file, 1048576);
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// fmt.Println(string(data));
|
||||
|
||||
err = json.Unmarshal(data, &app.Conf)
|
||||
|
||||
if err != nil {
|
||||
app.Log(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
Reference in New Issue
Block a user