Ignoring Null Values during MongoDB Document Unmarshalling
When unmarshalling a MongoDB document into a Go struct that contains non-nullable string fields, encountering null values in the document can lead to errors. To address this issue, it is necessary to find a way to ignore these null values during unmarshalling.
Using a Custom Decoder
One approach to handling null values is by creating a custom decoder for the string type. This custom decoder would recognize null values and handle them by setting the corresponding field to an empty string, effectively ignoring the null value:
import ( "go.mongodb.org/mongo-driver/bson/bsoncodec" "go.mongodb.org/mongo-driver/bson/bsonrw" "go.mongodb.org/mongo-driver/bson/bsontype" ) type nullawareStrDecoder struct{} func (nullawareStrDecoder) DecodeValue(dctx bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if !val.CanSet() || val.Kind() != reflect.String { return errors.New("bad type or not settable") } var str string var err error switch vr.Type() { case bsontype.String: if str, err = vr.ReadString(); err != nil { return err } case bsontype.Null: if err = vr.ReadNull(); err != nil { return err } default: return fmt.Errorf("cannot decode %v into a string type", vr.Type()) } val.SetString(str) return nil }
This custom decoder can then be registered with a bsoncodec.Registry and applied to a mongo.Client object:
clientOpts := options.Client(). ApplyURI("mongodb://localhost:27017/"). SetRegistry( bson.NewRegistryBuilder(). RegisterDecoder(reflect.TypeOf(""), nullawareStrDecoder{}). Build(), ) client, err := mongo.Connect(ctx, clientOpts)
Creating a Type-Neutral Null-Aware Decoder
To handle null values for multiple types, it is possible to create a single, type-neutral decoder that checks for null values and, if encountered, sets the corresponding field to the zero value of its type:
type nullawareDecoder struct { defDecoder bsoncodec.ValueDecoder zeroValue reflect.Value } func (d *nullawareDecoder) DecodeValue(dctx bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { if vr.Type() != bsontype.Null { return d.defDecoder.DecodeValue(dctx, vr, val) } if !val.CanSet() { return errors.New("value not settable") } if err := vr.ReadNull(); err != nil { return err } val.Set(d.zeroValue) return nil }
This decoder can be registered with a bsoncodec.Registry for specific types or for all types:
customValues := []interface{}{ "", // string int(0), // int int32(0), // int32 } rb := bson.NewRegistryBuilder() for _, v := range customValues { t := reflect.TypeOf(v) defDecoder, err := bson.DefaultRegistry.LookupDecoder(t) if err != nil { panic(err) } rb.RegisterDecoder(t, &nullawareDecoder{defDecoder, reflect.Zero(t)}) }
The above is the detailed content of How to Ignore Null Values When Unmarshalling MongoDB Documents in Go?. For more information, please follow other related articles on the PHP Chinese website!