misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
evilsocket 2018-02-22 21:52:37 +01:00
commit 7c2dc38819
4 changed files with 43 additions and 31 deletions

View file

@ -2,7 +2,12 @@ package network
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
"github.com/evilsocket/bettercap-ng/core"
)
type Meta struct {
@ -43,6 +48,29 @@ func (m *Meta) Get(name string) interface{} {
return ""
}
func (m *Meta) GetIntsWith(name string, with int, sorted bool) []int {
sints := strings.Split(m.Get(name).(string), ",")
ints := []int{with}
for _, s := range sints {
n, err := strconv.Atoi(s)
if err == nil {
ints = append(ints, n)
}
}
return core.UniqueInts(ints, sorted)
}
func (m *Meta) SetInts(name string, ints []int) {
list := make([]string, len(ints))
for i, n := range ints {
list[i] = fmt.Sprintf("%d", n)
}
m.Set(name, strings.Join(list, ","))
}
func (m *Meta) GetOr(name string, dflt interface{}) interface{} {
m.Lock()
defer m.Unlock()