forked from p93542168/wheat-cache
70 lines
1.5 KiB
Plaintext
70 lines
1.5 KiB
Plaintext
// Code generated by gen-struct. DO NOT EDIT.
|
|
// make gen-service generated
|
|
package persisted
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
|
|
"gitee.com/wheat-os/wheatCache/pkg/errorx"
|
|
"gitee.com/wheat-os/wheatCache/pkg/proto"
|
|
protobuf "google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type codecProto struct {
|
|
}
|
|
|
|
func (c *codecProto) Encode(method string, m protobuf.Message) ([]byte, error) {
|
|
buf, err := protobuf.Marshal(m)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 16 进制编码
|
|
dst := make([]byte, hex.EncodedLen(len(buf)))
|
|
n := hex.Encode(dst, buf)
|
|
buffer := bytes.NewBuffer(dst[:n])
|
|
buffer.WriteString(fmt.Sprint(":", method))
|
|
|
|
return buffer.Bytes(), nil
|
|
}
|
|
|
|
func (c *codecProto) Decode(buf []byte) (protobuf.Message, error) {
|
|
hexBuf := bytes.Split(buf, []byte(":"))
|
|
if len(hexBuf) != 2 {
|
|
return nil, errorx.New("decode aof err: not a valid aof")
|
|
}
|
|
|
|
method := string(hexBuf[1])
|
|
n, err := hex.Decode(buf, hexBuf[0])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return decode(method, buf[:n])
|
|
|
|
}
|
|
|
|
func decode(method string, buf []byte) (protobuf.Message, error) {
|
|
switch method {
|
|
{% for key in keys -%}
|
|
case "{{key.method}}":
|
|
return decode{{key.method}}(buf)
|
|
{% endfor %}
|
|
}
|
|
|
|
return nil, errorx.New("decode aof err, the method could not be resolved, method:%s", method)
|
|
}
|
|
|
|
{% for key in keys %}
|
|
func decode{{key.method}}(buf []byte) (*proto.{{key.method}}Request, error) {
|
|
req := &proto.{{key.method}}Request{}
|
|
err := protobuf.Unmarshal(buf, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return req, nil
|
|
}
|
|
{% endfor %}
|