Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix skipping map and array fields that are missing in the reader schema #208

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions v10/compiler/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ func (b *methodCallIRInstruction) CompileToVM(p *irProgram) ([]vm.Instruction, e
}

type blockStartIRInstruction struct {
blockId int
blockId int
hasTarget bool
}

func (b *blockStartIRInstruction) VMLength() int {
return 9
return blockJumpOffset(b.hasTarget) + 1
}

func blockJumpOffset(withTarget bool) int {
if withTarget {
return 8
}
return 7
}

// At the beginning of a block, read the length into the Long register
Expand All @@ -53,21 +61,27 @@ func (b *blockStartIRInstruction) VMLength() int {
// Once we've figured out the number of iterations, push the loop length onto the loop stack
func (b *blockStartIRInstruction) CompileToVM(p *irProgram) ([]vm.Instruction, error) {
block := p.blocks[b.blockId]
return []vm.Instruction{
vm.Instruction{vm.Read, vm.Long},
vm.Instruction{vm.EvalEqual, 0},
vm.Instruction{vm.CondJump, block.end + 5},
vm.Instruction{vm.EvalGreater, 0},
vm.Instruction{vm.CondJump, block.start + 7},
vm.Instruction{vm.Read, vm.UnusedLong},
vm.Instruction{vm.MultLong, -1},
vm.Instruction{vm.HintSize, vm.UnusedLong},
vm.Instruction{vm.PushLoop, 0},
}, nil

instructions := []vm.Instruction{
{vm.Read, vm.Long},
{vm.EvalEqual, 0},
{vm.CondJump, block.end + 5},
{vm.EvalGreater, 0},
{vm.CondJump, block.start + blockJumpOffset(b.hasTarget)},
{vm.Read, vm.UnusedLong},
{vm.MultLong, -1},
}

if b.hasTarget {
instructions = append(instructions, vm.Instruction{vm.HintSize, vm.UnusedLong})
}

return append(instructions, vm.Instruction{vm.PushLoop, 0}), nil
}

type blockEndIRInstruction struct {
blockId int
blockId int
hasTarget bool
}

func (b *blockEndIRInstruction) VMLength() int {
Expand All @@ -78,12 +92,13 @@ func (b *blockEndIRInstruction) VMLength() int {
// top to read a new block. otherwise jump to start + 7, which pushes the value back on the loop stack
func (b *blockEndIRInstruction) CompileToVM(p *irProgram) ([]vm.Instruction, error) {
block := p.blocks[b.blockId]

return []vm.Instruction{
vm.Instruction{vm.PopLoop, 0},
vm.Instruction{vm.AddLong, -1},
vm.Instruction{vm.EvalEqual, 0},
vm.Instruction{vm.CondJump, block.start},
vm.Instruction{vm.Jump, block.start + 8},
{vm.PopLoop, 0},
{vm.AddLong, -1},
{vm.EvalEqual, 0},
{vm.CondJump, block.start},
{vm.Jump, block.start + blockJumpOffset(b.hasTarget)},
}, nil
}

Expand Down
79 changes: 44 additions & 35 deletions v10/compiler/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,24 @@ func (p *irMethod) addMethodCall(method string) {
p.body = append(p.body, &methodCallIRInstruction{method})
}

func (p *irMethod) addBlockStart() int {
func (p *irMethod) addBlockStart(skip bool) int {
id := len(p.program.blocks)
p.program.blocks = append(p.program.blocks, &irBlock{})
p.body = append(p.body, &blockStartIRInstruction{id})
p.body = append(p.body, &blockStartIRInstruction{id, skip})
return id
}

func (p *irMethod) addBlockEnd(id int) {
p.body = append(p.body, &blockEndIRInstruction{id})
func (p *irMethod) inBlock(skip bool, fn func(p *irMethod) error) error {
blockId := p.addBlockStart(skip)
if err := fn(p); err != nil {
return err
}
p.addBlockEnd(blockId, skip)
return nil
}

func (p *irMethod) addBlockEnd(id int, skip bool) {
p.body = append(p.body, &blockEndIRInstruction{id, skip})
}

func (p *irMethod) addSwitchStart(size, errorId int) int {
Expand Down Expand Up @@ -209,41 +218,41 @@ func (p *irMethod) compileRef(writer, reader *schema.Reference) error {

func (p *irMethod) compileMap(writer, reader *schema.MapField) error {
log("compileMap()\n writer:\n %v\n---\nreader: %v\n---\n", writer, reader)
blockId := p.addBlockStart()
p.addLiteral(vm.Read, vm.String)
var readerType schema.AvroType
if reader != nil {
p.addLiteral(vm.AppendMap, vm.Unused)
readerType = reader.ItemType()
}
err := p.compileType(writer.ItemType(), readerType)
if err != nil {
return err
}
if reader != nil {
p.addLiteral(vm.Exit, vm.Unused)
}
p.addBlockEnd(blockId)
return nil
return p.inBlock(reader != nil, func(p *irMethod) error {
p.addLiteral(vm.Read, vm.String)
var readerType schema.AvroType
if reader != nil {
p.addLiteral(vm.AppendMap, vm.Unused)
readerType = reader.ItemType()
}
err := p.compileType(writer.ItemType(), readerType)
if err != nil {
return err
}
if reader != nil {
p.addLiteral(vm.Exit, vm.Unused)
}
return nil
})
}

func (p *irMethod) compileArray(writer, reader *schema.ArrayField) error {
log("compileArray()\n writer:\n %v\n---\nreader: %v\n---\n", writer, reader)
blockId := p.addBlockStart()
var readerType schema.AvroType
if reader != nil {
p.addLiteral(vm.AppendArray, vm.Unused)
readerType = reader.ItemType()
}
err := p.compileType(writer.ItemType(), readerType)
if err != nil {
return err
}
if reader != nil {
p.addLiteral(vm.Exit, vm.Unused)
}
p.addBlockEnd(blockId)
return nil
return p.inBlock(reader != nil, func(p *irMethod) error {
var readerType schema.AvroType
if reader != nil {
p.addLiteral(vm.AppendArray, vm.Unused)
readerType = reader.ItemType()
}
err := p.compileType(writer.ItemType(), readerType)
if err != nil {
return err
}
if reader != nil {
p.addLiteral(vm.Exit, vm.Unused)
}
return nil
})
}

func (p *irMethod) compileRecord(writer, reader *schema.RecordDefinition) error {
Expand Down
4 changes: 4 additions & 0 deletions v10/test/array-removed/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package avro

//go:generate gogen-avro.exe -package writer writer writer.avsc
//go:generate gogen-avro.exe -package reader reader reader.avsc
13 changes: 13 additions & 0 deletions v10/test/array-removed/reader.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "record",
"name": "escid.esdp.Foo",
"fields": [
{
"name": "f2",
"type": {
"type": "array",
"items": "string"
}
}
]
}
59 changes: 59 additions & 0 deletions v10/test/array-removed/reader/array_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading