Skip to content

New feature: Filter struct fields using WithTag and WithoutTag

Compare
Choose a tag to compare
@huandu huandu released this 27 Feb 03:13
· 47 commits to master since this release
71a8512

When we build an UPDATE or INSERT SQL with Struct, we may need to filter out struct fields representing primary key or those should not be updated. Before v1.20.0, we have to define several combinations of fieldtag tag for every UPDATE or INSERT. It's not convenient.

To address this issue, there is a new method WithoutTag in Struct to explicitly exclude fields tagged with tags, so that we can simply tag fields as "pk" and call st.WithoutTag("pk").Update("table") to filter them out. And WithTag is upgraded to accept more than one tag. With WithTag and WithoutTag, we can build a complex struct field filter for all kinds of requirement.

To learn how to use these method, checkout document and samples in Struct, Struct.WithTag and Struct.WithoutTag.

One more thing. All *ForTag methods in Struct are deprecated. It's highly recommended to use st.WithTag("tag").Select("table") to replace st.SelectForTag("table", "tag").

Special thanks to @wangmir for your great thoughts and code review feedback.

Full Changelog: v1.19.0...v1.20.0