Description
Extend the DriverMessageModel struct by implementing a new standalone query method called BadgerDBFindOne. To ensure backward compatibility and prevent downstream integration issues, do not refactor, rename, or drop the current etcd-based FindOne implementation. This method introduces native support to fetch and deserialize a specific driver message entry directly from BadgerDB.
Requirements
- File Location:
internal/models/messages.go
- Method Signature Strategy: Introduce a new method named
BadgerDBFindOne(id string) (*craneTypes.DriverMessage, error) on the DriverMessageModel struct.
- Transactionality: Execute the key search completely inside a BadgerDB View (read-only) transaction.
- Key Generation: Compute the precise lookup key using the struct's prefix and the requested ID parameter:
m.Prefix + id (yielding driver-messages/{id}).
- Data Retrieval & Deserialization:
- Fetch the matched record inside the transaction context.
- Safely extract the byte array payload following BadgerDB's item lifecycle guidelines (e.g., calling
item.ValueCopy()).
- Unmarshal the raw JSON bytes back into a reference of
craneTypes.DriverMessage via the standard json.Unmarshal package.
- Error Handling: If the calculated key is missing from the database structure (
errors.Is(err, badger.ErrKeyNotFound)), return a descriptive custom error matching the context of the etcd legacy counterpart: fmt.Errorf("message not found: %v", err).
- Testing: Author a standalone unit test variant inside
internal/models/messages_test.go to validate successful data parsing, property integrity, and error routing scenarios against an in-memory BadgerDB engine instance.
Success Criteria
Description
Extend the
DriverMessageModelstruct by implementing a new standalone query method calledBadgerDBFindOne. To ensure backward compatibility and prevent downstream integration issues, do not refactor, rename, or drop the current etcd-basedFindOneimplementation. This method introduces native support to fetch and deserialize a specific driver message entry directly from BadgerDB.Requirements
internal/models/messages.goBadgerDBFindOne(id string) (*craneTypes.DriverMessage, error)on theDriverMessageModelstruct.m.Prefix + id(yieldingdriver-messages/{id}).item.ValueCopy()).craneTypes.DriverMessagevia the standardjson.Unmarshalpackage.errors.Is(err, badger.ErrKeyNotFound)), return a descriptive custom error matching the context of the etcd legacy counterpart:fmt.Errorf("message not found: %v", err).internal/models/messages_test.goto validate successful data parsing, property integrity, and error routing scenarios against an in-memory BadgerDB engine instance.Success Criteria
BadgerDBFindOnemethod is cleanly exposed on the target model alongside older data paths without modifying existing public API signatures.Viewroutine block.message not foundtext formatting.BadgerDBFindOnewith the matching tracking ID.