fix #88
This commit is contained in:
@ -33,7 +33,7 @@ type jsonChat struct {
|
|||||||
|
|
||||||
Translate string `json:"translate,omitempty"`
|
Translate string `json:"translate,omitempty"`
|
||||||
With []json.RawMessage `json:"with,omitempty"` // How can go handle an JSON array with Object and String?
|
With []json.RawMessage `json:"with,omitempty"` // How can go handle an JSON array with Object and String?
|
||||||
Extra []jsonChat `json:"extra,omitempty"`
|
Extra []Message `json:"extra,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//UnmarshalJSON decode json to Message
|
//UnmarshalJSON decode json to Message
|
||||||
@ -73,12 +73,12 @@ func (m *Message) Append(extraMsg ...Message) {
|
|||||||
finalLen := origLen + len(extraMsg)
|
finalLen := origLen + len(extraMsg)
|
||||||
if cap(m.Extra) < len(m.Extra)+len(extraMsg) {
|
if cap(m.Extra) < len(m.Extra)+len(extraMsg) {
|
||||||
// pre expansion
|
// pre expansion
|
||||||
extra := make([]jsonChat, finalLen)
|
extra := make([]Message, finalLen)
|
||||||
copy(extra, m.Extra)
|
copy(extra, m.Extra)
|
||||||
m.Extra = extra
|
m.Extra = extra
|
||||||
}
|
}
|
||||||
for _, v := range extraMsg {
|
for _, v := range extraMsg {
|
||||||
m.Extra = append(m.Extra, jsonChat(v))
|
m.Extra = append(m.Extra, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +34,8 @@ var jsons = []string{
|
|||||||
`"Tnze"`,
|
`"Tnze"`,
|
||||||
`"§0Tnze"`,
|
`"§0Tnze"`,
|
||||||
`"§list"`,
|
`"§list"`,
|
||||||
|
|
||||||
|
`{"extra":[" "],"text":""}`,
|
||||||
}
|
}
|
||||||
|
|
||||||
var texts = []string{
|
var texts = []string{
|
||||||
@ -49,6 +51,8 @@ var texts = []string{
|
|||||||
"Tnze",
|
"Tnze",
|
||||||
"\033[30mTnze\033[0m",
|
"\033[30mTnze\033[0m",
|
||||||
"\033[1mist\033[0m",
|
"\033[1mist\033[0m",
|
||||||
|
|
||||||
|
" ",
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctexts = []string{
|
var ctexts = []string{
|
||||||
@ -64,6 +68,8 @@ var ctexts = []string{
|
|||||||
"Tnze",
|
"Tnze",
|
||||||
"Tnze",
|
"Tnze",
|
||||||
"ist",
|
"ist",
|
||||||
|
|
||||||
|
" ",
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChatMsgFormatString(t *testing.T) {
|
func TestChatMsgFormatString(t *testing.T) {
|
||||||
|
51
net/packet/util.go
Normal file
51
net/packet/util.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package packet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Ary struct {
|
||||||
|
Len Field
|
||||||
|
Ary interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Ary) Encode() (data []byte) {
|
||||||
|
length := int(reflect.ValueOf(a.Len).Int())
|
||||||
|
array := reflect.ValueOf(a.Ary).Elem()
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
elem := array.Index(i)
|
||||||
|
data = append(data, elem.Interface().(FieldEncoder).Encode()...)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a Ary) Decode(r DecodeReader) error {
|
||||||
|
length := int(reflect.ValueOf(a.Len).Int())
|
||||||
|
array := reflect.ValueOf(a.Ary).Elem()
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
elem := array.Index(i)
|
||||||
|
if err := elem.Interface().(FieldDecoder).Decode(r); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Opt struct {
|
||||||
|
Has func() bool
|
||||||
|
Field interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Opt) Encode() []byte {
|
||||||
|
if o.Has() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return o.Field.(FieldEncoder).Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Opt) Decode(r DecodeReader) error {
|
||||||
|
if o.Has() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return o.Field.(FieldDecoder).Decode(r)
|
||||||
|
}
|
Reference in New Issue
Block a user