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

[bug] need to skip unexported field #203

Open
1 task done
limpo1989 opened this issue Aug 30, 2023 · 2 comments
Open
1 task done

[bug] need to skip unexported field #203

limpo1989 opened this issue Aug 30, 2023 · 2 comments
Labels

Comments

@limpo1989
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

create a structInfo but the always inculde unexported field

schema/cache.go

Lines 129 to 139 in 5fca2dc

func (c *cache) create(t reflect.Type, parentAlias string) *structInfo {
info := &structInfo{}
var anonymousInfos []*structInfo
for i := 0; i < t.NumField(); i++ {
if f := c.createField(t.Field(i), 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))
}
}
}

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
Copy link
Member

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
Copy link
Contributor

zak905 commented Aug 25, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants