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

Generator fails for map[string]interface{} field #141

Open
sam016 opened this issue Mar 14, 2020 · 1 comment
Open

Generator fails for map[string]interface{} field #141

sam016 opened this issue Mar 14, 2020 · 1 comment

Comments

@sam016
Copy link

sam016 commented Mar 14, 2020

I have been facing an issue with the gojay generator for map[string]interface{} field. Let me share the code first:

import "github.com/francoispqt/gojay"

// PlainDictionary stores foo values
type PlainDictionary struct {
	data map[string]interface{}
}

// UnMarDictionary stores foo values which implements
// the MarshalerJSONArray, MarshalerJSONObject, UnmarshalerJSONArray, UnmarshalerJSONObject
type UnMarDictionary struct {
	data map[string]interface{}
}

func (d *UnMarDictionary) UnmarshalJSONObject(*gojay.Decoder, string) error { return nil }   // UnmarshalerJSONObject
func (d *UnMarDictionary) NKeys() int                                       { return 0 }     // UnmarshalerJSONObject
func (d *UnMarDictionary) UnmarshalJSONArray(*gojay.Decoder) error          { return nil }   // UnmarshalerJSONArray
func (d *UnMarDictionary) MarshalJSONObject(enc *gojay.Encoder)             {}               // MarshalerJSONObject
func (d *UnMarDictionary) IsNil() bool                                      { return false } // MarshalerJSONObject
func (d *UnMarDictionary) MarshalJSONArray(enc *gojay.Encoder)              {}               // MarshalerJSONArray
//func (d *UnMarDictionary) IsNil() bool                                    { return false } // MarshalerJSONArray

// PlainClassroom using PlainDictionary under the hood
type PlainClassroom struct {
	ID       int
	Students PlainDictionary
}

// UnMarClassroom using UnMarDictionary under the hood
type UnMarClassroom struct {
	ID       int
	Students UnMarDictionary
}

I am trying to generate the gojay code for PlainClassroom and UnMarClassroom.

I tried

gojay -s dictionary.go -p true -t PlainClassroom
gojay -s dictionary.go -p true -t UnMarClassroom

and both failed with the following error:
Unknown type map[string]interface{} for field data

Here, UnMarDictionary implements the MarshalerJSONArray, MarshalerJSONObject, UnmarshalerJSONArray and UnmarshalerJSONObject. Since, UnMarClassroom has Students UnMarDictionary under the hood, when I generate the gojay code for UnMarClassroom, I was expecting that gojay would skip generating the code for UnMarDictionary.

Am I doing it in the wrong way? Please help.

@zorro786
Copy link

zorro786 commented Oct 7, 2020

You need to define custom type for map. Read up examples on root page.

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

2 participants