{"record":{"id":"aa1c45aad674624e","repo":"apache/beam","slug":"mongodbio-inferprojection-no-names-to-infer-projection-from","errorCode":null,"errorMessage":"mongodbio.inferProjection: no names to infer projection from","messagePattern":"mongodbio\\.inferProjection: no names to infer projection from","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/read.go","lineNumber":150,"sourceCode":"\tvar err error\n\tif err = fn.mongoDBFn.Setup(ctx); err != nil {\n\t\treturn err\n\t}\n\n\tfn.filter, err = decodeBSON[bson.M](fn.Filter)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfn.projection = inferProjection(fn.Type.T, bsonTag)\n\n\treturn nil\n}\n\nfunc inferProjection(t reflect.Type, tagKey string) bson.D {\n\tnames := structx.InferFieldNames(t, tagKey)\n\tif len(names) == 0 {\n\t\tpanic(\"mongodbio.inferProjection: no names to infer projection from\")\n\t}\n\n\tprojection := make(bson.D, len(names))\n\n\tfor i, name := range names {\n\t\tprojection[i] = bson.E{Key: name, Value: 1}\n\t}\n\n\treturn projection\n}\n\nfunc (fn *readFn) CreateInitialRestriction(\n\tctx context.Context,\n\t_ []byte,\n) (idRangeRestriction, error) {\n\tif err := fn.Setup(ctx); err != nil {\n\t\treturn idRangeRestriction{}, err\n\t}","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/read.go#L132-L168","documentation":"inferProjection derives a BSON projection from a Go struct's field names (via structx.InferFieldNames with a tag key like bson). If the struct type yields zero field names, there is nothing to project and the function panics. This guards against reading into an empty or unsuitable struct type.","triggerScenarios":"Calling mongodbio.Read with a target type t that is an empty struct (no fields), an unexported/anonymous struct with no inferable fields, or a wrong tag key such that no field carries the expected tag and the inference falls back to nothing.","commonSituations":"Passing reflect.TypeOf on an empty result struct; mismatched tag key (e.g. expecting bson tags but the struct only has json tags combined with inference settings); generated types with no exported fields.","solutions":["Add exported fields with proper tags to the struct used as the read target","Use the correct tag key for your struct (bson for mongodbio)","Verify reflect.Type passed is the concrete struct, not an interface or empty alias","Fall back to nil projection (read all fields) if inference legitimately finds nothing"],"exampleFix":"// before\ntype Row struct{} // no fields\nmongodbio.Read(s, scope, uri, db, col, reflect.TypeOf(Row{}), opt)\n// after\ntype Row struct {\n    ID   primitive.ObjectID `bson:\"_id\"`\n    Name string             `bson:\"name\"`\n}\nmongodbio.Read(s, scope, uri, db, col, reflect.TypeOf(Row{}), opt)","handlingStrategy":"type-guard","validationCode":"t := reflect.TypeOf(Row{})\nif t.NumField() == 0 { return fmt.Errorf(\"read target struct has no fields\") }","typeGuard":"func hasInferableFields(t reflect.Type, tag string) bool {\n    if t == nil || t.Kind() != reflect.Struct { return false }\n    for i := 0; i < t.NumField(); i++ {\n        if _, ok := t.Field(i).Tag.Lookup(tag); ok { return true }\n    }\n    return t.NumField() > 0\n}","tryCatchPattern":null,"preventionTips":["Always tag struct fields with bson tags for read targets","Assert target types are non-empty structs in unit tests","Keep the tag key consistent (bson) across the codebase"],"tags":["go","mongodb","panic","reflection","struct"],"backgroundTag":"empty-required-field","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}