linkedin / linkedin/goavro

Strange behavior using goavro.Union with map. (Question)

Open
#274 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
1.1k
Forks
232
PR merge metrics
No merged PRs in 30d

Description

I'm stumbling with some strange behavior using Union and I don't understand if I'm missing something in using API or if it is a possible issue. Please check the following piece of code

Thanks for support

```
package main

import (
"fmt"

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

const schema = `{
"type": "record",
"name": "appPublished",
"namespace": "it.balyfix",
"fields": [{
"name": "appId",
"type": "string"
},
{
"name": "metaDataMap",
"type": [
"null",
{
"type": "map",
"values" : ["string", "int", "boolean", "long", "double"]
}
],
"default": null
}
]
}`

func main() {
codec, err := goavro.NewCodec(schema)
if err != nil {
fmt.Println(err)
}
attemptOneKO(map[string]interface{}{"appId": "app1"}, codec)
attemptTwoKO(map[string]interface{}{"appId": "app1"}, codec)
attemptWithoutMetadataOK(map[string]interface{}{"appId": "app1"}, codec)
}

func attemptOneKO(message2Send map[string]interface{}, codec *goavro.Codec) {
metaDataMap := make(map[string]string)
metaDataMap["key1"] = "value1"
metaDataMap["key2"] = "value2"
message2Send["metaDataMap"] = metaDataMap
buf, err := codec.TextualFromNative(nil, message2Send)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))

}

func attemptTwoKO(message2Send map[string]interface{}, codec *goavro.Codec) {

message2Send["metaDataMap"] = goavro.Union("string", map[string]interface{}{
"child1": "foo",
"child2": "bar"})
buf, err := codec.TextualFromNative(nil, message2Send)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))

}

func attemptWithoutMetadataOK(message2Send map[string]interface{}, codec *goavro.Codec) {
buf, err := codec.TextualFromNative(nil, message2Send)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))

}
```
I'm using v2.12.0

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the three attempts in the provided Go program with goavro v2.12.0, then inspect NewCodec, TextualFromNative, and Union handling for the map field. Compare the returned errors and output for each input, and document whether the behavior is expected or identify a focused failing case.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.