Skip to content

Conversation

@brianchennn
Copy link
Contributor

This PR is for SD value case insensitive purpose.

}

func findOneAndDecode(collection *mongo.Collection, filter bson.M) (map[string]interface{}, error) {
func findOneAndDecode(collection *mongo.Collection, filter bson.M, argOpt ...int) (map[string]interface{}, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func findOneAndDecode(collection *mongo.Collection, filter bson.M, strength int) (map[string]interface{}, error) {


func getOrigData(collection *mongo.Collection, filter bson.M) (map[string]interface{}, error) {
result, err := findOneAndDecode(collection, filter)
func getOrigData(collection *mongo.Collection, filter bson.M, argOpt ...int) (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func getOrigData(collection *mongo.Collection, filter bson.M, strength int) (
	result map[string]interface{}, err error,
)

}

func RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) {
func RestfulAPIGetMany(collName string, filter bson.M, argOpt ...int) ([]map[string]interface{}, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add

func RestfulAPIGetManyWithStrength(collName string, filter bson.M, strength int) ([]map[string]interface{}, error) {

Keep

func RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) {

}

func findOneAndDecode(collection *mongo.Collection, filter bson.M) (map[string]interface{}, error) {
func findOneAndDecode(collection *mongo.Collection, filter bson.M, strength int) (map[string]interface{}, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func findOneAndDecode(collection *mongo.Collection, filter bson.M, argOpt ...interface{}) (map[string]interface{}, error) {
	var result map[string]interface{}
	opts := new(options.FindOneOptions)
	if len(argOpt) > 0 {
		strength, ok := argOpt[0].(int)
		if !ok {
			return nil, fmt.Errorf("argOpt[0] type is not int")
		}
		myCollation := &options.Collation{Locale: "en_US", Strength: strength}
		opts.SetCollation(myCollation)
	}

	if err := collection.FindOne(context.TODO(), filter, opts).Decode(&result); err != nil {
	...


func getOrigData(collection *mongo.Collection, filter bson.M) (map[string]interface{}, error) {
result, err := findOneAndDecode(collection, filter)
func getOrigData(collection *mongo.Collection, filter bson.M, strength int) (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func getOrigData(collection *mongo.Collection, filter bson.M, argOpt ...interface{}) (
	result map[string]interface{}, err error,
) {
	result, err = findOneAndDecode(collection, filter, argOpt...)
	if err != nil {
		return nil, err
	}
	...


func checkDataExisted(collection *mongo.Collection, filter bson.M) (bool, error) {
result, err := findOneAndDecode(collection, filter)
func checkDataExistedWithStrength(collection *mongo.Collection, filter bson.M, strength int) (bool, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func checkDataExisted(collection *mongo.Collection, filter bson.M, argOpt ...interface{}) (bool, error) {
	result, err := findOneAndDecode(collection, filter, argOpt...)
	if err != nil {

return RestfulAPIGetOneWithStrength(collName, filter, 3)
}

func RestfulAPIGetOneWithStrength(collName string, filter bson.M, strength int) (
Copy link
Collaborator

@tim-ywliu tim-ywliu Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// argOpt[0] is strength (type int)
func RestfulAPIGetOneWithArg(collName string, filter bson.M, argOpt ...interface{}) (
	result map[string]interface{}, err error,
) {
	collection := Client.Database(dbName).Collection(collName)
	result, err = getOrigData(collection, filter, argOpt...)
	...

@brianchennn brianchennn force-pushed the main branch 2 times, most recently from afb1211 to 61da5d7 Compare August 9, 2023 08:12
return RestfulAPIGetOneWithStrength(collName, filter, 3)
}

func RestfulAPIGetOneWithStrength(collName string, filter bson.M, argOpt ...interface{}) (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to RestfulAPIGetOneWithArg()

return RestfulAPIGetManyWithStrength(collName, filter, 3)
}

func RestfulAPIGetManyWithStrength(collName string, filter bson.M, strength int) ([]map[string]interface{}, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func RestfulAPIGetManyWithArg(collName string, filter bson.M, argOpt ...interface{}) ([]map[string]interface{}, error) {

}

func RestfulAPIGetOne(collName string, filter bson.M) (map[string]interface{}, error) {
func RestfulAPIGetOne(collName string, filter bson.M, argOpt ...int) (result map[string]interface{}, err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func RestfulAPIGetOne(collName string, filter bson.M) (result map[string]interface{}, err error) {
	return RestfulAPIGetOneWithArg(collName, filter)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brianchennn miss this function to change

}

func RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) {
return RestfulAPIGetManyWithStrength(collName, filter, 3)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return RestfulAPIGetManyWithStrength(collName, filter)

Copy link
Collaborator

@tim-ywliu tim-ywliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace all function name to xxxWithArg(..., argOpt ...interface{})

}

func findOneAndDecode(collection *mongo.Collection, filter bson.M) (map[string]interface{}, error) {
func findOneAndDecode(collection *mongo.Collection, filter bson.M, argOpt ...interface{}) (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add

func getCollation(argOpt ...interface{}) *options.Collation {
	if len(argOpt) == 0 {
		return nil
	}
	strength, ok := argOpt[0].(int)
	if !ok {
		return nil
	}
	// Strength 2: Case insensitive, 3: Case sensitive (default)
	return &options.Collation{Locale: "en_US", Strength: strength}
}

) {
var result map[string]interface{}
if err := collection.FindOne(context.TODO(), filter).Decode(&result); err != nil {
opts := new(options.FindOneOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modify to

	var err error
	collation := getCollation(argOpt...)
	if collation != nil {
		opts := new(options.FindOneOptions)
		opts.SetCollation(collation)
		err = collection.FindOne(context.TODO(), filter, opts).Decode(&result)
	} else {
		err = collection.FindOne(context.TODO(), filter).Decode(&result)
	}

	if err != nil {
		// ErrNoDocuments means that the filter did not match any documents in
		// the collection.
		if err == mongo.ErrNoDocuments {
			return nil, nil
		}
		return nil, err
	}
	return result, nil

defer cancel()
cur, err := collection.Find(ctx, filter)

opts := new(options.FindOptions)
Copy link
Collaborator

@tim-ywliu tim-ywliu Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modify to

	var cur XXX
	var err error
	collation := getCollation(argOpt...)
	if collation != nil {
		opts := new(options.FindOptions)
		opts.SetCollation(collation)
		cur, err = collection.Find(ctx, filter, opts)
	} else {
		cur, err = collection.Find(ctx, filter)
	}

	if err != nil {
		...
	}

collection := Client.Database(dbName).Collection(collName)
if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$pull": putData}); err != nil {

opts := new(options.UpdateOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collection := Client.Database(dbName).Collection(collName)

if _, err := collection.DeleteOne(context.TODO(), filter); err != nil {
opts := new(options.DeleteOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collection := Client.Database(dbName).Collection(collName)

if _, err := collection.DeleteMany(context.TODO(), filter); err != nil {
opts := new(options.DeleteOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData}); err != nil {

opts := new(options.UpdateOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData}); err != nil {

opts := new(options.UpdateOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": bson.M{dataName: modifiedData}}); err != nil {

opts := new(options.UpdateOptions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tim-ywliu tim-ywliu merged commit e726373 into free5gc:main Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants