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

feat: skip explicitly ignored fields #85

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion musttag.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,19 @@ func (c *checker) checkStruct(styp *types.Struct, tag string) (valid bool) {
continue
}

if _, ok := reflect.StructTag(styp.Tag(i)).Lookup(tag); !ok {
tagValue, ok := reflect.StructTag(styp.Tag(i)).Lookup(tag)
if !ok {
// tag is not required for embedded types; see issue #12.
if !field.Embedded() {
return false
}
}

// Do not recurse into ignored fields.
if tagValue == "-" {
return true
}

if valid := c.checkType(field.Type(), tag); !valid {
return false
}
Expand Down
15 changes: 15 additions & 0 deletions testdata/src/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,18 @@ func nestedTypeWithInterface() {
json.Marshal(Foo{}) // no error
json.Marshal(&Foo{}) // no error
}

func ignoredNestedType() {
type Nested struct {
NoTag string
}
type Foo struct {
Ignored Nested `json:"-"`
Exported string `json:"exported"`
}
var foo Foo
json.Marshal(foo) // no error
json.Marshal(&foo) // no error
json.Marshal(Foo{}) // no error
json.Marshal(&Foo{}) // no error
}
Loading