-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
|
||
"github.com/free5gc/openapi/models" | ||
) | ||
|
||
func SnssaisToBsonM(snssais string) []bson.M { | ||
snssaisString := strings.Trim(snssais, "{}") | ||
slicesStrings := strings.Split(snssaisString, "},{") | ||
|
||
var bsonMArray []bson.M | ||
for _, slice := range slicesStrings { | ||
slice = "{" + slice + "}" | ||
|
||
snssaiStruct := &models.Snssai{} | ||
err := json.Unmarshal([]byte(slice), snssaiStruct) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
snssaiBsonM := bson.M{} | ||
snssaiBsonM["sst"] = snssaiStruct.Sst | ||
if snssaiStruct.Sd != "" { | ||
snssaiBsonM["sd"] = snssaiStruct.Sd | ||
} | ||
|
||
bsonMArray = append(bsonMArray, snssaiBsonM) | ||
} | ||
return bsonMArray | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package util_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.mongodb.org/mongo-driver/bson" | ||
|
||
"github.com/free5gc/nrf/internal/util" | ||
) | ||
|
||
// type Snssai struct { | ||
// Sst int32 `json:"sst" yaml:"sst" bson:"sst" mapstructure:"Sst"` | ||
// Sd string `json:"sd,omitempty" yaml:"sd" bson:"sd" mapstructure:"Sd"` | ||
// } | ||
|
||
func TestSnssaisToBsonM(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
snssais string | ||
expectBsonM []bson.M | ||
}{ | ||
{ | ||
Name: "Default 01010203", | ||
snssais: "{\"sst\":1,\"sd\":\"010203\"}", | ||
expectBsonM: []bson.M{ | ||
{ | ||
"sst": int32(1), | ||
"sd": "010203", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Empty SD", | ||
snssais: "{\"sst\":1}", | ||
expectBsonM: []bson.M{ | ||
{ | ||
"sst": int32(1), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Two Slices", | ||
snssais: "{\"sst\":1,\"sd\":\"010203\"},{\"sst\":1,\"sd\":\"112233\"}", | ||
expectBsonM: []bson.M{ | ||
{ | ||
"sst": int32(1), | ||
"sd": "010203", | ||
}, | ||
{ | ||
"sst": int32(1), | ||
"sd": "112233", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
output := util.SnssaisToBsonM(tc.snssais) | ||
require.Equal(t, tc.expectBsonM, output) | ||
}) | ||
} | ||
} |