14 Star 51 Fork 12

Hprose / hprose-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
int_decoder.go 3.20 KB
一键复制 编辑 原始数据 按行查看 历史
Hprose 提交于 2016-10-31 21:45 . Update to hprose 2.0
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* io/int_decoder.go *
* *
* hprose int decoder for Go. *
* *
* LastModified: Oct 15, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package io
import (
"errors"
"reflect"
"strconv"
"time"
"github.com/hprose/hprose-golang/util"
)
func readInt64(r *Reader) int64 {
return r.readInt64(TagSemicolon)
}
func readFloat64AsInt(r *Reader) int64 {
return int64(r.readFloat64())
}
func stringToInt(s string) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
panic(err)
}
return i
}
func readUTF8CharAsInt(r *Reader) int64 {
return stringToInt(util.ByteString(r.readUTF8Slice(1)))
}
func readStringAsInt(r *Reader) int64 {
return stringToInt(r.ReadStringWithoutTag())
}
func readDateTimeAsInt(r *Reader) int64 {
return r.ReadDateTimeWithoutTag().UnixNano()
}
func readTimeAsInt(r *Reader) int64 {
return r.ReadTimeWithoutTag().UnixNano()
}
func readRefAsInt(r *Reader) int64 {
ref := r.readRef()
if str, ok := ref.(string); ok {
return stringToInt(str)
}
if t, ok := ref.(*time.Time); ok {
return t.UnixNano()
}
panic(errors.New("value of type " +
reflect.TypeOf(ref).String() +
" cannot be converted to type int64"))
}
var intDecoders = [256]func(r *Reader) int64{
'0': func(r *Reader) int64 { return 0 },
'1': func(r *Reader) int64 { return 1 },
'2': func(r *Reader) int64 { return 2 },
'3': func(r *Reader) int64 { return 3 },
'4': func(r *Reader) int64 { return 4 },
'5': func(r *Reader) int64 { return 5 },
'6': func(r *Reader) int64 { return 6 },
'7': func(r *Reader) int64 { return 7 },
'8': func(r *Reader) int64 { return 8 },
'9': func(r *Reader) int64 { return 9 },
TagNull: func(r *Reader) int64 { return 0 },
TagEmpty: func(r *Reader) int64 { return 0 },
TagFalse: func(r *Reader) int64 { return 0 },
TagTrue: func(r *Reader) int64 { return 1 },
TagInteger: readInt64,
TagLong: readInt64,
TagDouble: readFloat64AsInt,
TagUTF8Char: readUTF8CharAsInt,
TagString: readStringAsInt,
TagDate: readDateTimeAsInt,
TagTime: readTimeAsInt,
TagRef: readRefAsInt,
}
func intDecoder(r *Reader, v reflect.Value, tag byte) {
decoder := intDecoders[tag]
if decoder != nil {
v.SetInt(decoder(r))
return
}
castError(tag, "int64")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/andot/hprose-go.git
git@gitee.com:andot/hprose-go.git
andot
hprose-go
hprose-go
v2.0.2

搜索帮助

344bd9b3 5694891 D2dac590 5694891