Files
cgrates/utils/dataprovider.go
2021-03-26 13:46:25 +01:00

76 lines
2.3 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 utils
import (
"net"
"strings"
)
// NMType the type used for navigable Map
type NMType byte
// posible NMType
const (
NMDataType NMType = iota
NMMapType
NMSliceType
)
// DataProvider is a data source from multiple formats
type DataProvider interface {
String() string // printable version of data
FieldAsInterface(fldPath []string) (interface{}, error)
FieldAsString(fldPath []string) (string, error) // remove this
RemoteHost() net.Addr
}
// RWDataProvider is a DataProvider with write methods on it
type RWDataProvider interface {
DataProvider
Set(fldPath []string, val interface{}) (err error)
Remove(fldPath []string) (err error)
}
// NavigableMapper is the interface supported by replies convertible to CGRReply
type NavigableMapper interface {
AsNavigableMap() map[string]*DataNode
}
// DPDynamicInterface returns the value of the field if the path is dynamic
func DPDynamicInterface(dnVal string, dP DataProvider) (interface{}, error) {
if strings.HasPrefix(dnVal, DynamicDataPrefix) &&
dnVal != DynamicDataPrefix {
dnVal = strings.TrimPrefix(dnVal, DynamicDataPrefix)
return dP.FieldAsInterface(strings.Split(dnVal, NestingSep))
}
return StringToInterface(dnVal), nil
}
// DPDynamicString returns the string value of the field if the path is dynamic
func DPDynamicString(dnVal string, dP DataProvider) (string, error) {
if strings.HasPrefix(dnVal, DynamicDataPrefix) &&
dnVal != DynamicDataPrefix {
dnVal = strings.TrimPrefix(dnVal, DynamicDataPrefix)
return dP.FieldAsString(strings.Split(dnVal, NestingSep))
}
return dnVal, nil
}