Open chenwng opened 4 years ago
Can you post a reproduction case? A struct with sizefrom in it that triggers that error message.
Actually the issue I encountered was not because of sizefrom. I was reading the README and saw that I can use a slice in a struct. Here is the statement about it.
Var []int
struc:"[]int32,little,sizeof=StringField"
will pack Var as a slice of little-endian int32, and link it as the size of StringField.
So I tried it in a test with something like F1 []uint8
struc:"[]uint8,sizeof=N"`` in one of my struct. Then I saw the error
struc: field
F1
is a slice with no length or sizeof field
I wondered why struc reported such error as I have put sizeof in the tag.
After I read the struc code, I found the statement in README is incorrect and error message is misleading. I should use sizefrom in the tag and if I don't use sizefrom the error message should report like is a slice with no length or *sizefrom* field
. And after I changed the tag to sizefrom, it works fine.
if f.Len == -1 && f.Sizefrom == nil {
return nil, fmt.Errorf("struc: field `%s` is a slice with no length or sizeof field", field.Name)
}
If the README can be updated and error message is fixed, it will be clearer.
There is a sizeof tag, but it goes on the size field and not on the array. Sizeof and Sizefrom are inverse of the same thing.
Hi,
In one of the PR, sizefrom was added. However it is not mentioned in README at all. And when I tried to use sizeof for slice without size, an incorrect message was reported, which is caused by this line.