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

[Question] Form encoding/decoding ignores slice of struct #214

Open
tommysalt opened this issue Apr 13, 2024 · 4 comments
Open

[Question] Form encoding/decoding ignores slice of struct #214

tommysalt opened this issue Apr 13, 2024 · 4 comments

Comments

@tommysalt
Copy link

tommysalt commented Apr 13, 2024

Current Behavior

When encoding/decoding from data with slices of custom structs, they are ignored even thought the examples mention:

[...]
struct
[...]
a slice or a pointer to a slice of one of the above types

Output:

map[name:[Name]]

Expected Behavior

Output:

map[name:[Name], id:[1 2], amount:[1 2]

Steps To Reproduce

var encoder = schema.NewEncoder()

type Position struct {
	ID     int64 `schema:"id"`
	Amount int64 `schema:"amount"`
}

type Document struct {
	Name      string     `schema:"name"`
	Positions []Position `schema:"position"`
}

func main() {
	person := Document{"Name", []Position{{1, 1}, {2, 2}}}
	form := url.Values{}

	encoder.Encode(person, form)

	fmt.Println(form)
}

Output:

map[name:[Name]]
@tommysalt tommysalt added the bug label Apr 13, 2024
@tommysalt
Copy link
Author

I added an if to catch the error and it seems it's missing an encoder.

	err := encoder.Encode(person, form)
	if err != nil {
		fmt.Println(err)
	}
schema: encoder not found for [{1 1} {2 2}]

@tommysalt
Copy link
Author

So it seems that it's not a bug but I find the behavior rather odd because if I follow the docs they mention

[...]
struct
[...]
a slice or a pointer to a slice of one of the above types

So why do I need a custom encoder/decoder even tho my struct only contains supported fields (int64)?

@jaitaiwan
Copy link
Member

That's a great question @tommysalt thanks for bringing this to our attention. I'm not sure what the answer is right now but I'll try to answer it in the near future.

@jaitaiwan jaitaiwan changed the title [BUG] Form encoding/decoding ignores slice of struct [Question] Form encoding/decoding ignores slice of struct Jun 15, 2024
@jaitaiwan jaitaiwan added discussion and removed bug labels Jun 15, 2024
@apoorvajagtap
Copy link
Member

Hello @tommysalt Thanks for bringing this up!
I tried to dig a little into the encoder & this is more likely a documentation bug.

As soon as the encoder detects that the field type is a slice, it checks for the dataType, and if the dataType is custom, it returns nil as encodingfunc. So, it seems that when the documentation mentions:

a slice or a pointer to a slice of one of the above types

It actually means,

  • a slice or a pointer to a slice of one of the:
    * bool
    • float variants (float32, float64)
    • int variants (int, int8, int16, int32, int64)
    • string
    • uint variants (uint, uint8, uint16, uint32, uint64)

& probably not a struct.

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

No branches or pull requests

3 participants