Skip to content

Commit

Permalink
Handle default values for fixed types properly
Browse files Browse the repository at this point in the history
  • Loading branch information
actgardner committed Sep 21, 2019
1 parent 18a279e commit 53f1d04
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion schema/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (s *FixedDefinition) DefaultValue(lvalue string, rvalue interface{}) (strin
return "", fmt.Errorf("Expected string as default for field %v, got %q", lvalue, rvalue)
}

return fmt.Sprintf("%v = []byte(%q)", lvalue, rvalue), nil
return fmt.Sprintf("copy(%v[:], []byte(%q))", lvalue, rvalue), nil
}

func (s *FixedDefinition) IsReadableBy(d Definition) bool {
Expand Down
15 changes: 15 additions & 0 deletions test/default-fixed/fixed-default.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "record",
"name": "FixedDefaultTestRecord",
"fields": [
{
"name": "FixedField",
"type": {
"name": "TestFixedDefaultType",
"size": 12,
"type": "fixed"
},
"default": "\u0000\u0001\u0012\u0000\u0013\u0043\u0000\u0001\u0012\u0000\u0013\u0053"
}
]
}
3 changes: 3 additions & 0 deletions test/default-fixed/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package avro

//go:generate $GOPATH/bin/gogen-avro . fixed-default.avsc
17 changes: 17 additions & 0 deletions test/default-fixed/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package avro

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDefault(t *testing.T) {
var fixedDefault = NewFixedDefaultTestRecord()
fixedDefault.SetDefault(0)

expected := FixedDefaultTestRecord{
FixedField: [12]byte{0, 1, 18, 0, 19, 67, 0, 1, 18, 0, 19, 83},
}
assert.Equal(t, &expected, fixedDefault, "Comparing default value")
}

0 comments on commit 53f1d04

Please sign in to comment.