-
Notifications
You must be signed in to change notification settings - Fork 12
/
datastore_key_value.go
84 lines (64 loc) · 2.33 KB
/
datastore_key_value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Package types implements all the types used by the DataStore protocol
package types
import (
"fmt"
"strings"
"github.com/PretendoNetwork/nex-go"
)
// DataStoreKeyValue is sent in the PrepareGetObject method
type DataStoreKeyValue struct {
nex.Structure
Key string
Value string
}
// Bytes encodes the DataStoreKeyValue and returns a byte array
func (dataStoreKeyValue *DataStoreKeyValue) Bytes(stream *nex.StreamOut) []byte {
stream.WriteString(dataStoreKeyValue.Key)
stream.WriteString(dataStoreKeyValue.Value)
return stream.Bytes()
}
// Copy returns a new copied instance of DataStoreKeyValue
func (dataStoreKeyValue *DataStoreKeyValue) Copy() nex.StructureInterface {
copied := NewDataStoreKeyValue()
copied.SetStructureVersion(dataStoreKeyValue.StructureVersion())
copied.Key = dataStoreKeyValue.Key
copied.Value = dataStoreKeyValue.Value
return copied
}
// Equals checks if the passed Structure contains the same data as the current instance
func (dataStoreKeyValue *DataStoreKeyValue) Equals(structure nex.StructureInterface) bool {
other := structure.(*DataStoreKeyValue)
if dataStoreKeyValue.StructureVersion() != other.StructureVersion() {
return false
}
if dataStoreKeyValue.Key != other.Key {
return false
}
if dataStoreKeyValue.Value != other.Value {
return false
}
return true
}
// String returns a string representation of the struct
func (dataStoreKeyValue *DataStoreKeyValue) String() string {
return dataStoreKeyValue.FormatToString(0)
}
// FormatToString pretty-prints the struct data using the provided indentation level
func (dataStoreKeyValue *DataStoreKeyValue) FormatToString(indentationLevel int) string {
indentationValues := strings.Repeat("\t", indentationLevel+1)
indentationEnd := strings.Repeat("\t", indentationLevel)
var b strings.Builder
b.WriteString("DataStoreKeyValue{\n")
b.WriteString(fmt.Sprintf("%sstructureVersion: %d,\n", indentationValues, dataStoreKeyValue.StructureVersion()))
b.WriteString(fmt.Sprintf("%sKey: %q,\n", indentationValues, dataStoreKeyValue.Key))
b.WriteString(fmt.Sprintf("%sValue: %q\n", indentationValues, dataStoreKeyValue.Value))
b.WriteString(fmt.Sprintf("%s}", indentationEnd))
return b.String()
}
// NewDataStoreKeyValue returns a new DataStoreKeyValue
func NewDataStoreKeyValue() *DataStoreKeyValue {
return &DataStoreKeyValue{
Key: "",
Value: "",
}
}