Skip to content

Commit

Permalink
add Strength option by Functional Options Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchennn committed Aug 7, 2023
1 parent 2e12095 commit a3b530a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
6 changes: 3 additions & 3 deletions fsm/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func NewFSM(transitions Transitions, callbacks Callbacks) (*FSM, error) {

// SendEvent triggers a callback with an event, and do transition after callback if need
// There are 3 types of callback:
// - on exit callback: call when fsm leave one state, with ExitEvent event
// - event callback: call when user trigger a user-defined event
// - on entry callback: call when fsm enter one state, with EntryEvent event
// - on exit callback: call when fsm leave one state, with ExitEvent event
// - event callback: call when user trigger a user-defined event
// - on entry callback: call when fsm enter one state, with EntryEvent event
func (fsm *FSM) SendEvent(state *State, event EventType, args ArgsType, log *logrus.Entry) error {
key := eventKey{
From: state.Current(),
Expand Down
2 changes: 1 addition & 1 deletion idgenerator/idgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (idGenerator *IDGenerator) Allocate() (int64, error) {
}

// param:
// - id: id to free
// - id: id to free
func (idGenerator *IDGenerator) FreeID(id int64) {
if id < idGenerator.minValue || id > idGenerator.maxValue {
return
Expand Down
41 changes: 33 additions & 8 deletions mongoapi/mongoapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ func SetMongoDB(setdbName string, url string) error {
return nil
}

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) {
var result map[string]interface{}
if err := collection.FindOne(context.TODO(), filter).Decode(&result); err != nil {
opts := new(options.FindOneOptions)
if len(argOpt) > 0 {
myCollation := &options.Collation{Locale: "en_US", Strength: argOpt[0]}
opts.SetCollation(myCollation)
}

if err := collection.FindOne(context.TODO(), filter, opts).Decode(&result); err != nil {
// ErrNoDocuments means that the filter did not match any documents in
// the collection.
if err == mongo.ErrNoDocuments {
Expand All @@ -45,8 +51,15 @@ func findOneAndDecode(collection *mongo.Collection, filter bson.M) (map[string]i
return result, 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, argOpt ...int) (
result map[string]interface{}, err error,
) {
if len(argOpt) == 0 {
result, err = findOneAndDecode(collection, filter)
} else {
result, err = findOneAndDecode(collection, filter, argOpt[0])
}

if err != nil {
return nil, err
}
Expand All @@ -68,21 +81,33 @@ func checkDataExisted(collection *mongo.Collection, filter bson.M) (bool, error)
return true, nil
}

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) {
collection := Client.Database(dbName).Collection(collName)
result, err := getOrigData(collection, filter)
if len(argOpt) == 0 {
result, err = getOrigData(collection, filter)
} else {
result, err = getOrigData(collection, filter, argOpt[0])
}

if err != nil {
return nil, fmt.Errorf("RestfulAPIGetOne err: %+v", err)
}
return result, nil
}

func RestfulAPIGetMany(collName string, filter bson.M) ([]map[string]interface{}, error) {
func RestfulAPIGetMany(collName string, filter bson.M, argOpt ...int) ([]map[string]interface{}, error) {
collection := Client.Database(dbName).Collection(collName)

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cur, err := collection.Find(ctx, filter)

opts := new(options.FindOptions)
if len(argOpt) > 0 {
myCollation := &options.Collation{Locale: "en_US", Strength: argOpt[0]}
opts.SetCollation(myCollation)
}

cur, err := collection.Find(ctx, filter, opts)
if err != nil {
return nil, fmt.Errorf("RestfulAPIGetMany err: %+v", err)
}
Expand Down

0 comments on commit a3b530a

Please sign in to comment.