Files
cgrates/config/slicedp.go

98 lines
2.9 KiB
Go

/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package config
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/cgrates/cgrates/utils"
)
// NewSliceDP constructs a DataProvider
func NewSliceDP(record []string, pathPrfx string) (dP DataProvider) {
dP = &SliceDP{req: record, cache: NewNavigableMap(nil), pathPrfx: pathPrfx}
return
}
// SliceDP implements engine.DataProvider so we can pass it to filters
type SliceDP struct {
req []string
cache *NavigableMap
pathPrfx string // if this comes in path it will be ignored
// pathPrfx should be reviewed once the cdrc is removed
}
// String is part of engine.DataProvider interface
// when called, it will display the already parsed values out of cache
func (cP *SliceDP) String() string {
return utils.ToJSON(cP)
}
// FieldAsInterface is part of engine.DataProvider interface
func (cP *SliceDP) FieldAsInterface(fldPath []string) (data interface{}, err error) {
if len(fldPath) == 0 {
return
}
idx := fldPath[0]
if len(fldPath) == 2 {
idx = fldPath[1]
}
if cP.pathPrfx != utils.EmptyString && (fldPath[0] != cP.pathPrfx || len(fldPath) < 2) {
return "", utils.ErrPrefixNotFound(strings.Join(fldPath, utils.NestingSep))
}
if data, err = cP.cache.FieldAsInterface(fldPath); err == nil ||
err != utils.ErrNotFound { // item found in cache
return
}
err = nil // cancel previous err
if cfgFieldIdx, err := strconv.Atoi(idx); err != nil {
return nil, fmt.Errorf("Ignoring record: %v with error : %+v", cP.req, err)
} else if len(cP.req) <= cfgFieldIdx {
return nil, utils.ErrNotFound
} else {
data = cP.req[cfgFieldIdx]
}
cP.cache.Set(fldPath, data, false, false)
return
}
// FieldAsString is part of engine.DataProvider interface
func (cP *SliceDP) FieldAsString(fldPath []string) (data string, err error) {
var valIface interface{}
valIface, err = cP.FieldAsInterface(fldPath)
if err != nil {
return
}
return utils.IfaceAsString(valIface), nil
}
// AsNavigableMap is part of engine.DataProvider interface
func (cP *SliceDP) AsNavigableMap([]*FCTemplate) (
nm *NavigableMap, err error) {
return nil, utils.ErrNotImplemented
}
// RemoteHost is part of engine.DataProvider interface
func (cP *SliceDP) RemoteHost() net.Addr {
return utils.LocalAddr()
}