Files
cgrates/engine/storage_mongo_config.go
ionutboangiu 26cdb571b8 Upgrade MongoDB driver to 1.13
- Set (but comment) serverAPI options (currently distinct api and
create.size BSON field are deprecated + possible others that are untested)
- Remove the custom time decoder used for mongo BSON
datetime values. The custom decoder was only converting these values
into UTC and was not any different from the default time.Time
decoder in the MongoDB driver, which also handles BSON string, int64,
and document values.
- Implement 'buildURL' function to connect to mongo (can also be
used for mysql and postgres)
- Update function names, variable names, and comments for clarity
- Replace 'bsonx.Regex' with the Regex primitive (deprecated since 1.12).
- Use simple concatenation instead of Sprintf
- Declare 'decimalType' locally, replace global 'decimalType'
- Simplify several functions without altering functionality
- Converting directly from a D to an M is deprecated. We are now decoding
  directly in a M.
- Used errors.As and errors.Is for proper error comparison and assertion
- Revised sloppy reassignments and added missing error checks
2023-11-14 18:24:21 +01:00

77 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 engine
import (
"errors"
"reflect"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
ColCfg = "config"
)
func (ms *MongoStorage) GetSection(ctx *context.Context, section string, val any) error {
return ms.query(context.TODO(), func(sctx mongo.SessionContext) error {
sr := ms.getCol(ColCfg).FindOne(sctx, bson.M{"section": section},
options.FindOne().SetProjection(bson.M{"cfg": 1, "_id": 0 /*"section": 0, */}))
tmp := map[string]bson.Raw{}
decodeErr := sr.Decode(&tmp)
if decodeErr != nil {
if errors.Is(decodeErr, mongo.ErrNoDocuments) {
return nil
}
return decodeErr
}
reg := bson.NewRegistry()
decimalType := reflect.TypeOf(utils.Decimal{})
reg.RegisterTypeEncoder(decimalType, bsoncodec.ValueEncoderFunc(decimalEncoder))
reg.RegisterTypeDecoder(decimalType, bsoncodec.ValueDecoderFunc(decimalDecoder))
dec, err := bson.NewDecoder(bsonrw.NewBSONDocumentReader(tmp["cfg"]))
if err != nil {
return err
}
if err = dec.SetRegistry(reg); err != nil {
return err
}
return dec.Decode(val)
})
}
func (ms *MongoStorage) SetSection(ctx *context.Context, section string, jsn any) (err error) {
return ms.query(ctx, func(sctx mongo.SessionContext) (err error) {
_, err = ms.getCol(ColCfg).UpdateOne(sctx, bson.M{"section": section},
bson.M{"$set": bson.M{
"section": section,
"cfg": jsn}},
options.Update().SetUpsert(true),
)
return err
})
}