From 40d88705846c5c7be0bfb6a63ce9ec885857a80e Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Thu, 10 Sep 2020 17:40:29 +0300 Subject: [PATCH] Update AsMapInterface function in config for AttributeS --- config/attributescfg.go | 41 ++++++++++++---------- config/attributescfg_test.go | 66 +++++++++++++++++++++++++++++------- utils/consts.go | 1 + 3 files changed, 77 insertions(+), 31 deletions(-) diff --git a/config/attributescfg.go b/config/attributescfg.go index cb9af3cb5..a994e19ef 100644 --- a/config/attributescfg.go +++ b/config/attributescfg.go @@ -72,27 +72,32 @@ func (alS *AttributeSCfg) loadFromJsonCfg(jsnCfg *AttributeSJsonCfg) (err error) } func (alS *AttributeSCfg) AsMapInterface() map[string]interface{} { - stringIndexedFields := []string{} - if alS.StringIndexedFields != nil { - stringIndexedFields = make([]string, len(*alS.StringIndexedFields)) - for i, item := range *alS.StringIndexedFields { - stringIndexedFields[i] = item - } - } - prefixIndexedFields := []string{} - if alS.PrefixIndexedFields != nil { - prefixIndexedFields = make([]string, len(*alS.PrefixIndexedFields)) - for i, item := range *alS.PrefixIndexedFields { - prefixIndexedFields[i] = item - } - } - return map[string]interface{}{ + initialMP := map[string]interface{}{ utils.EnabledCfg: alS.Enabled, utils.IndexedSelectsCfg: alS.IndexedSelects, - utils.StringIndexedFieldsCfg: stringIndexedFields, - utils.PrefixIndexedFieldsCfg: prefixIndexedFields, utils.ProcessRunsCfg: alS.ProcessRuns, utils.NestedFieldsCfg: alS.NestedFields, } - + if alS.StringIndexedFields != nil { + stringIndexedFields := make([]string, len(*alS.StringIndexedFields)) + for i, item := range *alS.StringIndexedFields { + stringIndexedFields[i] = item + } + initialMP[utils.StringIndexedFieldsCfg] = stringIndexedFields + } + if alS.PrefixIndexedFields != nil { + prefixIndexedFields := make([]string, len(*alS.PrefixIndexedFields)) + for i, item := range *alS.PrefixIndexedFields { + prefixIndexedFields[i] = item + } + initialMP[utils.PrefixIndexedFieldsCfg] = prefixIndexedFields + } + if alS.SuffixIndexedFields != nil { + suffixIndexedFields := make([]string, len(*alS.SuffixIndexedFields)) + for i, item := range *alS.SuffixIndexedFields { + suffixIndexedFields[i] = item + } + initialMP[utils.SuffixIndexedFieldsCfg] = suffixIndexedFields + } + return initialMP } diff --git a/config/attributescfg_test.go b/config/attributescfg_test.go index 7faaabf2e..51c4e6ed3 100644 --- a/config/attributescfg_test.go +++ b/config/attributescfg_test.go @@ -61,7 +61,6 @@ func TestAttributeSCfgloadFromJsonCfg(t *testing.T) { } func TestAttributeSCfgAsMapInterface(t *testing.T) { - var attscfg AttributeSCfg cfgJSONStr := `{ "attributes": { "enabled": true, @@ -70,20 +69,61 @@ func TestAttributeSCfgAsMapInterface(t *testing.T) { }, }` eMap := map[string]interface{}{ - "enabled": true, - "prefix_indexed_fields": []string{"*req.index1", "*req.index2"}, - "process_runs": 3, - "indexed_selects": false, - "nested_fields": false, - "string_indexed_fields": []string{}, + utils.EnabledCfg: true, + utils.PrefixIndexedFieldsCfg: []string{"*req.index1", "*req.index2"}, + utils.ProcessRunsCfg: 3, + utils.IndexedSelectsCfg: true, + utils.NestedFieldsCfg: false, + utils.SuffixIndexedFieldsCfg: []string{}, } - if jsnCfg, err := NewCgrJsonCfgFromBytes([]byte(cfgJSONStr)); err != nil { + if cgrCfg, err := NewCGRConfigFromJsonStringWithDefaults(cfgJSONStr); err != nil { t.Error(err) - } else if jsnAttSCfg, err := jsnCfg.AttributeServJsonCfg(); err != nil { - t.Error(err) - } else if err = attscfg.loadFromJsonCfg(jsnAttSCfg); err != nil { - t.Error(err) - } else if rcv := attscfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { + } else if rcv := cgrCfg.attributeSCfg.AsMapInterface(); !reflect.DeepEqual(eMap, rcv) { t.Errorf("\nExpected: %+v\nRecived: %+v", utils.ToJSON(eMap), utils.ToJSON(rcv)) } } + +func TestAttributeSCfgAsMapInterface2(t *testing.T) { + cfgJSONStr := `{ + "attributes": { + "suffix_indexed_fields": ["*req.index1","*req.index2"], + "nested_fields": true, + "enabled": true, + "process_runs": 7, + }, +}` + expectedMap := map[string]interface{}{ + utils.EnabledCfg: true, + utils.IndexedSelectsCfg: true, + utils.PrefixIndexedFieldsCfg: []string{}, + utils.SuffixIndexedFieldsCfg: []string{"*req.index1", "*req.index2"}, + utils.NestedFieldsCfg: true, + utils.ProcessRunsCfg: 7, + } + if cgrCfg, err := NewCGRConfigFromJsonStringWithDefaults(cfgJSONStr); err != nil { + t.Error(err) + } else if newMap := cgrCfg.attributeSCfg.AsMapInterface(); !reflect.DeepEqual(expectedMap, newMap) { + t.Errorf("Expected %+v \n, recieved %+v", utils.ToJSON(expectedMap), utils.ToJSON(newMap)) + } +} + +func TestAttributeSCfgAsMapInterface3(t *testing.T) { + myJSONStr := ` +{ + "attributes": {} +} +` + expectedMap := map[string]interface{}{ + utils.EnabledCfg: false, + utils.IndexedSelectsCfg: true, + utils.PrefixIndexedFieldsCfg: []string{}, + utils.SuffixIndexedFieldsCfg: []string{}, + utils.NestedFieldsCfg: false, + utils.ProcessRunsCfg: 1, + } + if conv, err := NewCGRConfigFromJsonStringWithDefaults(myJSONStr); err != nil { + t.Error(err) + } else if newMap := conv.attributeSCfg.AsMapInterface(); !reflect.DeepEqual(expectedMap, newMap) { + t.Errorf("Expected %+v, recieved %+v", expectedMap, newMap) + } +} diff --git a/utils/consts.go b/utils/consts.go index 9d186a74f..ba97311e8 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -1888,6 +1888,7 @@ const ( ConnMaxLifetimeCfg = "conn_max_lifetime" StringIndexedFieldsCfg = "string_indexed_fields" PrefixIndexedFieldsCfg = "prefix_indexed_fields" + SuffixIndexedFieldsCfg = "suffix_indexed_fields" QueryTimeoutCfg = "query_timeout" SSLModeCfg = "sslmode" ItemsCfg = "items"