Closed veqryn closed 5 years ago
Sure this, seems like a good idea. We are preparing a release for new types (time.Time and nullable types from the sql package), will squeeze a few array/slice types there.
Something like this would be great:
type InformeDiario struct {
Conceptos []models.Concepto
Cuentas []models.Cuenta
Columnas []string
Stocks []idNombre
}
func (i InformeDiario) MarshalJSONObject(e *gojay.Encoder) {
arr := e.NewArray()
for _, v := range i.Conceptos {
item = e.NewArrayItem()
item.AddStringKey("name", v.Name)
item.AddStringKey("number", v.Number)
arr.Add(item)
}
e.AddArrayKey("conceptos", arr)
}
There is something that already allows to do this:
func (i InformeDiario) MarshalJSONObject(e *gojay.Encoder) {
e.AddArrayKey("conceptos", gojay.EncodeArrayFunc(func(enc *gojay.Encoder){
for _, v := range i.Conceptos {
enc.AddObject(gojay.EncodeObjectFunc(func(enc *gojay.Encoder) {
enc.AddStringKey("name", v.Name)
enc.AddStringKey("number", v.Number)
}))
}
})
}
Maybe will add a syntax similar to yours in the future but for now I will just add some basic slice types like:
enc.AddSliceStrKey("someSlice", v)
enc.AddSliceIntKey("someSlice", v)
enc.AddSliceBoolKey("someSlice", v)
enc.AddSliceObjectKey("someSlice", v)
enc.AddSliceSliceKey("someSlice", v)
Just added
dec.AddSliceString(*[]string)
dec.AddSliceInt(*[]int)
dec.AddSliceFloat64(*[]float64)
dec.AddSliceBool(*[]bool)
And there encoding counterparts. I'm closing this one.
Right now, having to create custom types for every single slice or array in your structs is pretty annoying. Additionally, it can be annoying to have to cast it back to the original type, though golang does a good job of not requiring this most of the time.
Would it be possible to support slices and arrays natively, perhaps by forcing the logic into the UnmarshalJSONObject/MarshalJSONObject functions?
For example, instead of:
Maybe something like this (rough pseudocode):
or