-
Notifications
You must be signed in to change notification settings - Fork 19
add Strength option by Functional Options Pattern #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
mongoapi/mongoapi.go
Outdated
| } | ||
|
|
||
| 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) { |
There was a problem hiding this comment.
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) {
mongoapi/mongoapi.go
Outdated
|
|
||
| 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) ( |
There was a problem hiding this comment.
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,
)
mongoapi/mongoapi.go
Outdated
| } | ||
|
|
||
| func RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) { | ||
| func RestfulAPIGetMany(collName string, filter bson.M, argOpt ...int) ([]map[string]interface{}, error) { |
There was a problem hiding this comment.
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) {
mongoapi/mongoapi.go
Outdated
| } | ||
|
|
||
| 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) { |
There was a problem hiding this comment.
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 {
...
mongoapi/mongoapi.go
Outdated
|
|
||
| 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) ( |
There was a problem hiding this comment.
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
}
...
mongoapi/mongoapi.go
Outdated
|
|
||
| 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) { |
There was a problem hiding this comment.
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 {
mongoapi/mongoapi.go
Outdated
| return RestfulAPIGetOneWithStrength(collName, filter, 3) | ||
| } | ||
|
|
||
| func RestfulAPIGetOneWithStrength(collName string, filter bson.M, strength int) ( |
There was a problem hiding this comment.
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...)
...
afb1211 to
61da5d7
Compare
mongoapi/mongoapi.go
Outdated
| return RestfulAPIGetOneWithStrength(collName, filter, 3) | ||
| } | ||
|
|
||
| func RestfulAPIGetOneWithStrength(collName string, filter bson.M, argOpt ...interface{}) ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to RestfulAPIGetOneWithArg()
mongoapi/mongoapi.go
Outdated
| return RestfulAPIGetManyWithStrength(collName, filter, 3) | ||
| } | ||
|
|
||
| func RestfulAPIGetManyWithStrength(collName string, filter bson.M, strength int) ([]map[string]interface{}, error) { |
There was a problem hiding this comment.
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) {
mongoapi/mongoapi.go
Outdated
| } | ||
|
|
||
| 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) { |
There was a problem hiding this comment.
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)
}
There was a problem hiding this comment.
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
mongoapi/mongoapi.go
Outdated
| } | ||
|
|
||
| func RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) { | ||
| return RestfulAPIGetManyWithStrength(collName, filter, 3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return RestfulAPIGetManyWithStrength(collName, filter)
tim-ywliu
left a comment
There was a problem hiding this 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{}) ( |
There was a problem hiding this comment.
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}
}
mongoapi/mongoapi.go
Outdated
| ) { | ||
| var result map[string]interface{} | ||
| if err := collection.FindOne(context.TODO(), filter).Decode(&result); err != nil { | ||
| opts := new(options.FindOneOptions) |
There was a problem hiding this comment.
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
mongoapi/mongoapi.go
Outdated
| defer cancel() | ||
| cur, err := collection.Find(ctx, filter) | ||
|
|
||
| opts := new(options.FindOptions) |
There was a problem hiding this comment.
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 {
...
}
mongoapi/mongoapi.go
Outdated
| collection := Client.Database(dbName).Collection(collName) | ||
| if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$pull": putData}); err != nil { | ||
|
|
||
| opts := new(options.UpdateOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mongoapi/mongoapi.go
Outdated
| collection := Client.Database(dbName).Collection(collName) | ||
|
|
||
| if _, err := collection.DeleteOne(context.TODO(), filter); err != nil { | ||
| opts := new(options.DeleteOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mongoapi/mongoapi.go
Outdated
| collection := Client.Database(dbName).Collection(collName) | ||
|
|
||
| if _, err := collection.DeleteMany(context.TODO(), filter); err != nil { | ||
| opts := new(options.DeleteOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mongoapi/mongoapi.go
Outdated
| } | ||
| if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData}); err != nil { | ||
|
|
||
| opts := new(options.UpdateOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mongoapi/mongoapi.go
Outdated
| } | ||
| if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": modifiedData}); err != nil { | ||
|
|
||
| opts := new(options.UpdateOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mongoapi/mongoapi.go
Outdated
| } | ||
| if _, err := collection.UpdateOne(context.TODO(), filter, bson.M{"$set": bson.M{dataName: modifiedData}}); err != nil { | ||
|
|
||
| opts := new(options.UpdateOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is for SD value case insensitive purpose.