From e2342c3997bb854ccfdb8c35a809312cbcc31cf9 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Wed, 17 Nov 2021 08:03:54 +0200 Subject: [PATCH] Started adding FieldAsInterface on Account --- engine/account.go | 112 +++++++++++++++++++++++++++++++++++++++ engine/action_trigger.go | 93 ++++++++++++++++++++++++++++++++ engine/units_counter.go | 28 ++++++++++ utils/consts.go | 2 + 4 files changed, 235 insertions(+) diff --git a/engine/account.go b/engine/account.go index f6798b89f..bb5b6d2a5 100644 --- a/engine/account.go +++ b/engine/account.go @@ -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 +} diff --git a/engine/action_trigger.go b/engine/action_trigger.go index 409777b7e..430385841 100644 --- a/engine/action_trigger.go +++ b/engine/action_trigger.go @@ -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 +} diff --git a/engine/units_counter.go b/engine/units_counter.go index 7eca6a478..0012da03c 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -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 +} +*/ diff --git a/utils/consts.go b/utils/consts.go index 896235392..bd96d68d9 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -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"