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) IfcGetStr(ifc0 interface{}, arr ...string) (string, error) { ifc, err := app.IfcGet(ifc0, arr...) if err != nil { app.Log(err) 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; case bool: if ifc.(bool) { return "true", nil } return "false", nil } return "", errors.New("Invalid type value not string or float64/bool"); } func (app *App) ConfGetStr(arr ...string) (string, error) { return app.IfcGetStr(app.Conf, arr...) } func (app *App) IfcGetFlt(ifc0 interface{}, arr ...string) (float64, error) { ifc, err := app.IfcGet(ifc0, arr...) if err != nil { app.Log(err) return 0, err } switch ifc.(type) { case string: flt, err := strconv.ParseFloat(ifc.(string), 64) if err != nil { app.Log(err) return 0, err } return flt, nil; case float64: return ifc.(float64), nil; case bool: if ifc.(bool) { return 1, nil } return 0, nil } return 0, errors.New("Invalid type value not float64 or string/bool"); } func (app *App) ConfGetFlt(arr ...string) (float64, error) { return app.IfcGetFlt(app.Conf, arr...) } 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; }