Skip to content

Decimal logical type not serilized correctly #296

@olegKoshmeliuk

Description

@olegKoshmeliuk

In current implementation decimal is serilized as simple string.
but in 1.10.2 AVRO spec it must be sterilized as:

The byte array must contain the two's-complement representation of the unscaled integer value in big-endian byte order.
The scale is fixed, and is specified using an attribute.

Example:

package main

import (
	"fmt"

	"github.com/linkedin/goavro/v2"
)

func main() {
	codec, err := goavro.NewCodec(`
        {"type":"bytes","logicalType":"decimal","precision":4,"scale":2}`)
	if err != nil {
		fmt.Println(err)
	}
	textual := []byte(`"40.20"`)
	native, _, err := codec.NativeFromTextual(textual)
	if err != nil {
		fmt.Println(err)
	}
	// Here is incorrect result. decimal value must be represented as bytes and not as string.
	binary, err := codec.BinaryFromNative(nil, native)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(binary))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions