修复序列注册包不一致的问题
This commit is contained in:
parent
2514af413e
commit
c5be59e309
12
base.go
12
base.go
|
@ -77,7 +77,7 @@ type IKeyList interface {
|
|||
GetLength() int
|
||||
}
|
||||
|
||||
// SaveData 存数据
|
||||
// SaveData gob序列化存数据
|
||||
func SaveData(fname string, v interface{}) {
|
||||
var keybuf = &bytes.Buffer{}
|
||||
|
||||
|
@ -94,6 +94,7 @@ func SaveData(fname string, v interface{}) {
|
|||
gw.Write(keybuf.Bytes())
|
||||
}
|
||||
|
||||
// SaveGob 用于解析rocksdb源数据, 爬取数据存储在rocksdb后取出 序列化存储
|
||||
func SaveGob(fname string, datatype reflect.Type, kfs ...KeyKind) {
|
||||
|
||||
// cl := &CountryList{}
|
||||
|
@ -118,19 +119,12 @@ func SaveGob(fname string, datatype reflect.Type, kfs ...KeyKind) {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// t.Error(string(iter.Key().Data()), len(pic.Data()))
|
||||
// country.Pic = pic.Data()
|
||||
fv := data.FieldByName(kfs[n].FieldName)
|
||||
fv.Set(reflect.ValueOf(v.Data()))
|
||||
// cl.KeyList = append(cl.KeyList, country)
|
||||
|
||||
}
|
||||
kl.AppendKey(data.Interface())
|
||||
}
|
||||
// t.Error(i)
|
||||
// // SaveData("./data/country.gob", cl)
|
||||
SaveData(fname, kl)
|
||||
// t.Error()
|
||||
}
|
||||
|
||||
// LoadGob load gob from file
|
||||
|
@ -141,7 +135,7 @@ func LoadGob(fname string, v IKeyList) {
|
|||
CheckErrorPanic(err)
|
||||
reader := flate.NewReader(f)
|
||||
dec := gob.NewDecoder(reader)
|
||||
dec.Decode(v)
|
||||
CheckErrorPanic(dec.Decode(v))
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -6,10 +6,6 @@ import (
|
|||
|
||||
var buildinglist = &KeyList{}
|
||||
|
||||
func init() {
|
||||
// LoadGob("./data/building.gob", buildinglist)
|
||||
}
|
||||
|
||||
// UnimplementedBuildingServer can be embedded to have forward compatible implementations.
|
||||
type buildingserver struct {
|
||||
}
|
||||
|
|
14
main.go
14
main.go
|
@ -16,11 +16,12 @@ var cl *KeyList = &KeyList{}
|
|||
|
||||
func init() {
|
||||
|
||||
gob.Register(KeyList{})
|
||||
gob.Register(Country{})
|
||||
gob.Register(LastName{})
|
||||
gob.Register(FirstName{})
|
||||
gob.Register(NameCode{})
|
||||
// 注册序列化结构
|
||||
gob.RegisterName("workshop.KeyList", KeyList{})
|
||||
gob.RegisterName("workshop.Country", Country{})
|
||||
gob.RegisterName("workshop.LastName", LastName{})
|
||||
gob.RegisterName("workshop.FirstName", FirstName{})
|
||||
gob.RegisterName("workshop.NameCode", NameCode{})
|
||||
|
||||
f, err := os.OpenFile("./my.log", os.O_CREATE|os.O_RDWR, os.ModePerm)
|
||||
CheckErrorPanic(err)
|
||||
|
@ -28,6 +29,7 @@ func init() {
|
|||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// 加载序列化结构
|
||||
LoadGob("./data/building.gob", buildinglist)
|
||||
LoadGob("./data/country.gob", countrylist)
|
||||
|
||||
|
@ -36,6 +38,7 @@ func init() {
|
|||
|
||||
LoadGob("./data/province.gob", province)
|
||||
LoadGob("./data/ways.gob", ways)
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -47,6 +50,7 @@ func main() {
|
|||
// gserver := grpc.NewServer()
|
||||
mux := runtime.NewServeMux()
|
||||
|
||||
// 注册rpc http接口 根据protobuf协议生成
|
||||
RegisterNameHandlerServer(ctx, mux, &nameserver{})
|
||||
RegisterCountryHandlerServer(ctx, mux, &countryserver{})
|
||||
RegisterProvinceAreaCityHandlerServer(ctx, mux, &provinceserver{})
|
||||
|
|
19
main_test.go
19
main_test.go
|
@ -3,5 +3,24 @@ package main
|
|||
import "testing"
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
|
||||
// LoadGob("./data/building.gob", buildinglist)
|
||||
// SaveData("./data/building1.gob", buildinglist)
|
||||
|
||||
// LoadGob("./data/country.gob", countrylist)
|
||||
// SaveData("./data/country1.gob", countrylist)
|
||||
|
||||
// LoadGob("./data/firstname.gob", fnl)
|
||||
// SaveData("./data/firstname1.gob", fnl)
|
||||
|
||||
// LoadGob("./data/lastname.gob", lnl)
|
||||
// SaveData("./data/lastname1.gob", lnl)
|
||||
|
||||
// LoadGob("./data/province.gob", province)
|
||||
// SaveData("./data/province1.gob", province)
|
||||
|
||||
// LoadGob("./data/ways.gob", ways)
|
||||
// SaveData("./data/ways1.gob", ways)
|
||||
|
||||
main()
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ var lnl *KeyList = &KeyList{}
|
|||
type nameserver struct {
|
||||
}
|
||||
|
||||
// FirstName first name 首名(姓)
|
||||
func (s *nameserver) FirstName(cxt context.Context, request *Request) (*Reply, error) {
|
||||
|
||||
reply := &Reply{}
|
||||
|
@ -27,6 +28,7 @@ func (s *nameserver) FirstName(cxt context.Context, request *Request) (*Reply, e
|
|||
|
||||
}
|
||||
|
||||
// LastName last name 名字
|
||||
func (s *nameserver) LastName(cxt context.Context, request *Request) (*Reply, error) {
|
||||
reply := &Reply{}
|
||||
log.Println(len(lnl.Keys))
|
||||
|
@ -34,6 +36,7 @@ func (s *nameserver) LastName(cxt context.Context, request *Request) (*Reply, er
|
|||
return reply, nil
|
||||
}
|
||||
|
||||
// FullName 全名
|
||||
func (s *nameserver) FullName(cxt context.Context, request *Request) (*Reply, error) {
|
||||
reply := &Reply{}
|
||||
var fullname []byte
|
||||
|
|
|
@ -16,6 +16,7 @@ var province = &KeyList{}
|
|||
type provinceserver struct {
|
||||
}
|
||||
|
||||
// Province 省份 与 代码
|
||||
func (ps *provinceserver) Province(ctx context.Context, req *Request) (*NameCodeReply, error) {
|
||||
reply := &NameCodeReply{}
|
||||
|
||||
|
@ -26,6 +27,7 @@ func (ps *provinceserver) Province(ctx context.Context, req *Request) (*NameCode
|
|||
return reply, nil
|
||||
}
|
||||
|
||||
// Area 区(市, 悬) 与 代码
|
||||
func (ps *provinceserver) Area(ctx context.Context, req *Request) (*NameCodeReply, error) {
|
||||
reply := &NameCodeReply{}
|
||||
|
||||
|
@ -38,6 +40,7 @@ func (ps *provinceserver) Area(ctx context.Context, req *Request) (*NameCodeRepl
|
|||
return reply, nil
|
||||
}
|
||||
|
||||
// City 城市 与 代码
|
||||
func (ps *provinceserver) City(ctx context.Context, req *Request) (*NameCodeReply, error) {
|
||||
reply := &NameCodeReply{}
|
||||
|
||||
|
@ -51,6 +54,7 @@ func (ps *provinceserver) City(ctx context.Context, req *Request) (*NameCodeRepl
|
|||
return reply, nil
|
||||
}
|
||||
|
||||
// AreaParent 区域 与 代码 与 父类一起 返回
|
||||
func (ps *provinceserver) AreaParent(ctx context.Context, req *Request) (*NameCodeParentReply, error) {
|
||||
reply := &NameCodeParentReply{}
|
||||
|
||||
|
@ -68,6 +72,7 @@ func (ps *provinceserver) AreaParent(ctx context.Context, req *Request) (*NameCo
|
|||
return reply, nil
|
||||
}
|
||||
|
||||
// CityParent 同上 多级返回
|
||||
func (ps *provinceserver) CityParent(ctx context.Context, req *Request) (*NameCodeParentReply, error) {
|
||||
reply := &NameCodeParentReply{}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user