Commit c49c72d0 authored by Kirill Smelkov's avatar Kirill Smelkov

.

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