初始化2
This commit is contained in:
26
server/dto/id_string.go
Normal file
26
server/dto/id_string.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// StringID 自定义ID类型, JSON序列化为字符串(兼容JS大整数)
|
||||
type StringID uint64
|
||||
|
||||
func (id StringID) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"` + strconv.FormatUint(uint64(id), 10) + `"`), nil
|
||||
}
|
||||
|
||||
func (id *StringID) UnmarshalJSON(data []byte) error {
|
||||
s, err := strconv.Unquote(string(data))
|
||||
if err != nil {
|
||||
return fmt.Errorf("StringID unmarshal: %w", err)
|
||||
}
|
||||
v, err := strconv.ParseUint(s, 10, 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("StringID parse: %w", err)
|
||||
}
|
||||
*id = StringID(v)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user