Commit c49c72d0 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent d0a883df
......@@ -4,8 +4,8 @@ package neo
// XXX move imports out of here
import (
"encoding/binary"
"math"
//"encoding/binary"
//"math"
)
const (
......@@ -105,28 +105,27 @@ type Address struct {
}
// NOTE if Host == "" -> Port not added to wire (see py.PAddress):
/*
func (a *Address) NEOEncode(b []byte) int {
n := string_NEOEncode(a.Host, b[0:])
if a.Host != "" {
BigEndian.PutUint16(b[n:], a.Port)
n += 2
}
return n
}
func (a *Address) NEODecode(b []byte) int {
n := string_NEODecode(&a.Host, b)
if a.Host != "" {
a.Port = BigEndian.Uint16(b[n:])
n += 2
} else {
a.Port = 0
}
return n
}
*/
// func (a *Address) NEOEncode(b []byte) int {
// n := string_NEOEncode(a.Host, b[0:])
// if a.Host != "" {
// BigEndian.PutUint16(b[n:], a.Port)
// n += 2
// }
// return n
// }
//
// func (a *Address) NEODecode(b []byte) int {
// n := string_NEODecode(&a.Host, b)
// if a.Host != "" {
// a.Port = BigEndian.Uint16(b[n:])
// n += 2
// } else {
// a.Port = 0
// }
// return n
// }
/*
// A SHA1 hash
type Checksum [20]byte
......@@ -180,15 +179,13 @@ type RowInfo struct {
/*
// XXX link request <-> answer ?
// XXX naming -> PktHeader ?
type PktHead struct {
ConnId be32 // NOTE is .msgid in py
MsgCode be16
Len be32 // whole packet length (including header)
}
*/
// // XXX link request <-> answer ?
// // XXX naming -> PktHeader ?
// type PktHead struct {
// ConnId be32 // NOTE is .msgid in py
// MsgCode be16
// Len be32 // whole packet length (including header)
// }
// TODO generate .Encode() / .Decode()
......@@ -704,7 +701,7 @@ type AnswerPack struct {
// ctl -> A
// A -> M
type CheckReplicas struct {
PartitionDict map[uint32/*PNumber*/]UUID // partition -> source
PartitionDict map[uint32]UUID // partition -> source (PNumber)
MinTID Tid
MaxTID Tid
......@@ -795,3 +792,4 @@ type NotifyReady struct {
// replication
// TODO
*/
......@@ -21,11 +21,13 @@ import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"os"
)
// information about one packet type
......@@ -75,6 +77,7 @@ func main() {
//return
f := fv[0] // proto.go comes first
out := Buffer{}
for _, decl := range f.Decls {
// we look for types (which can be only under GenDecl)
......@@ -98,7 +101,7 @@ func main() {
//fmt.Println(t)
//ast.Print(fset, t)
gendecode(typespec)
out.WriteString(gendecode(typespec))
/*
PacketType{name: typename, msgCode: ncode}
......@@ -133,6 +136,18 @@ func main() {
//fmt.Println(gdecl)
//ast.Print(fset, gdecl)
}
// format & emit out
outf, err := format.Source(out.Bytes())
if err != nil {
panic(err) // should not happen
}
_, err = os.Stdout.Write(outf)
//_, err = os.Stdout.Write(out.Bytes())
if err != nil {
log.Fatal(err)
}
}
/*
......@@ -174,18 +189,18 @@ type Buffer struct {
bytes.Buffer
}
func (b *Buffer) Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(b, format, a...)
func (b *Buffer) Printfln(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(b, format+"\n", a...)
}
func gendecode(typespec *ast.TypeSpec) string {
buf := Buffer{}
emitf := buf.Printf
emit := buf.Printfln
typename := typespec.Name.Name
t := typespec.Type.(*ast.StructType) // must be
emitf("func (p *%s) NEODecode(data []byte) int {\n", typename)
emit("func (p *%s) NEODecode(data []byte) (int, error) {", typename)
n := 0 // current decode pos in data
......@@ -215,7 +230,13 @@ func gendecode(typespec *ast.TypeSpec) string {
}
emitstrbytes := func(fieldname string) {
emitf("{ l := %v", decodeBasic(types.Typ[types.Uint32]))
emit("{ l := %v", decodeBasic(types.Typ[types.Uint32]))
emit("data = data[%v:]", n)
emit("if len(data) < l { return 0, ErrDecodeOverflow }")
emit("p.%v = string(data[:l])", fieldname)
emit("data = data[l:]")
emit("}")
n = 0
}
......@@ -231,7 +252,7 @@ func gendecode(typespec *ast.TypeSpec) string {
continue
}
emitf("p.%s = %s", fieldname, decodeBasic(u))
emit("p.%s = %s", fieldname, decodeBasic(u))
case *types.Slice:
// TODO
......@@ -260,10 +281,10 @@ func gendecode(typespec *ast.TypeSpec) string {
// len u32
// [len] items
emitf("length = Uint32(data[%s:])", n)
emit("length = Uint32(data[%s:])", n)
n += 4
emitf("for ; length != 0; length-- {")
emitf("}")
emit("for ; length != 0; length-- {")
emit("}")
......@@ -271,7 +292,7 @@ func gendecode(typespec *ast.TypeSpec) string {
case *ast.MapType:
// len u32
// [len] key, value
emitf("length = Uint32(data[%s:])", n)
emit("length = Uint32(data[%s:])", n)
n += 4
keysize := wiresize(fieldtype.Key)
......@@ -286,7 +307,7 @@ func gendecode(typespec *ast.TypeSpec) string {
}
}
fmt.Fprintf(&buf, "}\n")
// TODO format.Source(buf.Bytes()) (XXX -> better at top-level for whole file)
emit("return %v /* + TODO variable part */, nil", n)
emit("}")
return buf.String()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment