add func convert #8

Merged
sinhluu merged 3 commits from add-convert into master 2022-12-14 10:07:24 +00:00
1 changed files with 1 additions and 41 deletions
Showing only changes of commit 6dc437b245 - Show all commits

View File

@ -1,18 +1,11 @@
package mongodb package mongodb
import ( import (
"encoding/json"
"github.com/thoas/go-funk" "github.com/thoas/go-funk"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"sort" "sort"
) )
// GetAppIDFromHex ...
func GetAppIDFromHex(s string) primitive.ObjectID {
id, _ := primitive.ObjectIDFromHex(s)
return id
}
// ConvertObjectIDsToStrings ... // ConvertObjectIDsToStrings ...
func ConvertObjectIDsToStrings(ids []primitive.ObjectID) []string { func ConvertObjectIDsToStrings(ids []primitive.ObjectID) []string {
return funk.Map(ids, func(item primitive.ObjectID) string { return funk.Map(ids, func(item primitive.ObjectID) string {
@ -30,7 +23,7 @@ func UniqObjectIds(ids []primitive.ObjectID) []primitive.ObjectID {
// ConvertStringsToObjectIDs ... // ConvertStringsToObjectIDs ...
func ConvertStringsToObjectIDs(strValues []string) []primitive.ObjectID { func ConvertStringsToObjectIDs(strValues []string) []primitive.ObjectID {
return funk.Map(strValues, func(item string) primitive.ObjectID { return funk.Map(strValues, func(item string) primitive.ObjectID {
return GetAppIDFromHex(item) return ConvertStringToObjectID(item)
}).([]primitive.ObjectID) }).([]primitive.ObjectID)
} }
@ -47,36 +40,3 @@ func SortObjectId(ids []primitive.ObjectID) []primitive.ObjectID {
}) })
return ids return ids
} }
// ConvertInterfacesToObjectIDs ...
func ConvertInterfacesToObjectIDs(values []interface{}) []primitive.ObjectID {
s := make([]primitive.ObjectID, len(values))
for i, v := range values {
s[i] = v.(primitive.ObjectID)
}
return s
}
// ConvertInterfacesToStrings ...
func ConvertInterfacesToStrings(values []interface{}) []string {
s := make([]string, len(values))
for i, v := range values {
s[i] = v.(string)
}
return s
}
// ConvertStringsToInterfaces ...
func ConvertStringsToInterfaces(values []string) []interface{} {
s := make([]interface{}, len(values))
for _, v := range values {
s = append(s, v)
}
return s
}
// ConvertToJSONString ...
func ConvertToJSONString(data interface{}) string {
dataByte, _ := json.Marshal(data)
return string(dataByte)
}