You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
993 B
Go
45 lines
993 B
Go
package codec
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.noahlan.cn/noahlan/ntool-biz/nkatago/req"
|
|
"git.noahlan.cn/noahlan/ntool-biz/nkatago/resp"
|
|
"git.noahlan.cn/noahlan/ntool/ndef"
|
|
"strings"
|
|
)
|
|
|
|
type GtpSerializer struct {
|
|
}
|
|
|
|
func NewGtpSerializer() ndef.Serializer {
|
|
return &GtpSerializer{}
|
|
}
|
|
|
|
func (s *GtpSerializer) Marshal(v interface{}) ([]byte, error) {
|
|
ret, ok := v.(*req.GtpReq)
|
|
if !ok {
|
|
return nil, errors.New(fmt.Sprintf("参数类型必须为 %T", req.GtpReq{}))
|
|
}
|
|
// ret arg0 arg1 arg2 ...
|
|
// cmd arg0 arg1 arg2 ...
|
|
sb := strings.Builder{}
|
|
if ret.ID != "" {
|
|
sb.WriteString(ret.ID)
|
|
sb.WriteString(" ")
|
|
}
|
|
sb.WriteString(ret.Command)
|
|
sb.WriteString(" ")
|
|
sb.WriteString(strings.Join(ret.Args, " "))
|
|
return []byte(sb.String()), nil
|
|
}
|
|
|
|
func (s *GtpSerializer) Unmarshal(data []byte, v interface{}) error {
|
|
t, ok := v.(*resp.GtpResponse)
|
|
if !ok {
|
|
return errors.New(fmt.Sprintf("参数类型必须为 %T", resp.GtpResponse{}))
|
|
}
|
|
t.SetContent(string(data))
|
|
return nil
|
|
}
|