Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
6e01e0f0
Commit
6e01e0f0
authored
Jan 19, 2017
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
a1b4dd6c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
21 deletions
+32
-21
t/neo/protogen.go
t/neo/protogen.go
+32
-21
No files found.
t/neo/protogen.go
View file @
6e01e0f0
...
@@ -116,7 +116,8 @@ import (
...
@@ -116,7 +116,8 @@ import (
fmt
.
Fprintf
(
&
buf
,
"// %d. %s
\n\n
"
,
pktCode
,
typename
)
fmt
.
Fprintf
(
&
buf
,
"// %d. %s
\n\n
"
,
pktCode
,
typename
)
buf
.
WriteString
(
gendecode
(
typespec
))
//buf.WriteString(generateCodecCode(typespec, &encoder{}))
buf
.
WriteString
(
generateCodecCode
(
typespec
,
&
decoder
{}))
buf
.
WriteString
(
"
\n
"
)
buf
.
WriteString
(
"
\n
"
)
pktCode
++
pktCode
++
...
@@ -173,6 +174,9 @@ func (b *Buffer) emit(format string, a ...interface{}) {
...
@@ -173,6 +174,9 @@ func (b *Buffer) emit(format string, a ...interface{}) {
// interface of codegenerator for coder/decoder
// interface of codegenerator for coder/decoder
type
CodecCodeGen
interface
{
type
CodecCodeGen
interface
{
genPrologue
(
recvName
,
typeName
string
)
genEpilogue
()
// emit code to process basic fixed types (not string)
// emit code to process basic fixed types (not string)
// userType is type actually used in source (for which typ is underlying), or nil
// userType is type actually used in source (for which typ is underlying), or nil
genBasic
(
path
string
,
typ
*
types
.
Basic
,
userType
types
.
Type
,
obj
types
.
Object
)
genBasic
(
path
string
,
typ
*
types
.
Basic
,
userType
types
.
Type
,
obj
types
.
Object
)
...
@@ -182,6 +186,8 @@ type CodecCodeGen interface {
...
@@ -182,6 +186,8 @@ type CodecCodeGen interface {
// XXX particular case of slice
// XXX particular case of slice
genStrBytes
(
path
string
)
genStrBytes
(
path
string
)
generatedCode
()
string
}
}
// encode/decode codegen
// encode/decode codegen
...
@@ -198,20 +204,35 @@ type decoder struct {
...
@@ -198,20 +204,35 @@ type decoder struct {
//var _ CodecCodeGen = (*encoder)(nil)
//var _ CodecCodeGen = (*encoder)(nil)
var
_
CodecCodeGen
=
(
*
decoder
)(
nil
)
var
_
CodecCodeGen
=
(
*
decoder
)(
nil
)
func
(
d
*
decoder
)
generatedCode
()
string
{
return
d
.
String
()
// XXX -> d.buf.String() ?
}
func
(
d
*
decoder
)
genPrologue
(
recvName
,
typeName
string
)
{
d
.
emit
(
"func (%s *%s) NEODecode(data []byte) (int, error) {"
,
recvName
,
typeName
)
d
.
emit
(
"var nread uint32"
)
}
func
(
d
*
decoder
)
genEpilogue
()
{
d
.
emit
(
"return int(nread) + %v, nil"
,
d
.
n
)
d
.
emit
(
"
\n
overflow:"
)
d
.
emit
(
"return 0, ErrDecodeOverflow"
)
d
.
emit
(
"}"
)
}
func
(
/*e*/
d
*
encoder
)
genBasic
(
path
string
,
typ
*
types
.
Basic
,
userType
types
.
Type
,
obj
types
.
Object
)
{
func
(
/*e*/
d
*
encoder
)
genBasic
(
path
string
,
typ
*
types
.
Basic
,
userType
types
.
Type
,
obj
types
.
Object
)
{
basic
:=
basicTypes
[
typ
.
Kind
()]
basic
:=
basicTypes
[
typ
.
Kind
()]
dataptr
:=
fmt
.
Sprintf
(
"data[%v:]"
,
d
.
n
)
dataptr
:=
fmt
.
Sprintf
(
"data[%v:]"
,
d
.
n
)
decoded
:=
fmt
.
Sprintf
(
basic
.
decode
,
dataptr
)
d
.
n
+=
basic
.
wireSize
if
userType
!=
nil
&&
userType
!=
typ
{
if
userType
!=
nil
&&
userType
!=
typ
{
// userType is a named type over some basic, like
// userType is a named type over some basic, like
// type ClusterState int32
// type ClusterState int32
// -> need to cast
// -> need to cast
decoded
=
fmt
.
Sprintf
(
"%v(%v)"
,
typeName
(
userType
),
decoded
)
path
=
fmt
.
Sprintf
(
"%v(%v)"
,
typeName
(
userType
),
path
)
}
}
d
.
n
+=
basic
.
wireSize
// NOTE no space before "=" - to be able to merge with ":"
// NOTE no space before "=" - to be able to merge with ":"
// prefix and become defining assignment
// prefix and become defining assignment
d
.
emit
(
"%s= %s"
,
path
,
decoded
)
d
.
emit
(
basic
.
encode
,
dataptr
,
path
)
}
}
func
(
d
*
decoder
)
genBasic
(
assignto
string
,
typ
*
types
.
Basic
,
userType
types
.
Type
,
obj
types
.
Object
)
{
func
(
d
*
decoder
)
genBasic
(
assignto
string
,
typ
*
types
.
Basic
,
userType
types
.
Type
,
obj
types
.
Object
)
{
...
@@ -358,26 +379,16 @@ func codegenType(path string, typ types.Type, obj types.Object, codegen CodecCod
...
@@ -358,26 +379,16 @@ func codegenType(path string, typ types.Type, obj types.Object, codegen CodecCod
}
}
// generate decoder func for a type declaration typespec
// generate encoder/decoder funcs for a type declaration typespec
func
gendecode
(
typespec
*
ast
.
TypeSpec
)
string
{
func
generateCodecCode
(
typespec
*
ast
.
TypeSpec
,
codec
CodecCodeGen
)
string
{
d
:=
decoder
{}
codec
.
genPrologue
(
"p"
,
typespec
.
Name
.
Name
)
// prologue
d
.
emit
(
"func (p *%s) NEODecode(data []byte) (int, error) {"
,
typespec
.
Name
.
Name
)
d
.
emit
(
"var nread uint32"
)
//n := 0
//t := typespec.Type.(*ast.StructType) // must be
// type & object which refers to this type
// type & object which refers to this type
typ
:=
info
.
Types
[
typespec
.
Type
]
.
Type
typ
:=
info
.
Types
[
typespec
.
Type
]
.
Type
obj
:=
info
.
Defs
[
typespec
.
Name
]
obj
:=
info
.
Defs
[
typespec
.
Name
]
codegenType
(
"p"
,
typ
,
obj
,
&
d
)
codegenType
(
"p"
,
typ
,
obj
,
codec
)
d
.
emit
(
"return int(nread) + %v, nil"
,
d
.
n
)
codec
.
genEpilogue
()
d
.
emit
(
"
\n
overflow:"
)
return
codec
.
generatedCode
()
d
.
emit
(
"return 0, ErrDecodeOverflow"
)
d
.
emit
(
"}"
)
//return d.buf.String()
return
d
.
String
()
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment