Improve session unmarshal

This commit is contained in:
nemunaire 2020-07-18 00:23:35 +02:00
parent a169d200ff
commit bfcff4220d

View File

@ -33,14 +33,17 @@ package happydns
import ( import (
"crypto/rand" "crypto/rand"
"encoding/json"
"fmt"
mrand "math/rand"
"time" "time"
) )
type Session struct { type Session struct {
Id []byte `json:"id"` Id []byte `json:"id"`
IdUser int64 `json:"login"` IdUser int64 `json:"login"`
Time time.Time `json:"time"` Time time.Time `json:"time"`
Content map[string]interface{} `json:"content,omitempty"` Content map[string][]byte `json:"content,omitempty"`
changed bool changed bool
} }
@ -62,9 +65,22 @@ func (s *Session) HasChanged() bool {
return s.changed return s.changed
} }
func (s *Session) FindNewKey(prefix string) (key string, id int64) {
for {
// max random id is 2^53 to fit on float64 without loosing precision (JSON limitation)
id = mrand.Int63n(1 << 53)
key = fmt.Sprintf("%s%d", prefix, id)
if _, ok := s.Content[key]; !ok {
return
}
}
return
}
func (s *Session) SetValue(key string, value interface{}) { func (s *Session) SetValue(key string, value interface{}) {
if s.Content == nil && value != nil { if s.Content == nil && value != nil {
s.Content = map[string]interface{}{} s.Content = map[string][]byte{}
} }
if value == nil { if value == nil {
@ -77,14 +93,19 @@ func (s *Session) SetValue(key string, value interface{}) {
s.changed = true s.changed = true
} }
} else { } else {
s.Content[key] = value s.Content[key], _ = json.Marshal(value)
s.changed = true s.changed = true
} }
} }
func (s *Session) GetValue(key string, value interface{}) (ok bool) { func (s *Session) GetValue(key string, value interface{}) bool {
value, ok = s.Content[key] if v, ok := s.Content[key]; !ok {
return return false
} else if json.Unmarshal(v, value) != nil {
return false
} else {
return true
}
} }
func (s *Session) DropKey(key string) { func (s *Session) DropKey(key string) {