-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1、格式化import的路径 2、预分配len(ids)的内存 3、封装unmarshal和marshal方法
- Loading branch information
彭业昌
committed
Feb 14, 2023
1 parent
598fb9e
commit 1311c5c
Showing
6 changed files
with
48 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
package xfreecache | ||
|
||
import ( | ||
"fmt" | ||
|
||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package xfreecache | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
|
||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// 反序列化,如果是pb格式,则使用proto序列化 | ||
func unmarshal[T any](body []byte) (value T, err error) { | ||
if msg, ok := any(value).(proto.Message); ok { // Constrained to proto.Message | ||
// Peek the type inside T (as T= *SomeProtoMsgType) | ||
msgType := reflect.TypeOf(msg).Elem() | ||
|
||
// Make a new one, and throw it back into T | ||
msg = reflect.New(msgType).Interface().(proto.Message) | ||
|
||
err = proto.Unmarshal(body, msg) | ||
value = msg.(T) | ||
} else { | ||
err = json.Unmarshal(body, &value) | ||
} | ||
return | ||
} | ||
|
||
// 序列化,如果是pb格式,则使用proto序列化 | ||
func marshal[T any](cacheData T) (data []byte, err error) { | ||
if msg, ok := any(cacheData).(proto.Message); ok { | ||
data, err = proto.Marshal(msg) | ||
} else { | ||
data, err = json.Marshal(cacheData) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters