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

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

Open
fbalicchia opened this issue Jul 7, 2023 · 0 comments
Open

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

fbalicchia opened this issue Jul 7, 2023 · 0 comments

Comments

@fbalicchia
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant