gorilla / schema

Package gorilla/schema fills a struct with form values.
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
1.38k stars 229 forks source link

[bug] need to skip unexported field #203

Open limpo1989 opened 1 year ago

limpo1989 commented 1 year ago

Is there an existing issue for this?

Current Behavior

create a structInfo but the always inculde unexported field

https://github.com/gorilla/schema/blob/5fca2dce7a3a9cde2cd764a822ae373b4aaaaece/cache.go#L129-L139

Expected Behavior

create a structInfo but the skip unexported field

// create creates a structInfo with meta-data about a struct.
func (c *cache) create(t reflect.Type, parentAlias string) *structInfo {
    info := &structInfo{}
    var anonymousInfos []*structInfo
    for i := 0; i < t.NumField(); i++ {
        field := t.Field(i)
                // !!! skip unexported field
        if !field.IsExported() {
            continue
        }
        if f := c.createField(field, parentAlias); f != nil {
            info.fields = append(info.fields, f)
            if ft := indirectType(f.typ); ft.Kind() == reflect.Struct && f.isAnonymous {
                anonymousInfos = append(anonymousInfos, c.create(ft, f.canonicalAlias))
            }
        }
    }

Steps To Reproduce

No response

Anything else?

No response

jaitaiwan commented 4 months ago

This does seem like it should be expected behaviour but I don't know if there's anywhere in documentation saying that this is the expected behaviour. It seems to me that there should/could be some sort of setting that enables this globally.

zak905 commented 2 months ago

I agree with @jaitaiwan, if we want to mimic the behavior of other go standard packages like json, unexported fields are to be ignored. A flag can help maintain backward compatibility, to avoid breaking things. @limpo1989 are you willing to submit a PR?