2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-08-26 13:15:48 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2017-09-23 01:03:27 +02:00
|
|
|
package mocks
|
|
|
|
|
2021-02-17 16:21:40 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2021-12-08 19:46:59 +08:00
|
|
|
"strconv"
|
2021-02-17 16:21:40 +01:00
|
|
|
|
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
)
|
2017-09-23 01:03:27 +02:00
|
|
|
|
2021-12-08 19:46:59 +08:00
|
|
|
const uuidLen = 36
|
|
|
|
|
2018-03-11 18:06:01 +01:00
|
|
|
// Since mocks will store data in map, and they need to resemble the real
|
|
|
|
// identifiers as much as possible, a key will be created as combination of
|
|
|
|
// owner and their own identifiers. This will allow searching either by
|
|
|
|
// prefix or suffix.
|
2018-12-05 13:09:25 +01:00
|
|
|
func key(owner string, id string) string {
|
|
|
|
return fmt.Sprintf("%s-%s", owner, id)
|
2017-09-23 01:03:27 +02:00
|
|
|
}
|
2021-02-17 16:21:40 +01:00
|
|
|
|
|
|
|
func sortThings(pm things.PageMetadata, ths []things.Thing) []things.Thing {
|
|
|
|
switch pm.Order {
|
|
|
|
case "name":
|
|
|
|
if pm.Dir == "asc" {
|
|
|
|
sort.SliceStable(ths, func(i, j int) bool {
|
|
|
|
return ths[i].Name < ths[j].Name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if pm.Dir == "desc" {
|
|
|
|
sort.SliceStable(ths, func(i, j int) bool {
|
|
|
|
return ths[i].Name > ths[j].Name
|
|
|
|
})
|
|
|
|
}
|
2021-03-11 10:28:44 +01:00
|
|
|
case "id":
|
|
|
|
if pm.Dir == "asc" {
|
|
|
|
sort.SliceStable(ths, func(i, j int) bool {
|
|
|
|
return ths[i].ID < ths[j].ID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if pm.Dir == "desc" {
|
|
|
|
sort.SliceStable(ths, func(i, j int) bool {
|
|
|
|
return ths[i].ID > ths[j].ID
|
|
|
|
})
|
|
|
|
}
|
2021-02-17 16:21:40 +01:00
|
|
|
default:
|
|
|
|
sort.SliceStable(ths, func(i, j int) bool {
|
|
|
|
return ths[i].ID < ths[j].ID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ths
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortChannels(pm things.PageMetadata, chs []things.Channel) []things.Channel {
|
|
|
|
switch pm.Order {
|
|
|
|
case "name":
|
|
|
|
if pm.Dir == "asc" {
|
|
|
|
sort.SliceStable(chs, func(i, j int) bool {
|
|
|
|
return chs[i].Name < chs[j].Name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if pm.Dir == "desc" {
|
|
|
|
sort.SliceStable(chs, func(i, j int) bool {
|
|
|
|
return chs[i].Name > chs[j].Name
|
|
|
|
})
|
|
|
|
}
|
2021-03-11 10:28:44 +01:00
|
|
|
case "id":
|
|
|
|
if pm.Dir == "asc" {
|
|
|
|
sort.SliceStable(chs, func(i, j int) bool {
|
|
|
|
return chs[i].ID < chs[j].ID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if pm.Dir == "desc" {
|
|
|
|
sort.SliceStable(chs, func(i, j int) bool {
|
|
|
|
return chs[i].ID > chs[j].ID
|
|
|
|
})
|
|
|
|
}
|
2021-02-17 16:21:40 +01:00
|
|
|
default:
|
|
|
|
sort.SliceStable(chs, func(i, j int) bool {
|
|
|
|
return chs[i].ID < chs[j].ID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return chs
|
|
|
|
}
|
2021-12-08 19:46:59 +08:00
|
|
|
|
|
|
|
func parseID(ID string) (id uint64) {
|
|
|
|
var serialNum string
|
|
|
|
|
|
|
|
if len(ID) == uuidLen {
|
|
|
|
serialNum = ID[len(ID)-6:]
|
|
|
|
}
|
|
|
|
id, _ = strconv.ParseUint(serialNum, 10, 64)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|