francoispqt / gojay

high performance JSON encoder/decoder with stream API for Golang
MIT License
2.11k stars 112 forks source link

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

Open sam016 opened 4 years ago

sam016 commented 4 years ago

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 commented 3 years ago

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