Started adding FieldAsInterface on Account

This commit is contained in:
Trial97
2021-11-17 08:03:54 +02:00
committed by Dan Christian Bogos
parent 941531039e
commit e2342c3997
4 changed files with 235 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
@@ -1273,3 +1274,114 @@ func (as *AccountSummary) AsMapInterface() map[string]interface{} {
utils.BalanceSummaries: as.BalanceSummaries,
}
}
func (acc *Account) String() string {
return utils.ToJSON(acc)
}
func (acc *Account) FieldAsInterface(fldPath []string) (val interface{}, err error) {
if acc == nil || len(fldPath) == 0 {
return nil, utils.ErrNotFound
}
switch fldPath[0] {
default:
opath, indx := utils.GetPathIndexString(fldPath[0])
if indx != nil {
switch opath {
case utils.BalanceMap:
bl, has := acc.BalanceMap[*indx]
if !has {
return nil, utils.ErrNotFound
}
if len(fldPath) == 1 {
return bl, nil
}
return bl.FieldAsInterface(fldPath[1:])
case utils.UnitCounters:
uc, has := acc.UnitCounters[*indx]
if !has || len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return uc, nil
case utils.ActionTriggers:
var idx int
if idx, err = strconv.Atoi(*indx); err != nil {
return
}
if len(acc.ActionTriggers) <= idx {
return nil, utils.ErrNotFound
}
at := acc.ActionTriggers[idx]
if len(fldPath) == 1 {
return at, nil
}
return at.FieldAsInterface(fldPath[1:])
}
}
return nil, fmt.Errorf("unsupported field prefix: <%s>", fldPath[0])
case utils.ID:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return acc.ID, nil
case utils.BalanceMap:
if len(fldPath) != 1 {
return acc.BalanceMap, nil
}
if bc, has := acc.BalanceMap[fldPath[1]]; has {
if len(fldPath) == 2 {
return bc, nil
}
return bc.FieldAsInterface(fldPath[2:])
}
return nil, utils.ErrNotFound
case utils.UnitCounters:
if len(fldPath) != 1 {
return acc.UnitCounters, nil
}
if uc, has := acc.UnitCounters[fldPath[1]]; has {
if len(fldPath) == 2 {
return uc, nil
}
return uc.FieldAsInterface(fldPath[2:])
}
return nil, utils.ErrNotFound
case utils.ActionTriggers:
if len(fldPath) != 1 {
return acc.ActionTriggers, nil
}
for _, at := range acc.ActionTriggers {
if at.ID == fldPath[1] {
if len(fldPath) == 2 {
return at, nil
}
return at.FieldAsInterface(fldPath[2:])
}
}
return nil, utils.ErrNotFound
case utils.AllowNegative:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return acc.AllowNegative, nil
case utils.Disabled:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return acc.Disabled, nil
case utils.UpdateTime:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return acc.UpdateTime, nil
}
}
func (acc *Account) FieldAsString(fldPath []string) (val string, err error) {
var iface interface{}
iface, err = acc.FieldAsInterface(fldPath)
if err != nil {
return
}
return utils.IfaceAsString(iface), nil
}

View File

@@ -213,3 +213,96 @@ func (atpl ActionTriggers) Clone() ActionTriggers {
}
return clone
}
func (at *ActionTrigger) String() string {
return utils.ToJSON(at)
}
func (at *ActionTrigger) FieldAsInterface(fldPath []string) (val interface{}, err error) {
if at == nil || len(fldPath) == 0 {
return nil, utils.ErrNotFound
}
switch fldPath[0] {
default:
return nil, fmt.Errorf("unsupported field prefix: <%s>", fldPath[0])
case utils.ID:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.ID, nil
case utils.UniqueID:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.UniqueID, nil
case utils.ThresholdType:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.ThresholdType, nil
case utils.ThresholdValue:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.ThresholdValue, nil
case utils.Recurrent:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.Recurrent, nil
case utils.MinSleep:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.MinSleep, nil
case utils.ExpirationDate:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.ExpirationDate, nil
case utils.ActivationDate:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.ActivationDate, nil
case utils.BalanceField:
if len(fldPath) == 1 {
return at.Balance, nil
}
return at.Balance.FieldAsInterface(fldPath[1:])
case utils.Weight:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.Weight, nil
case utils.ActionsID:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.ActionsID, nil
case utils.MinQueuedItems:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.MinQueuedItems, nil
case utils.Executed:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.Executed, nil
case utils.LastExecutionTime:
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
return at.LastExecutionTime, nil
}
}
func (at *ActionTrigger) FieldAsString(fldPath []string) (val string, err error) {
var iface interface{}
iface, err = at.FieldAsInterface(fldPath)
if err != nil {
return
}
return utils.IfaceAsString(iface), nil
}

View File

@@ -151,3 +151,31 @@ func (ucs UnitCounters) resetCounters(a *Action) {
}
}
}
/*
func (uc *UnitCounter) String() string {
return utils.ToJSON(uc)
}
func (uc *UnitCounter) FieldAsInterface(fldPath []string) (val interface{}, err error) {
if uc == nil || len(fldPath) == 0 {
return nil, utils.ErrNotFound
}
switch fldPath[0] {
default:
return nil, fmt.Errorf("unsupported field prefix: <%s>", fldPath[0])
case utils.CounterType:
case utils.Counters:
}
}
func (uc *UnitCounter) FieldAsString(fldPath []string) (val string, err error) {
var iface interface{}
iface, err = uc.FieldAsInterface(fldPath)
if err != nil {
return
}
return utils.IfaceAsString(iface), nil
}
*/

View File

@@ -452,6 +452,7 @@ const (
MetaEveryMinute = "*every_minute"
MetaHourly = "*hourly"
ID = "ID"
UniqueID = "UniqueID"
Address = "Address"
Addresses = "Addresses"
Transport = "Transport"
@@ -760,6 +761,7 @@ const (
ThresholdValue = "ThresholdValue"
Recurrent = "Recurrent"
Executed = "Executed"
LastExecutionTime = "LastExecutionTime"
MinSleep = "MinSleep"
ActivationDate = "ActivationDate"
ExpirationDate = "ExpirationDate"