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

Possible to decode without embedding type information? #106

Closed
xrstf opened this issue Mar 29, 2018 · 1 comment
Closed

Possible to decode without embedding type information? #106

xrstf opened this issue Mar 29, 2018 · 1 comment
Labels

Comments

@xrstf
Copy link

xrstf commented Mar 29, 2018

Sorry for misusing the issue tracker for asking a question :-/

I'm trying to decode an OCF file into JSON, to mimic a Clojure library's behaviour. Unfortunately, I cannot get goavro to not embed type information into the decoded output.

Consider the following sample program:

package main

import (
	"fmt"
	"log"

	"github.com/linkedin/goavro"
)

type dummy struct {
	MyBool *bool
}

func (d *dummy) ToMap() map[string]interface{} {
	result := make(map[string]interface{})

	if d.MyBool == nil {
		result["MyBool"] = goavro.Union("null", nil)
	} else {
		result["MyBool"] = goavro.Union("boolean", *d.MyBool)
	}

	return result
}

func main() {
	codec, err := goavro.NewCodec(`
     {
       "name": "Dummy",
       "type": "record",
       "fields": [
         {
           "name": "MyBool",
           "type": ["null", "boolean"]
         }
       ]
     }`)
	if err != nil {
		log.Fatalln(err)
	}

	val := true
	data := dummy{
		MyBool: &val,
	}

	textual, err := codec.TextualFromNative(nil, data.ToMap())
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(string(textual)) // {"MyBool":{"boolean":true}}
}

What I would need is this textual representation:

{
  "MyBool": true
}

Is there a way to construct a codec that does not embed type information into the output? Or would I need to manually walk the decoded data and the schema and remove the types manually?

@karrick
Copy link
Contributor

karrick commented Apr 27, 2018

This is the way that the Avro specification requires Avro data to be printed in JSON form when a Union is involved.

https://avro.apache.org/docs/current/spec.html#json_encoding

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

No branches or pull requests

2 participants