feat(firestore): add support for omitzero struct tag
Fixes: https://github.com/googleapis/google-cloud-go/issues/11885
This change introduces support for the omitzero struct tag option in the Firestore client library, similar to its behavior in encoding/json.
The omitzero tag allows struct fields to be omitted from Firestore documents during marshaling if their value is considered "zero". The determination of "zero" follows these rules:
- If the field's type has an
IsZero() boolmethod, that method is called. If it returnstrue, the field is omitted. - Otherwise, if the field's value is the Go default zero value for its type (e.g., 0 for numbers, false for bool, "" for strings, nil for pointers/interfaces/slices/maps, and the zero value for structs), it is omitted. This is checked using
reflect.Value.IsZero().
If both omitzero and omitempty tags are present on a field, the field will be omitted if either condition (empty as per omitempty's rules, or zero as per omitzero's rules) is met.
This provides more fine-grained control over field serialization, especially for distinguishing between a field that is intentionally set to a zero value versus a field that is not set or should be considered absent.
Changes include:
- Updated
tagOptionsandparseTagto recognize and process theomitzerooption. - Modified
structToProtoValueto implement the omission logic based on theomitzerotag. - Added comprehensive unit tests in
to_value_test.goto verify the new functionality across various types, including those withIsZero()methods and combinations withomitempty.
Output: