Initial QSfera import
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Buffer struct {
|
||||
Data []byte
|
||||
B1 []byte
|
||||
B2 []byte
|
||||
B4 []byte
|
||||
B8 []byte
|
||||
B16 []byte
|
||||
offset int
|
||||
}
|
||||
|
||||
func (b *Buffer) Write(w io.Writer, vs ...byte) error {
|
||||
if len(b.Data) < b.offset+len(vs) {
|
||||
_, err := w.Write(b.Data[:b.offset])
|
||||
b.offset = 0
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(b.Data) < len(vs) {
|
||||
b.Data = append(b.Data, make([]byte, len(vs)-len(b.Data))...)
|
||||
}
|
||||
}
|
||||
for i := range vs {
|
||||
b.Data[b.offset+i] = vs[i]
|
||||
}
|
||||
b.offset += len(vs)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Buffer) Flush(w io.Writer) error {
|
||||
_, err := w.Write(b.Data[:b.offset])
|
||||
return err
|
||||
}
|
||||
|
||||
var bufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
data := make([]byte, 64)
|
||||
return &Buffer{
|
||||
Data: data,
|
||||
B1: data[:1],
|
||||
B2: data[:2],
|
||||
B4: data[:4],
|
||||
B8: data[:8],
|
||||
B16: data[:16],
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func GetBuffer() *Buffer {
|
||||
buf := bufPool.Get().(*Buffer)
|
||||
buf.offset = 0
|
||||
return buf
|
||||
}
|
||||
|
||||
func PutBuffer(buf *Buffer) {
|
||||
bufPool.Put(buf)
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Common is used encoding/decoding
|
||||
type Common struct {
|
||||
}
|
||||
|
||||
// CheckField returns flag whether should encode/decode or not and field name
|
||||
func (c *Common) CheckField(field reflect.StructField) (public, omit bool, name string) {
|
||||
// A to Z
|
||||
if !c.isPublic(field.Name) {
|
||||
return false, false, ""
|
||||
}
|
||||
tag := field.Tag.Get("msgpack")
|
||||
if tag == "" {
|
||||
return true, false, field.Name
|
||||
}
|
||||
|
||||
parts := strings.Split(tag, ",")
|
||||
// check ignore
|
||||
if parts[0] == "-" {
|
||||
return false, false, ""
|
||||
}
|
||||
// check omitempty
|
||||
for _, part := range parts[1:] {
|
||||
if part == "omitempty" {
|
||||
omit = true
|
||||
}
|
||||
}
|
||||
// check name
|
||||
name = field.Name
|
||||
if parts[0] != "" {
|
||||
name = parts[0]
|
||||
}
|
||||
return true, omit, name
|
||||
}
|
||||
|
||||
func (c *Common) isPublic(name string) bool {
|
||||
return 0x41 <= name[0] && name[0] <= 0x5a
|
||||
}
|
||||
Reference in New Issue
Block a user