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

Make example errors fatal #272

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions README.md
Expand Up @@ -141,6 +141,7 @@ package main

import (
"fmt"
"log"

"github.com/linkedin/goavro/v2"
)
Expand All @@ -155,7 +156,7 @@ func main() {
]
}`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// NOTE: May omit fields when using default value
Expand All @@ -164,25 +165,25 @@ func main() {
// Convert textual Avro data (in Avro JSON format) to native Go form
native, _, err := codec.NativeFromTextual(textual)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to binary Avro data
binary, err := codec.BinaryFromNative(nil, native)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert binary Avro data back to native Go form
native, _, err = codec.NativeFromBinary(binary)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to textual Avro data
textual, err = codec.TextualFromNative(nil, native)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// NOTE: Textual encoding will show all fields, even those with values that
Expand All @@ -205,6 +206,7 @@ package main
import (
"bytes"
"fmt"
"log"
"strings"

"github.com/linkedin/goavro/v2"
Expand Down Expand Up @@ -234,7 +236,7 @@ func main() {
Schema: avroSchema,
})
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
err = writer.Append([]map[string]interface{}{
{
Expand All @@ -251,13 +253,13 @@ func main() {
// Reading OCF data
ocfReader, err := goavro.NewOCFReader(strings.NewReader(ocfFileContents.String()))
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
fmt.Println("Records in OCF File");
for ocfReader.Scan() {
record, err := ocfReader.Read()
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
fmt.Println("record", record)
}
Expand Down Expand Up @@ -372,11 +374,11 @@ specified above.
func ExampleUnion() {
codec, err := goavro.NewCodec(`["null","string","int"]`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
buf, err := codec.TextualFromNative(nil, goavro.Union("string", "some string"))
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
fmt.Println(string(buf))
// Output: {"string":"some string"}
Expand Down
20 changes: 10 additions & 10 deletions codec.go
Expand Up @@ -93,7 +93,7 @@ type codecBuilder struct {
// ]
// }`)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
func NewCodec(schemaSpecification string) (*Codec, error) {
return NewCodecFrom(schemaSpecification, &codecBuilder{
Expand Down Expand Up @@ -133,7 +133,7 @@ func NewCodec(schemaSpecification string) (*Codec, error) {
// ]
// }`)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// The above will take json of this sort:
Expand Down Expand Up @@ -367,7 +367,7 @@ func newSymbolTable() map[string]*Codec {
// ]
// }`)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// // Convert native Go form to binary Avro data
Expand All @@ -383,7 +383,7 @@ func newSymbolTable() map[string]*Codec {
// },
// })
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// fmt.Printf("%#v", binary)
Expand Down Expand Up @@ -413,15 +413,15 @@ func (c *Codec) BinaryFromNative(buf []byte, datum interface{}) ([]byte, error)
// ]
// }`)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// // Convert native Go form to binary Avro data
// binary := []byte{0x2, 0x2, 0x0}
//
// native, _, err := codec.NativeFromBinary(binary)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// fmt.Printf("%v", native)
Expand Down Expand Up @@ -482,15 +482,15 @@ func (c *Codec) NativeFromSingle(buf []byte) (interface{}, []byte, error) {
// ]
// }`)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// // Convert native Go form to text Avro data
// text := []byte(`{"next":{"LongList":{"next":{"LongList":{"next":null}}}}}`)
//
// native, _, err := codec.NativeFromTextual(text)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// fmt.Printf("%v", native)
Expand Down Expand Up @@ -553,7 +553,7 @@ func (c *Codec) SingleFromNative(buf []byte, datum interface{}) ([]byte, error)
// ]
// }`)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// // Convert native Go form to text Avro data
Expand All @@ -569,7 +569,7 @@ func (c *Codec) SingleFromNative(buf []byte, datum interface{}) ([]byte, error)
// },
// })
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
//
// fmt.Printf("%s", text)
Expand Down
3 changes: 2 additions & 1 deletion codec_test.go
Expand Up @@ -12,6 +12,7 @@ package goavro
import (
"bytes"
"fmt"
"log"
"os"
"testing"
)
Expand All @@ -20,7 +21,7 @@ func ExampleCodec_CanonicalSchema() {
schema := `{"type":"map","values":{"type":"enum","name":"foo","symbols":["alpha","bravo"]}}`
codec, err := NewCodec(schema)
if err != nil {
fmt.Println(err)
log.Fatal(err)
} else {
fmt.Println(codec.CanonicalSchema())
}
Expand Down
10 changes: 5 additions & 5 deletions doc.go
Expand Up @@ -29,7 +29,7 @@ Usage Example:
]
}`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// NOTE: May omit fields when using default value
Expand All @@ -38,25 +38,25 @@ Usage Example:
// Convert textual Avro data (in Avro JSON format) to native Go form
native, _, err := codec.NativeFromTextual(textual)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to binary Avro data
binary, err := codec.BinaryFromNative(nil, native)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert binary Avro data back to native Go form
native, _, err = codec.NativeFromBinary(binary)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to textual Avro data
textual, err = codec.TextualFromNative(nil, native)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// NOTE: Textual encoding will show all fields, even those with values that
Expand Down
7 changes: 4 additions & 3 deletions logical_type_test.go
Expand Up @@ -11,6 +11,7 @@ package goavro

import (
"fmt"
"log"
"math"
"math/big"
"testing"
Expand Down Expand Up @@ -247,19 +248,19 @@ func ExampleUnion_logicalType() {
// * decimal - big.Rat
codec, err := NewCodec(`["null", {"type": "long", "logicalType": "timestamp-millis"}]`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Note the usage of type.logicalType i.e. `long.timestamp-millis` to denote the type in a union. This is due to the single string naming format
// used by goavro. Decimal can be both bytes.decimal or fixed.decimal
bytes, err := codec.BinaryFromNative(nil, map[string]interface{}{"long.timestamp-millis": time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)})
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

decoded, _, err := codec.NativeFromBinary(bytes)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}
out := decoded.(map[string]interface{})
fmt.Printf("%#v\n", out["long.timestamp-millis"].(time.Time).String())
Expand Down
27 changes: 14 additions & 13 deletions record_test.go
Expand Up @@ -12,6 +12,7 @@ package goavro
import (
"bytes"
"fmt"
"log"
"testing"
)

Expand Down Expand Up @@ -456,7 +457,7 @@ func ExampleCodec_NativeFromTextual_roundTrip() {
}
`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// NOTE: May omit fields when using default value
Expand All @@ -465,25 +466,25 @@ func ExampleCodec_NativeFromTextual_roundTrip() {
// Convert textual Avro data (in Avro JSON format) to native Go form
native, _, err := codec.NativeFromTextual(textual)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to binary Avro data
binary, err := codec.BinaryFromNative(nil, native)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert binary Avro data back to native Go form
native, _, err = codec.NativeFromBinary(binary)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to textual Avro data
textual, err = codec.TextualFromNative(nil, native)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// NOTE: Textual encoding will show all fields, even those with values that
Expand All @@ -503,7 +504,7 @@ func ExampleCodec_BinaryFromNative_avro() {
}
`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to binary Avro data
Expand All @@ -519,7 +520,7 @@ func ExampleCodec_BinaryFromNative_avro() {
},
})
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

fmt.Printf("%#v", binary)
Expand All @@ -537,15 +538,15 @@ func ExampleCodec_NativeFromBinary_avro() {
}
`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to binary Avro data
binary := []byte{0x2, 0x2, 0x0}

native, _, err := codec.NativeFromBinary(binary)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

fmt.Printf("%v", native)
Expand All @@ -563,15 +564,15 @@ func ExampleCodec_NativeFromTextual_avro() {
}
`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to text Avro data
text := []byte(`{"next":{"LongList":{"next":{"LongList":{"next":null}}}}}`)

native, _, err := codec.NativeFromTextual(text)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

fmt.Printf("%v", native)
Expand All @@ -589,7 +590,7 @@ func ExampleCodec_TextualFromNative_avro() {
}
`)
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

// Convert native Go form to text Avro data
Expand All @@ -605,7 +606,7 @@ func ExampleCodec_TextualFromNative_avro() {
},
})
if err != nil {
fmt.Println(err)
log.Fatal(err)
}

fmt.Printf("%s", text)
Expand Down
2 changes: 1 addition & 1 deletion schema_test.go
Expand Up @@ -137,7 +137,7 @@ func TestSchemaFixedNameCanBeUsedLater(t *testing.T) {
// schema := `{"type":"map","values":{"type":"enum","name":"foo","symbols":["alpha","bravo"]}}`
// codec, err := NewCodec(schema)
// if err != nil {
// fmt.Println(err)
// log.Fatal(err)
// }
// fmt.Println(codec.Schema())
// // Output: {"type":"map","values":{"name":"foo","type":"enum","symbols":["alpha","bravo"]}}
Expand Down