Files
tasks/common/persistence/cassandra/common.go
Stephan BehnkeandGitHub c9e360a3fb go fix (#9337)
## What changed?

Integrate Go 1.26's new `go fix` into workflow.

NOTE that the changes caused our linter to fire; a [separate
commit](https://github.com/temporalio/temporal/pull/9337/commits/1b23f787aef370098b35829c4d17dcc06230b4c1)
addresses those.

## Why?

Ensure Go code is standardized/modernized.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
2026-02-18 09:12:19 -08:00

62 lines
1.7 KiB
Go

package cassandra
import (
"fmt"
)
type (
// FieldNotFoundError is an error type returned when an untyped query return does not contain the expected fields.
FieldNotFoundError struct {
Msg string
}
)
func (f FieldNotFoundError) Error() string {
return f.Msg
}
func newFieldNotFoundError(fieldName string, payload map[string]any) error {
return &FieldNotFoundError{Msg: fmt.Sprintf("Unable to find field '%s' in payload - '%v'", fieldName, payload)}
}
type (
// PersistedTypeMismatchError is an error type returned when a persisted cassandra value does not match the expected type.
PersistedTypeMismatchError struct {
Msg string
}
)
func (f PersistedTypeMismatchError) Error() string {
return f.Msg
}
func newPersistedTypeMismatchError(
fieldName string,
expectedType any,
received any,
payload map[string]any,
) *PersistedTypeMismatchError {
return &PersistedTypeMismatchError{
Msg: fmt.Sprintf("Field '%s' is of type '%T' but expected type '%T' in payload - '%v'",
fieldName, received, expectedType, payload)}
}
// Returns a correctly typed value for fieldName retrieved from a row populated by a MapScan operation.
// Returns the zero value for the provided type and an appropriate error if the field is not found in
// the row or if the value cannot be cast to the provided type.
func getTypedFieldFromRow[T any](fieldName string, row map[string]any) (T, error) {
var zeroVal T // used as a placeholder for zero value of type T since we can't directly return nil
raw, ok := row[fieldName]
if !ok {
return zeroVal, newFieldNotFoundError(fieldName, row)
}
typed, ok := raw.(T)
if !ok {
return zeroVal, newPersistedTypeMismatchError(fieldName, typed, raw, row)
}
return typed, nil
}