gogen-avro icon indicating copy to clipboard operation
gogen-avro copied to clipboard

Example of using an Avro big.Rat as a value to produce the Avro Decimal

Open divo77 opened this issue 5 years ago • 3 comments

HI, It would be super cool if you give some examples of working properly for serialization/deserialization the Logical data types with gogen avro structs. I am in stack with the situation where I need to read the bytes from Avro Native like:

var codecDecimal, err= goavro.NewCodec({"type": ["null", {"type": "bytes", "logicalType": "decimal", "precision": 6, "scale": 4}]}) bytesDecimalBinary, err := codecDecimal.BinaryFromNative(nil, map[string]interface{}{"bytes.decimal": big.NewRat(500,4)})

from the Native GoAvro form I can read the value as big.Rat

What need to be done to assign the value to gogen avro struct correctly to produce the correct Decimal number after serialization to binary form ?

type AvroLogical struct { TEST__DECIMAL *UnionNullBytes }

in my case

gogenAvro:= model.NewAvroLogical()
gogenAvro.TEST_DECIMAL=model.NewUnionNullBytes()
gogenAvro.TEST_DECIMAL.Bytes = **NEW_DECIMAL**
gogenAvro.TEST_DECIMAL.UnionType = model.UnionNullBytesTypeEnumBytes

SO how to make a correct NEW_DECIMAL in my code ?

divo77 avatar Oct 14 '19 18:10 divo77

Let's simplify the question. Does gogen-avro support a Avro Logical datatypes as goavro does it OR it doesn't ?

divo77 avatar Oct 18 '19 16:10 divo77

could you answer the question ?

divo77 avatar Nov 05 '19 16:11 divo77

There's currently no support for logical types. You're able to work with these fields as a primitive type (bytes) but you would need to write logic to handle the decimal encoding. I would welcome a PR that added this functionality.

actgardner avatar Nov 20 '19 18:11 actgardner