set extra fields from user profile too

This commit is contained in:
Radu Ioan Fericean
2015-07-29 12:53:48 +03:00
parent 13d77363f5
commit f0fe0d9ef4
4 changed files with 104 additions and 46 deletions

View File

@@ -359,10 +359,7 @@ func LoadUserProfile(in interface{}, extraFields string) (interface{}, error) {
if userService == nil { // no user service => no fun
return in, nil
}
m, err := utils.ToMapStringString(in)
if err != nil {
return nil, err
}
m := utils.ToMapStringString(in)
up := &UserProfile{
Profile: make(map[string]string),
@@ -381,12 +378,9 @@ func LoadUserProfile(in interface{}, extraFields string) (interface{}, error) {
}
// add extra fields
if extraFields != "" {
extra, err := utils.GetMapExtraFields(in, extraFields)
if err != nil {
return nil, err
}
extra := utils.GetMapExtraFields(in, extraFields)
for key, val := range extra {
if val != "" {
if val != "" && val != utils.USERS {
up.Profile[key] = val
}
}
@@ -399,7 +393,9 @@ func LoadUserProfile(in interface{}, extraFields string) (interface{}, error) {
up = ups[0]
m := up.Profile
m["Tenant"] = up.Tenant
return utils.FromMapStringString(m, in)
utils.FromMapStringString(m, in)
utils.SetMapExtraFields(in, m, extraFields)
return in, nil
}
return nil, utils.ErrNotFound
}

View File

@@ -562,7 +562,7 @@ func TestUsersStoredCDRGetLoadUserProfile(t *testing.T) {
AnswerTime: "t4",
Usage: "13",
}
*ur = out.(UsageRecord)
ur = out.(*UsageRecord)
if !reflect.DeepEqual(ur, expected) {
t.Errorf("Expected: %+v got: %+v", expected, ur)
}
@@ -612,8 +612,11 @@ func TestUsersStoredCDRGetLoadUserProfileExtraFields(t *testing.T) {
SetupTime: "s4",
AnswerTime: "t4",
Usage: "13",
ExtraFields: map[string]string{
"Test": "1",
},
}
*ur = out.(ExternalCdr)
ur = out.(*ExternalCdr)
if !reflect.DeepEqual(ur, expected) {
t.Errorf("Expected: %+v got: %+v", expected, ur)
}
@@ -652,3 +655,59 @@ func TestUsersStoredCDRGetLoadUserProfileExtraFieldsNotFound(t *testing.T) {
t.Error("Error detecting err in loading user profile: ", err)
}
}
func TestUsersStoredCDRGetLoadUserProfileExtraFieldsSet(t *testing.T) {
userService = &UserMap{
table: map[string]map[string]string{
"test:user": map[string]string{"TOR": "01", "ReqType": "1", "Direction": "*out", "Category": "c1", "Account": "dan", "Subject": "0723", "Destination": "+401", "SetupTime": "s1", "AnswerTime": "t1", "Usage": "10"},
":user": map[string]string{"TOR": "02", "ReqType": "2", "Direction": "*out", "Category": "c2", "Account": "ivo", "Subject": "0724", "Destination": "+402", "SetupTime": "s2", "AnswerTime": "t2", "Usage": "11"},
"test:": map[string]string{"TOR": "03", "ReqType": "3", "Direction": "*out", "Category": "c3", "Account": "elloy", "Subject": "0725", "Destination": "+403", "SetupTime": "s3", "AnswerTime": "t3", "Usage": "12"},
"test1:user1": map[string]string{"TOR": "04", "ReqType": "4", "Direction": "*out", "Category": "call", "Account": "rif", "Subject": "0726", "Destination": "+404", "SetupTime": "s4", "AnswerTime": "t4", "Usage": "13", "Test": "1", "Best": "BestValue"},
},
index: make(map[string]map[string]bool),
}
ur := &ExternalCdr{
TOR: utils.USERS,
ReqType: utils.USERS,
Direction: "*out",
Tenant: "",
Category: "call",
Account: utils.USERS,
Subject: utils.USERS,
Destination: utils.USERS,
SetupTime: utils.USERS,
AnswerTime: utils.USERS,
Usage: "13",
ExtraFields: map[string]string{
"Test": "1",
"Best": utils.USERS,
},
}
out, err := LoadUserProfile(ur, "ExtraFields")
if err != nil {
t.Error("Error loading user profile: ", err)
}
expected := &ExternalCdr{
TOR: "04",
ReqType: "4",
Direction: "*out",
Tenant: "",
Category: "call",
Account: "rif",
Subject: "0726",
Destination: "+404",
SetupTime: "s4",
AnswerTime: "t4",
Usage: "13",
ExtraFields: map[string]string{
"Test": "1",
"Best": "BestValue",
},
}
ur = out.(*ExternalCdr)
if !reflect.DeepEqual(ur, expected) {
t.Errorf("Expected: %+v got: %+v", expected, ur)
}
}

View File

@@ -78,7 +78,7 @@ func NonemptyStructFields(s interface{}) map[string]interface{} {
}*/
// Converts a struct to map[string]interface{}
func ToMapMapStringInterface(in interface{}) (map[string]interface{}, error) {
func ToMapMapStringInterface(in interface{}) map[string]interface{} {
out := make(map[string]interface{})
v := reflect.ValueOf(in)
@@ -89,11 +89,11 @@ func ToMapMapStringInterface(in interface{}) (map[string]interface{}, error) {
for i := 0; i < v.NumField(); i++ {
out[typ.Field(i).Name] = v.Field(i).Interface()
}
return out, nil
return out
}
// Converts a struct to map[string]string
func ToMapStringString(in interface{}) (map[string]string, error) {
func ToMapStringString(in interface{}) map[string]string {
out := make(map[string]string)
v := reflect.ValueOf(in)
@@ -110,11 +110,10 @@ func ToMapStringString(in interface{}) (map[string]string, error) {
out[typField.Name] = field.String()
}
}
return out, nil
return out
}
// Converts a struct to map[string]string
func GetMapExtraFields(in interface{}, extraFields string) (map[string]string, error) {
func GetMapExtraFields(in interface{}, extraFields string) map[string]string {
out := make(map[string]string)
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
@@ -128,21 +127,38 @@ func GetMapExtraFields(in interface{}, extraFields string) (map[string]string, e
out[key.String()] = field.MapIndex(key).String()
}
}
return out, nil
return out
}
func FromMapStringString(m map[string]string, in interface{}) (interface{}, error) {
func SetMapExtraFields(in interface{}, values map[string]string, extraFields string) {
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
in = v.Interface()
}
st := reflect.TypeOf(in)
elem := reflect.New(st).Elem()
for fieldName, fieldValue := range m {
field := elem.FieldByName(fieldName)
if field.IsValid() {
efField := v.FieldByName(extraFields)
if efField.Kind() == reflect.Map {
keys := efField.MapKeys()
for _, key := range keys {
if efField.MapIndex(key).String() != "" {
if val, found := values[key.String()]; found {
efField.SetMapIndex(key, reflect.ValueOf(val))
}
}
}
}
return
}
func FromMapStringString(m map[string]string, in interface{}) {
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
in = v.Interface()
}
for fieldName, fieldValue := range m {
field := v.FieldByName(fieldName)
if field.IsValid() {
if field.Kind() == reflect.String {
if v.FieldByName(fieldName).String() != "" {
field.SetString(fieldValue)
@@ -150,7 +166,7 @@ func FromMapStringString(m map[string]string, in interface{}) (interface{}, erro
}
}
}
return elem.Interface(), nil
return
}
// Update struct with map fields, returns not matching map keys, s is a struct to be updated

View File

@@ -18,14 +18,9 @@ func TestStructMapStruct(t *testing.T) {
Address: "3",
Other: "",
}
m, err := ToMapStringString(ts)
if err != nil {
t.Error("Error converting to map: ", err)
}
out, err := FromMapStringString(m, ts)
if err != nil {
t.Error("Error converting to struct: ", err)
}
m := ToMapStringString(ts)
out := FromMapStringString(m, ts)
nts := out.(TestStruct)
if !reflect.DeepEqual(ts, &nts) {
t.Log(m)
@@ -46,15 +41,10 @@ func TestMapStructAddStructs(t *testing.T) {
Address: "3",
Other: "",
}
m, err := ToMapStringString(ts)
if err != nil {
t.Error("Error converting to map: ", err)
}
m := ToMapStringString(ts)
m["Test"] = "4"
out, err := FromMapStringString(m, ts)
if err != nil {
t.Error("Error converting to struct: ", err)
}
out := FromMapStringString(m, ts)
nts := out.(TestStruct)
if !reflect.DeepEqual(ts, &nts) {
t.Log(m)
@@ -78,10 +68,7 @@ func TestStructExtraFields(t *testing.T) {
"k3": "v3",
},
}
efMap, err := GetMapExtraFields(ts, "ExtraFields")
if err != nil {
t.Error("Error getting extra fields: ", err)
}
efMap := GetMapExtraFields(ts, "ExtraFields")
if !reflect.DeepEqual(efMap, ts.ExtraFields) {
t.Errorf("expected: %v got: %v", ts.ExtraFields, efMap)