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.
ntool/nsys/cmdn/serializer_plain.go

49 lines
950 B
Go

package cmdn
import (
"errors"
"fmt"
"git.noahlan.cn/noahlan/ntool/ndef"
"runtime"
"strings"
)
type PlainSerializer struct {
sysType string
}
func NewPlainSerializer() ndef.Serializer {
return &PlainSerializer{
sysType: runtime.GOOS,
}
}
func (s *PlainSerializer) Marshal(v any) ([]byte, error) {
ret, ok := v.(*PlainCommand)
if !ok {
return nil, errors.New(fmt.Sprintf("参数类型必须为 %T", PlainCommand{}))
}
// ret arg0 arg1 arg2 ...
// cmd arg0 arg1 arg2 ...
sb := strings.Builder{}
if ret.ID != "" {
sb.WriteString(ret.ID)
sb.WriteString(" ")
}
sb.WriteString(ret.Cmd)
sb.WriteString(" ")
sb.WriteString(strings.Join(ret.Args, " "))
return []byte(sb.String()), nil
}
func (s *PlainSerializer) Unmarshal(data []byte, v any) error {
t, ok := v.(*PlainResp)
if !ok {
return errors.New(fmt.Sprintf("参数类型必须为 %T", PlainResp{}))
}
t.ID = ""
//t.Command
t.Result = string(data)
return nil
}