From 39f6bc3abe0e9c6f741bd62a4d965e1b6eb5c9eb Mon Sep 17 00:00:00 2001 From: Mihai Todor Date: Fri, 19 Aug 2022 19:22:47 +0100 Subject: [PATCH] Fix golangci-lint action --- .github/workflows/golangci-lint.yml | 10 +- codec.go | 320 ++++++++++++++-------------- doc.go | 88 ++++---- helpers_test.go | 12 +- logical_type.go | 20 +- ocf_reader.go | 34 +-- rabin.go | 48 ++--- text_test.go | 8 +- union.go | 38 ++-- union_test.go | 14 +- 10 files changed, 297 insertions(+), 295 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 096a7e6..26ce59f 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -7,9 +7,11 @@ jobs: name: lint runs-on: ubuntu-latest steps: - - uses: actions/setup-go@v2 - - uses: actions/checkout@v2 + - uses: actions/setup-go@v3 + with: + go-version: 1.18.x + - uses: actions/checkout@v3 - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v3 with: - version: v1.45.2 + version: v1.48.0 diff --git a/codec.go b/codec.go index cd14110..ee5bda1 100644 --- a/codec.go +++ b/codec.go @@ -84,17 +84,17 @@ type codecBuilder struct { // after instantiation. In other words, `Codec`s may be safely used by // many go routines simultaneously, as your program requires. // -// codec, err := goavro.NewCodec(` -// { -// "type": "record", -// "name": "LongList", -// "fields" : [ -// {"name": "next", "type": ["null", "LongList"], "default": null} -// ] -// }`) -// if err != nil { -// fmt.Println(err) -// } +// codec, err := goavro.NewCodec(` +// { +// "type": "record", +// "name": "LongList", +// "fields" : [ +// {"name": "next", "type": ["null", "LongList"], "default": null} +// ] +// }`) +// if err != nil { +// fmt.Println(err) +// } func NewCodec(schemaSpecification string) (*Codec, error) { return NewCodecFrom(schemaSpecification, &codecBuilder{ buildCodecForTypeDescribedByMap, @@ -124,17 +124,17 @@ func NewCodec(schemaSpecification string) (*Codec, error) { // The following is the exact same schema used in the above // code for NewCodec: // -// codec, err := goavro.NewCodecForStandardJSON(` -// { -// "type": "record", -// "name": "LongList", -// "fields" : [ -// {"name": "next", "type": ["null", "LongList"], "default": null} -// ] -// }`) -// if err != nil { -// fmt.Println(err) -// } +// codec, err := goavro.NewCodecForStandardJSON(` +// { +// "type": "record", +// "name": "LongList", +// "fields" : [ +// {"name": "next", "type": ["null", "LongList"], "default": null} +// ] +// }`) +// if err != nil { +// fmt.Println(err) +// } // // The above will take json of this sort: // @@ -149,7 +149,7 @@ func NewCodecForStandardJSON(schemaSpecification string) (*Codec, error) { return NewCodecFrom(schemaSpecification, &codecBuilder{ buildCodecForTypeDescribedByMap, buildCodecForTypeDescribedByString, - buildCodecForTypeDescribedBySliceOneWayJson, + buildCodecForTypeDescribedBySliceOneWayJSON, }) } @@ -194,7 +194,7 @@ func NewCodecForStandardJSONFull(schemaSpecification string) (*Codec, error) { return NewCodecFrom(schemaSpecification, &codecBuilder{ buildCodecForTypeDescribedByMap, buildCodecForTypeDescribedByString, - buildCodecForTypeDescribedBySliceTwoWayJson, + buildCodecForTypeDescribedBySliceTwoWayJSON, }) } @@ -357,38 +357,38 @@ func newSymbolTable() map[string]*Codec { // a nil error value. On error, it returns the original byte slice, and the // error message. // -// func ExampleBinaryFromNative() { -// codec, err := goavro.NewCodec(` -// { -// "type": "record", -// "name": "LongList", -// "fields" : [ -// {"name": "next", "type": ["null", "LongList"], "default": null} -// ] -// }`) -// if err != nil { -// fmt.Println(err) -// } -// -// // Convert native Go form to binary Avro data -// binary, err := codec.BinaryFromNative(nil, map[string]interface{}{ -// "next": map[string]interface{}{ -// "LongList": map[string]interface{}{ -// "next": map[string]interface{}{ -// "LongList": map[string]interface{}{ -// // NOTE: May omit fields when using default value -// }, -// }, -// }, -// }, -// }) -// if err != nil { -// fmt.Println(err) -// } -// -// fmt.Printf("%#v", binary) -// // Output: []byte{0x2, 0x2, 0x0} -// } +// func ExampleBinaryFromNative() { +// codec, err := goavro.NewCodec(` +// { +// "type": "record", +// "name": "LongList", +// "fields" : [ +// {"name": "next", "type": ["null", "LongList"], "default": null} +// ] +// }`) +// if err != nil { +// fmt.Println(err) +// } +// +// // Convert native Go form to binary Avro data +// binary, err := codec.BinaryFromNative(nil, map[string]interface{}{ +// "next": map[string]interface{}{ +// "LongList": map[string]interface{}{ +// "next": map[string]interface{}{ +// "LongList": map[string]interface{}{ +// // NOTE: May omit fields when using default value +// }, +// }, +// }, +// }, +// }) +// if err != nil { +// fmt.Println(err) +// } +// +// fmt.Printf("%#v", binary) +// // Output: []byte{0x2, 0x2, 0x0} +// } func (c *Codec) BinaryFromNative(buf []byte, datum interface{}) ([]byte, error) { newBuf, err := c.binaryFromNative(buf, datum) if err != nil { @@ -403,30 +403,30 @@ func (c *Codec) BinaryFromNative(buf []byte, datum interface{}) ([]byte, error) // undecoded bytes, and a nil error value. On error, it returns nil for // the datum value, the original byte slice, and the error message. // -// func ExampleNativeFromBinary() { -// codec, err := goavro.NewCodec(` -// { -// "type": "record", -// "name": "LongList", -// "fields" : [ -// {"name": "next", "type": ["null", "LongList"], "default": null} -// ] -// }`) -// if err != nil { -// fmt.Println(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) -// } -// -// fmt.Printf("%v", native) -// // Output: map[next:map[LongList:map[next:map[LongList:map[next:]]]]] -// } +// func ExampleNativeFromBinary() { +// codec, err := goavro.NewCodec(` +// { +// "type": "record", +// "name": "LongList", +// "fields" : [ +// {"name": "next", "type": ["null", "LongList"], "default": null} +// ] +// }`) +// if err != nil { +// fmt.Println(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) +// } +// +// fmt.Printf("%v", native) +// // Output: map[next:map[LongList:map[next:map[LongList:map[next:]]]]] +// } func (c *Codec) NativeFromBinary(buf []byte) (interface{}, []byte, error) { value, newBuf, err := c.nativeFromBinary(buf) if err != nil { @@ -442,14 +442,14 @@ func (c *Codec) NativeFromBinary(buf []byte) (interface{}, []byte, error) { // error value. On error, it returns nil for the datum value, the original byte // slice, and the error message. // -// func decode(codec *goavro.Codec, buf []byte) error { -// datum, _, err := codec.NativeFromSingle(buf) -// if err != nil { -// return err -// } -// _, err = fmt.Println(datum) -// return err -// } +// func decode(codec *goavro.Codec, buf []byte) error { +// datum, _, err := codec.NativeFromSingle(buf) +// if err != nil { +// return err +// } +// _, err = fmt.Println(datum) +// return err +// } func (c *Codec) NativeFromSingle(buf []byte) (interface{}, []byte, error) { fingerprint, newBuf, err := FingerprintFromSOE(buf) if err != nil { @@ -472,30 +472,30 @@ func (c *Codec) NativeFromSingle(buf []byte) (interface{}, []byte, error) { // error, it returns nil for the datum value, the original byte slice, and the // error message. // -// func ExampleNativeFromTextual() { -// codec, err := goavro.NewCodec(` -// { -// "type": "record", -// "name": "LongList", -// "fields" : [ -// {"name": "next", "type": ["null", "LongList"], "default": null} -// ] -// }`) -// if err != nil { -// fmt.Println(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) -// } -// -// fmt.Printf("%v", native) -// // Output: map[next:map[LongList:map[next:map[LongList:map[next:]]]]] -// } +// func ExampleNativeFromTextual() { +// codec, err := goavro.NewCodec(` +// { +// "type": "record", +// "name": "LongList", +// "fields" : [ +// {"name": "next", "type": ["null", "LongList"], "default": null} +// ] +// }`) +// if err != nil { +// fmt.Println(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) +// } +// +// fmt.Printf("%v", native) +// // Output: map[next:map[LongList:map[next:map[LongList:map[next:]]]]] +// } func (c *Codec) NativeFromTextual(buf []byte) (interface{}, []byte, error) { value, newBuf, err := c.nativeFromTextual(buf) if err != nil { @@ -512,22 +512,22 @@ func (c *Codec) NativeFromTextual(buf []byte) (interface{}, []byte, error) { // encoded bytes appended, and a nil error value. On error, it returns the // original byte slice, and the error message. // -// func ExampleSingleItemEncoding() { -// codec, err := goavro.NewCodec(`"int"`) -// if err != nil { -// fmt.Fprintf(os.Stderr, "%s\n", err) -// return -// } -// -// buf, err := codec.SingleFromNative(nil, 3) -// if err != nil { -// fmt.Fprintf(os.Stderr, "%s\n", err) -// return -// } -// -// fmt.Println(buf) -// // Output: [195 1 143 92 57 63 26 213 117 114 6] -// } +// func ExampleSingleItemEncoding() { +// codec, err := goavro.NewCodec(`"int"`) +// if err != nil { +// fmt.Fprintf(os.Stderr, "%s\n", err) +// return +// } +// +// buf, err := codec.SingleFromNative(nil, 3) +// if err != nil { +// fmt.Fprintf(os.Stderr, "%s\n", err) +// return +// } +// +// fmt.Println(buf) +// // Output: [195 1 143 92 57 63 26 213 117 114 6] +// } func (c *Codec) SingleFromNative(buf []byte, datum interface{}) ([]byte, error) { newBuf, err := c.binaryFromNative(append(buf, c.soeHeader...), datum) if err != nil { @@ -543,38 +543,38 @@ func (c *Codec) SingleFromNative(buf []byte, datum interface{}) ([]byte, error) // appended, and a nil error value. On error, it returns the original byte // slice, and the error message. // -// func ExampleTextualFromNative() { -// codec, err := goavro.NewCodec(` -// { -// "type": "record", -// "name": "LongList", -// "fields" : [ -// {"name": "next", "type": ["null", "LongList"], "default": null} -// ] -// }`) -// if err != nil { -// fmt.Println(err) -// } -// -// // Convert native Go form to text Avro data -// text, err := codec.TextualFromNative(nil, map[string]interface{}{ -// "next": map[string]interface{}{ -// "LongList": map[string]interface{}{ -// "next": map[string]interface{}{ -// "LongList": map[string]interface{}{ -// // NOTE: May omit fields when using default value -// }, -// }, -// }, -// }, -// }) -// if err != nil { -// fmt.Println(err) -// } -// -// fmt.Printf("%s", text) -// // Output: {"next":{"LongList":{"next":{"LongList":{"next":null}}}}} -// } +// func ExampleTextualFromNative() { +// codec, err := goavro.NewCodec(` +// { +// "type": "record", +// "name": "LongList", +// "fields" : [ +// {"name": "next", "type": ["null", "LongList"], "default": null} +// ] +// }`) +// if err != nil { +// fmt.Println(err) +// } +// +// // Convert native Go form to text Avro data +// text, err := codec.TextualFromNative(nil, map[string]interface{}{ +// "next": map[string]interface{}{ +// "LongList": map[string]interface{}{ +// "next": map[string]interface{}{ +// "LongList": map[string]interface{}{ +// // NOTE: May omit fields when using default value +// }, +// }, +// }, +// }, +// }) +// if err != nil { +// fmt.Println(err) +// } +// +// fmt.Printf("%s", text) +// // Output: {"next":{"LongList":{"next":{"LongList":{"next":null}}}}} +// } func (c *Codec) TextualFromNative(buf []byte, datum interface{}) ([]byte, error) { newBuf, err := c.textualFromNative(buf, datum) if err != nil { diff --git a/doc.go b/doc.go index 1358af6..3e48f21 100644 --- a/doc.go +++ b/doc.go @@ -11,58 +11,58 @@ files. Usage Example: - package main + package main - import ( - "fmt" + import ( + "fmt" - "github.com/linkedin/goavro" - ) + "github.com/linkedin/goavro" + ) - func main() { - codec, err := goavro.NewCodec(` - { - "type": "record", - "name": "LongList", - "fields" : [ - {"name": "next", "type": ["null", "LongList", {"type": "long", "logicalType": "timestamp-millis"}], "default": null} - ] - }`) - if err != nil { - fmt.Println(err) - } + func main() { + codec, err := goavro.NewCodec(` + { + "type": "record", + "name": "LongList", + "fields" : [ + {"name": "next", "type": ["null", "LongList", {"type": "long", "logicalType": "timestamp-millis"}], "default": null} + ] + }`) + if err != nil { + fmt.Println(err) + } - // NOTE: May omit fields when using default value - textual := []byte(`{"next":{"LongList":{}}}`) + // NOTE: May omit fields when using default value + textual := []byte(`{"next":{"LongList":{}}}`) - // Convert textual Avro data (in Avro JSON format) to native Go form - native, _, err := codec.NativeFromTextual(textual) - if err != nil { - fmt.Println(err) - } + // Convert textual Avro data (in Avro JSON format) to native Go form + native, _, err := codec.NativeFromTextual(textual) + if err != nil { + fmt.Println(err) + } - // Convert native Go form to binary Avro data - binary, err := codec.BinaryFromNative(nil, native) - if err != nil { - fmt.Println(err) - } + // Convert native Go form to binary Avro data + binary, err := codec.BinaryFromNative(nil, native) + if err != nil { + fmt.Println(err) + } - // Convert binary Avro data back to native Go form - native, _, err = codec.NativeFromBinary(binary) - if err != nil { - fmt.Println(err) - } + // Convert binary Avro data back to native Go form + native, _, err = codec.NativeFromBinary(binary) + if err != nil { + fmt.Println(err) + } - // Convert native Go form to textual Avro data - textual, err = codec.TextualFromNative(nil, native) - if err != nil { - fmt.Println(err) - } + // Convert native Go form to textual Avro data + textual, err = codec.TextualFromNative(nil, native) + if err != nil { + fmt.Println(err) + } - // NOTE: Textual encoding will show all fields, even those with values that - // match their default values - fmt.Println(string(textual)) - // Output: {"next":{"LongList":{"next":null}}} - } + // NOTE: Textual encoding will show all fields, even those with values that + // match their default values + fmt.Println(string(textual)) + // Output: {"next":{"LongList":{"next":null}}} + } */ package goavro diff --git a/helpers_test.go b/helpers_test.go index 627948c..ac74eb5 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -18,14 +18,14 @@ import ( // // Copied with author's permission from https://github.com/karrick/gorill. // -// bb := NopCloseBuffer() -// sw := ShortWriter(bb, 16) +// bb := NopCloseBuffer() +// sw := ShortWriter(bb, 16) // -// n, err := sw.Write([]byte("short write")) -// // n == 11, err == nil +// n, err := sw.Write([]byte("short write")) +// // n == 11, err == nil // -// n, err := sw.Write([]byte("a somewhat longer write")) -// // n == 16, err == io.ErrShortWrite +// n, err := sw.Write([]byte("a somewhat longer write")) +// // n == 16, err == io.ErrShortWrite func ShortWriter(w io.Writer, max int) io.Writer { return shortWriter{Writer: w, max: max} } diff --git a/logical_type.go b/logical_type.go index 353b71f..80e9dc2 100644 --- a/logical_type.go +++ b/logical_type.go @@ -23,9 +23,9 @@ type fromNativeFn func([]byte, interface{}) ([]byte, error) var reFromPattern = make(map[string]*regexp.Regexp) -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// // date logical type - to/from time.Time, time.UTC location -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// func nativeFromDate(fn toNativeFn) toNativeFn { return func(bytes []byte) (interface{}, []byte, error) { l, b, err := fn(bytes) @@ -64,9 +64,9 @@ func dateFromNative(fn fromNativeFn) fromNativeFn { } } -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// // time-millis logical type - to/from time.Time, time.UTC location -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// func nativeFromTimeMillis(fn toNativeFn) toNativeFn { return func(bytes []byte) (interface{}, []byte, error) { l, b, err := fn(bytes) @@ -100,9 +100,9 @@ func timeMillisFromNative(fn fromNativeFn) fromNativeFn { } } -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// // time-micros logical type - to/from time.Time, time.UTC location -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// func nativeFromTimeMicros(fn toNativeFn) toNativeFn { return func(bytes []byte) (interface{}, []byte, error) { l, b, err := fn(bytes) @@ -136,9 +136,9 @@ func timeMicrosFromNative(fn fromNativeFn) fromNativeFn { } } -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// // timestamp-millis logical type - to/from time.Time, time.UTC location -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// func nativeFromTimeStampMillis(fn toNativeFn) toNativeFn { return func(bytes []byte) (interface{}, []byte, error) { l, b, err := fn(bytes) @@ -174,9 +174,9 @@ func timeStampMillisFromNative(fn fromNativeFn) fromNativeFn { } } -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// // timestamp-micros logical type - to/from time.Time, time.UTC location -////////////////////////////////////////////////////////////////////////////////////////////// +// //////////////////////////////////////////////////////////////////////////////////////////// func nativeFromTimeStampMicros(fn toNativeFn) toNativeFn { return func(bytes []byte) (interface{}, []byte, error) { l, b, err := fn(bytes) diff --git a/ocf_reader.go b/ocf_reader.go index 338b2d6..0a7c771 100644 --- a/ocf_reader.go +++ b/ocf_reader.go @@ -34,23 +34,23 @@ type OCFReader struct { // NewOCFReader initializes and returns a new structure used to read an Avro // Object Container File (OCF). // -// func example(ior io.Reader) error { -// // NOTE: Wrap provided io.Reader in a buffered reader, which improves the -// // performance of streaming file data. -// br := bufio.NewReader(ior) -// ocfr, err := goavro.NewOCFReader(br) -// if err != nil { -// return err -// } -// for ocfr.Scan() { -// datum, err := ocfr.Read() -// if err != nil { -// return err -// } -// fmt.Println(datum) -// } -// return ocfr.Err() -// } +// func example(ior io.Reader) error { +// // NOTE: Wrap provided io.Reader in a buffered reader, which improves the +// // performance of streaming file data. +// br := bufio.NewReader(ior) +// ocfr, err := goavro.NewOCFReader(br) +// if err != nil { +// return err +// } +// for ocfr.Scan() { +// datum, err := ocfr.Read() +// if err != nil { +// return err +// } +// fmt.Println(datum) +// } +// return ocfr.Err() +// } func NewOCFReader(ior io.Reader) (*OCFReader, error) { header, err := readOCFHeader(ior) if err != nil { diff --git a/rabin.go b/rabin.go index bfb1bed..157e4d4 100644 --- a/rabin.go +++ b/rabin.go @@ -293,33 +293,33 @@ const soeHeaderLen = soeMagicPrefix + 8 // 2-byte prefix plus 8-byte fingerprint // the second return value. On failure this function returns an // ErrNotSingleObjectEncoded error. // -// func decode(codex map[uint64]*goavro.Codec, buf []byte) error { -// // Perform a sanity check on the buffer, then return the Rabin fingerprint -// // of the schema used to encode the data. -// fingerprint, newBuf, err := goavro.FingerprintFromSOE(buf) -// if err != nil { -// return err -// } +// func decode(codex map[uint64]*goavro.Codec, buf []byte) error { +// // Perform a sanity check on the buffer, then return the Rabin fingerprint +// // of the schema used to encode the data. +// fingerprint, newBuf, err := goavro.FingerprintFromSOE(buf) +// if err != nil { +// return err +// } // -// // Get a previously stored Codec from the codex map. -// codec, ok := codex[fingerprint] -// if !ok { -// return fmt.Errorf("unknown codec: %#x", fingerprint) -// } +// // Get a previously stored Codec from the codex map. +// codec, ok := codex[fingerprint] +// if !ok { +// return fmt.Errorf("unknown codec: %#x", fingerprint) +// } // -// // Use the fetched Codec to decode the buffer as a SOE. -// // -// // Faster because SOE magic prefix and schema fingerprint already -// // checked and used to fetch the Codec. Just need to decode the binary -// // bytes remaining after the prefix were removed. -// datum, _, err := codec.NativeFromBinary(newBuf) -// if err != nil { -// return err -// } +// // Use the fetched Codec to decode the buffer as a SOE. +// // +// // Faster because SOE magic prefix and schema fingerprint already +// // checked and used to fetch the Codec. Just need to decode the binary +// // bytes remaining after the prefix were removed. +// datum, _, err := codec.NativeFromBinary(newBuf) +// if err != nil { +// return err +// } // -// _, err = fmt.Println(datum) -// return err -// } +// _, err = fmt.Println(datum) +// return err +// } func FingerprintFromSOE(buf []byte) (uint64, []byte, error) { if len(buf) < soeHeaderLen { // Not enough bytes to encode schema fingerprint. diff --git a/text_test.go b/text_test.go index 553751a..a398dd1 100644 --- a/text_test.go +++ b/text_test.go @@ -66,24 +66,24 @@ func testTextDecodePass(t *testing.T, schema string, datum interface{}, encoded } toNativeAndCompare(t, schema, datum, encoded, codec) } -func testJsonDecodePass(t *testing.T, schema string, datum interface{}, encoded []byte) { +func testJSONDecodePass(t *testing.T, schema string, datum interface{}, encoded []byte) { t.Helper() codec, err := NewCodecFrom(schema, &codecBuilder{ buildCodecForTypeDescribedByMap, buildCodecForTypeDescribedByString, - buildCodecForTypeDescribedBySliceOneWayJson, + buildCodecForTypeDescribedBySliceOneWayJSON, }) if err != nil { t.Fatalf("schema: %s; %s", schema, err) } toNativeAndCompare(t, schema, datum, encoded, codec) } -func testNativeToTextualJsonPass(t *testing.T, schema string, datum interface{}, encoded []byte) { +func testNativeToTextualJSONPass(t *testing.T, schema string, datum interface{}, encoded []byte) { t.Helper() codec, err := NewCodecFrom(schema, &codecBuilder{ buildCodecForTypeDescribedByMap, buildCodecForTypeDescribedByString, - buildCodecForTypeDescribedBySliceTwoWayJson, + buildCodecForTypeDescribedBySliceTwoWayJSON, }) if err != nil { t.Fatalf("schema: %s; %s", schema, err) diff --git a/union.go b/union.go index bcef06f..031e84f 100644 --- a/union.go +++ b/union.go @@ -35,18 +35,18 @@ type codecInfo struct { // Avro type name and the value is the datum's value. As a convenience, the // `Union` function wraps any datum value in a map as specified above. // -// func ExampleUnion() { -// codec, err := goavro.NewCodec(`["null","string","int"]`) -// if err != nil { -// fmt.Println(err) -// } -// buf, err := codec.TextualFromNative(nil, goavro.Union("string", "some string")) -// if err != nil { -// fmt.Println(err) -// } -// fmt.Println(string(buf)) -// // Output: {"string":"some string"} -// } +// func ExampleUnion() { +// codec, err := goavro.NewCodec(`["null","string","int"]`) +// if err != nil { +// fmt.Println(err) +// } +// buf, err := codec.TextualFromNative(nil, goavro.Union("string", "some string")) +// if err != nil { +// fmt.Println(err) +// } +// fmt.Println(string(buf)) +// // Output: {"string":"some string"} +// } func Union(name string, datum interface{}) interface{} { if datum == nil && name == "null" { return nil @@ -196,7 +196,7 @@ func unionTextualFromNative(cr *codecInfo) func(buf []byte, datum interface{}) ( return nil, fmt.Errorf("cannot encode textual union: non-nil values ought to be specified with Go map[string]interface{}, with single key equal to type name, and value equal to datum value: %v; received: %T", cr.allowedTypes, datum) } } -func textualJsonFromNativeAvro(cr *codecInfo) func(buf []byte, datum interface{}) ([]byte, error) { +func textualJSONFromNativeAvro(cr *codecInfo) func(buf []byte, datum interface{}) ([]byte, error) { return func(buf []byte, datum interface{}) ([]byte, error) { switch v := datum.(type) { case nil: @@ -277,7 +277,7 @@ func buildCodecForTypeDescribedBySlice(st map[string]*Codec, enclosingNamespace // and then it will remain avro-json object // avro data is not serialized back into standard json // the data goes to avro-json and stays that way -func buildCodecForTypeDescribedBySliceOneWayJson(st map[string]*Codec, enclosingNamespace string, schemaArray []interface{}, cb *codecBuilder) (*Codec, error) { +func buildCodecForTypeDescribedBySliceOneWayJSON(st map[string]*Codec, enclosingNamespace string, schemaArray []interface{}, cb *codecBuilder) (*Codec, error) { if len(schemaArray) == 0 { return nil, errors.New("Union ought to have one or more members") } @@ -296,12 +296,12 @@ func buildCodecForTypeDescribedBySliceOneWayJson(st map[string]*Codec, enclosing typeName: &name{"union", nullNamespace}, nativeFromBinary: unionNativeFromBinary(&cr), binaryFromNative: unionBinaryFromNative(&cr), - nativeFromTextual: nativeAvroFromTextualJson(&cr), + nativeFromTextual: nativeAvroFromTextualJSON(&cr), textualFromNative: unionTextualFromNative(&cr), } return rv, nil } -func buildCodecForTypeDescribedBySliceTwoWayJson(st map[string]*Codec, enclosingNamespace string, schemaArray []interface{}, cb *codecBuilder) (*Codec, error) { +func buildCodecForTypeDescribedBySliceTwoWayJSON(st map[string]*Codec, enclosingNamespace string, schemaArray []interface{}, cb *codecBuilder) (*Codec, error) { if len(schemaArray) == 0 { return nil, errors.New("Union ought to have one or more members") } @@ -320,8 +320,8 @@ func buildCodecForTypeDescribedBySliceTwoWayJson(st map[string]*Codec, enclosing typeName: &name{"union", nullNamespace}, nativeFromBinary: unionNativeFromBinary(&cr), binaryFromNative: unionBinaryFromNative(&cr), - nativeFromTextual: nativeAvroFromTextualJson(&cr), - textualFromNative: textualJsonFromNativeAvro(&cr), + nativeFromTextual: nativeAvroFromTextualJSON(&cr), + textualFromNative: textualJSONFromNativeAvro(&cr), } return rv, nil } @@ -344,7 +344,7 @@ func checkAll(allowedTypes []string, cr *codecInfo, buf []byte) (interface{}, [] } return nil, buf, fmt.Errorf("could not decode any json data in input %v", string(buf)) } -func nativeAvroFromTextualJson(cr *codecInfo) func(buf []byte) (interface{}, []byte, error) { +func nativeAvroFromTextualJSON(cr *codecInfo) func(buf []byte) (interface{}, []byte, error) { return func(buf []byte) (interface{}, []byte, error) { reader := bytes.NewReader(buf) diff --git a/union_test.go b/union_test.go index 83298b6..b66884f 100644 --- a/union_test.go +++ b/union_test.go @@ -250,7 +250,7 @@ func ExampleCodec_TextualFromNative_json() { codec, err := NewCodecFrom(`["null","string","int"]`, &codecBuilder{ buildCodecForTypeDescribedByMap, buildCodecForTypeDescribedByString, - buildCodecForTypeDescribedBySliceOneWayJson, + buildCodecForTypeDescribedBySliceOneWayJSON, }) if err != nil { fmt.Println(err) @@ -267,7 +267,7 @@ func ExampleCodec_NativeFromTextual_json() { codec, err := NewCodecFrom(`["null","string","int"]`, &codecBuilder{ buildCodecForTypeDescribedByMap, buildCodecForTypeDescribedByString, - buildCodecForTypeDescribedBySliceOneWayJson, + buildCodecForTypeDescribedBySliceOneWayJSON, }) if err != nil { fmt.Println(err) @@ -328,14 +328,14 @@ func TestUnionJson(t *testing.T) { } for _, td := range testData { - testJsonDecodePass(t, td.schema, td.datum, td.encoded) - testNativeToTextualJsonPass(t, td.schema, td.datum, td.encoded) + testJSONDecodePass(t, td.schema, td.datum, td.encoded) + testNativeToTextualJSONPass(t, td.schema, td.datum, td.encoded) } - //these two give different results depending on if its going into native or into a string + // these two give different results depending on if its going into native or into a string // when this goes to native it gets the "field2" because its given, but it also gets a "field1" because "field1" has a default value // when this goes to a string it has a field from both "field1" and one for "field2" - testJsonDecodePass(t, `{"type":"record","name":"kubeEvents","fields":[{"name":"field1","type":"string","default":""},{"name":"field2","type":"string"}]}`, map[string]interface{}{"field1": "", "field2": "deef"}, []byte(`{"field2": "deef"}`)) - testNativeToTextualJsonPass(t, `{"type":"record","name":"kubeEvents","fields":[{"name":"field1","type":"string","default":""},{"name":"field2","type":"string"}]}`, map[string]interface{}{"field1": "", "field2": "deef"}, []byte(`{"field1":"","field2":"deef"}`)) + testJSONDecodePass(t, `{"type":"record","name":"kubeEvents","fields":[{"name":"field1","type":"string","default":""},{"name":"field2","type":"string"}]}`, map[string]interface{}{"field1": "", "field2": "deef"}, []byte(`{"field2": "deef"}`)) + testNativeToTextualJSONPass(t, `{"type":"record","name":"kubeEvents","fields":[{"name":"field1","type":"string","default":""},{"name":"field2","type":"string"}]}`, map[string]interface{}{"field1": "", "field2": "deef"}, []byte(`{"field1":"","field2":"deef"}`)) }