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

sizefrom is not documented and error message is incorrect #76

Open
chenwng opened this issue Oct 18, 2019 · 3 comments
Open

sizefrom is not documented and error message is incorrect #76

chenwng opened this issue Oct 18, 2019 · 3 comments

Comments

@chenwng
Copy link

chenwng commented Oct 18, 2019

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.

@lunixbochs
Copy link
Owner

Can you post a reproduction case? A struct with sizefrom in it that triggers that error message.

@chenwng
Copy link
Author

chenwng commented Oct 19, 2019

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.

@lunixbochs
Copy link
Owner

lunixbochs commented Oct 19, 2019

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.

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