-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
98 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
package match | ||
|
||
import ( | ||
"github.com/MoonSHRD/p2chat-android/pkg/utils" | ||
) | ||
|
||
// Response alias is used for json marshaling | ||
// the match ([topic]=>[mxIDs]) response | ||
// the whole match ([topic]=>[matrixIDs]) response | ||
// or for the new matches ([matrixID]=>[topics]) | ||
type Response map[string][]string | ||
|
||
// MatchProcessor is helper type for work | ||
// with match operations processing | ||
type MatchProcessor struct { | ||
mathes Response | ||
newMatchesQueue utils.Queue | ||
} | ||
|
||
// GetAllMatches returns the whole matches map within json format | ||
func (mp *MatchProcessor) GetAllMatches() string { | ||
return utils.ObjectToJSON(mp.mathes) | ||
} | ||
|
||
// GetNewMatch returns new match from the matches-queue | ||
func (mp *MatchProcessor) GetNewMatch() string { | ||
return utils.ObjectToJSON(mp.newMatchesQueue.PopBack()) | ||
} | ||
|
||
// AddNewMatch pushes new match [topic]=>[newMatrixID] to the matches-queue and map | ||
func (mp *MatchProcessor) AddNewMatch(matrixID string, topics []string) { | ||
for _, topic := range topics { | ||
mp.mathes[topic] = append(mp.mathes[topic], matrixID) | ||
} | ||
mp.newMatchesQueue.PushBack(Response{matrixID: topics}) | ||
} |
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,16 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
) | ||
|
||
// ObjectToJSON converts any object to json-string | ||
func ObjectToJSON(v interface{}) string { | ||
json, err := json.Marshal(v) | ||
if err != nil { | ||
log.Println(err.Error()) | ||
return "" | ||
} | ||
return string(json) | ||
} |