github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/queue/item.go (about) 1 package queue 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "encoding/gob" 7 "github.com/angenalZZZ/gofunc/f" 8 ) 9 10 // Item represents an entry in either a stack or queue. 11 type Item struct { 12 ID uint64 13 Key []byte 14 Value []byte 15 } 16 17 // ToString returns the item value as a string. 18 func (i *Item) ToString() string { 19 return string(i.Value) 20 } 21 22 // ToObject decodes the item value into the given value type using 23 // encoding/gob. 24 // 25 // The value passed to this method should be a pointer to a variable 26 // of the type you wish to decode into. The variable pointed to will 27 // hold the decoded object. 28 // 29 // Objects containing pointers with zero values will decode to nil 30 // when using this function. This is due to how the encoding/gob 31 // package works. Because of this, you should only use this function 32 // to decode simple types. 33 func (i *Item) ToObject(value interface{}) error { 34 buffer := bytes.NewBuffer(i.Value) 35 dec := gob.NewDecoder(buffer) 36 return dec.Decode(value) 37 } 38 39 // ToObjectFromJSON decodes the item value into the given value type 40 // using encoding/json. 41 // 42 // The value passed to this method should be a pointer to a variable 43 // of the type you wish to decode into. The variable pointed to will 44 // hold the decoded object. 45 func (i *Item) ToObjectFromJSON(value interface{}) error { 46 return f.DecodeJson(i.Value, value) 47 } 48 49 // PriorityItem represents an entry in a priority queue. 50 type PriorityItem struct { 51 ID uint64 52 Priority uint8 53 Key []byte 54 Value []byte 55 } 56 57 // ToString returns the priority item value as a string. 58 func (pi *PriorityItem) ToString() string { 59 return string(pi.Value) 60 } 61 62 // ToObject decodes the item value into the given value type using 63 // encoding/gob. 64 // 65 // The value passed to this method should be a pointer to a variable 66 // of the type you wish to decode into. The variable pointed to will 67 // hold the decoded object. 68 // 69 // Objects containing pointers with zero values will decode to nil 70 // when using this function. This is due to how the encoding/gob 71 // package works. Because of this, you should only use this function 72 // to decode simple types. 73 func (pi *PriorityItem) ToObject(value interface{}) error { 74 buffer := bytes.NewBuffer(pi.Value) 75 dec := gob.NewDecoder(buffer) 76 return dec.Decode(value) 77 } 78 79 // ToObjectFromJSON decodes the item value into the given value type 80 // using encoding/json. 81 // 82 // The value passed to this method should be a pointer to a variable 83 // of the type you wish to decode into. The variable pointed to will 84 // hold the decoded object. 85 func (pi *PriorityItem) ToObjectFromJSON(value interface{}) error { 86 return f.DecodeJson(pi.Value, value) 87 } 88 89 // idToKey converts and returns the given ID to a key. 90 func idToKey(id uint64) []byte { 91 key := make([]byte, 8) 92 binary.BigEndian.PutUint64(key, id) 93 return key 94 } 95 96 // keyToID converts and returns the given key to an ID. 97 func keyToID(key []byte) uint64 { 98 return binary.BigEndian.Uint64(key) 99 }