Skip to content

Commit

Permalink
comment typo and regression test cases
Browse files Browse the repository at this point in the history
addresses linkedin#172
  • Loading branch information
Karrick S. McDermott committed Jul 10, 2019
1 parent b045554 commit 368ddcb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
6 changes: 3 additions & 3 deletions record.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func makeRecordCodec(st map[string]*Codec, enclosingNamespace string, schemaMap
}
defaultValue = int64(v)
case "int":
// When codec specifies a long, then ensure value is a JSON
// When codec specifies an int, then ensure value is a JSON
// number (decodes as a float64), then cast into the correct
// type.
v, ok := defaultValue.(float64)
Expand All @@ -92,7 +92,7 @@ func makeRecordCodec(st map[string]*Codec, enclosingNamespace string, schemaMap
}
defaultValue = int32(v)
case "double":
// When codec specifies a long, then ensure value is a JSON
// When codec specifies a double, then ensure value is a JSON
// number (decodes as a float64), then cast into the correct
// type.
v, ok := defaultValue.(float64)
Expand All @@ -101,7 +101,7 @@ func makeRecordCodec(st map[string]*Codec, enclosingNamespace string, schemaMap
}
defaultValue = float64(v)
case "float":
// When codec specifies a long, then ensure value is a JSON
// When codec specifies a float, then ensure value is a JSON
// number (decodes as a float64), then cast into the correct
// type.
v, ok := defaultValue.(float64)
Expand Down
70 changes: 45 additions & 25 deletions record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,33 +639,53 @@ func TestRecordFieldFixedDefaultValue(t *testing.T) {
}

func TestRecordFieldLongDefaultValue(t *testing.T) {
codec, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": 0}]}`)
if err != nil {
t.Fatal(err)
}
r1, _, err := codec.NativeFromTextual([]byte("{}"))
if err != nil {
t.Fatal(err)
}
r1m := r1.(map[string]interface{})
t.Run("success", func(t *testing.T) {
codec, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": 0}]}`)
if err != nil {
t.Fatal(err)
}
r1, _, err := codec.NativeFromTextual([]byte("{}"))
if err != nil {
t.Fatal(err)
}
r1m := r1.(map[string]interface{})

someLong := r1m["someLong"]
if _, ok := someLong.(int64); !ok {
t.Errorf("GOT: %T; WANT: int64", someLong)
}
someLong := r1m["someLong"]
if _, ok := someLong.(int64); !ok {
t.Errorf("GOT: %T; WANT: int64", someLong)
}

someInt := r1m["someInt"]
if _, ok := someInt.(int32); !ok {
t.Errorf("GOT: %T; WANT: int32", someInt)
}
someInt := r1m["someInt"]
if _, ok := someInt.(int32); !ok {
t.Errorf("GOT: %T; WANT: int32", someInt)
}

someFloat := r1m["someFloat"]
if _, ok := someFloat.(float32); !ok {
t.Errorf("GOT: %T; WANT: float32", someFloat)
}
someFloat := r1m["someFloat"]
if _, ok := someFloat.(float32); !ok {
t.Errorf("GOT: %T; WANT: float32", someFloat)
}

someDouble := r1m["someDouble"]
if _, ok := someDouble.(float64); !ok {
t.Errorf("GOT: %T; WANT: float64", someDouble)
}
someDouble := r1m["someDouble"]
if _, ok := someDouble.(float64); !ok {
t.Errorf("GOT: %T; WANT: float64", someDouble)
}
})
t.Run("provided default is wrong type", func(t *testing.T) {
t.Run("long", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": "0"},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": 0}]}`)
ensureError(t, err, "field schema")
})
t.Run("int", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": "0"},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": 0}]}`)
ensureError(t, err, "field schema")
})
t.Run("float", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": "0"},{"name": "someDouble", "type": "double", "default": 0}]}`)
ensureError(t, err, "field schema")
})
t.Run("double", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": "0"}]}`)
ensureError(t, err, "field schema")
})
})
}

0 comments on commit 368ddcb

Please sign in to comment.