From 02315c7ad0078c8ea34d9230d97d2170da15ddf0 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Fri, 1 Oct 2021 11:56:56 +0300 Subject: [PATCH] Add functions for getting interface type values from config/APIOpts --- engine/libfilters.go | 20 ++++++++++++++++++++ utils/cgrevent.go | 10 ++++++++++ 2 files changed, 30 insertions(+) diff --git a/engine/libfilters.go b/engine/libfilters.go index ec33eaee0..d8dece30a 100644 --- a/engine/libfilters.go +++ b/engine/libfilters.go @@ -165,3 +165,23 @@ func FilterDecimalBigCfgOpts(ctx *context.Context, tnt string, ev utils.DataProv } return // return the option or NOT_FOUND if there are no options or none of the filters pass } + +// FilterInterfaceCfgOpts returns the option as interface{} if the filters match +func FilterInterfaceCfgOpts(ctx *context.Context, tnt string, ev utils.DataProvider, fS *FilterS, dynOpts []*utils.DynamicInterfaceOpt) (dft interface{}, err error) { + var hasDefault bool + for _, opt := range dynOpts { // iterate through the options + if len(opt.FilterIDs) == 0 { + hasDefault = true + dft = opt.Value + } + if pass, err := fS.Pass(ctx, tnt, opt.FilterIDs, ev); err != nil { // check if the filter is passing for the DataProvider and return the option if it does + return false, err + } else if pass { + return opt.Value, nil + } + } + if !hasDefault { + err = utils.ErrNotFound + } + return // return the option or NOT_FOUND if there are no options or none of the filters pass +} diff --git a/utils/cgrevent.go b/utils/cgrevent.go index 96ab59be1..4dd7e4bb2 100644 --- a/utils/cgrevent.go +++ b/utils/cgrevent.go @@ -150,6 +150,16 @@ func (ev *CGREvent) OptsAsStringSlice(defaultValue []string, optNames ...string) return defaultValue, nil } +// OptsAsInterface returns an option as float64 +func (ev *CGREvent) OptsAsInterface(defaultValue interface{}, optNames ...string) interface{} { + for _, optName := range optNames { + if iface, has := ev.APIOpts[optName]; has { + return iface + } + } + return defaultValue +} + func (ev *CGREvent) Clone() (clned *CGREvent) { clned = &CGREvent{ Tenant: ev.Tenant,