diff --git a/CHANGELOG.md b/CHANGELOG.md index 34d1816..0f27c5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ - `Query`, `MustQuery`, and `UpdateOne` helpers (+ Context variants). - `WithTxValue` for transactional functions that return a value. - `QueryCount`, `BuildQueryCountFn`, and `CountResult` for pagination counts. -- `StringKSUIDExpr`, `BinaryKSUIDExpr`, and flexible `Parse*Any` KSUID parsers. +- `StringKSUIDExpr`, `BinaryKSUIDExpr`, `ExprPtrs`, and flexible `Parse*Any` KSUID parsers. - `MarshalUint64` and `UnmarshalUint64` for JS-safe GraphQL uint64 scalars. - Context-aware variants for all query and mutation helpers. - `Delete`, `DeleteAffected`, `WithTx`, `ContainsCol`, and `CurrentDialect()`. diff --git a/README.md b/README.md index 7b7ddc3..4a272f5 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ For Postgres, use `dbx.DialectPostgres` and blank-import `dbxp` instead of `dbxm | Query | `Fetch`, `MustFetch`, `FetchOne`, `MustFetchOne`, `Query`, `MustQuery` (+ `*Context` variants) | | Mutations | `Insert`, `InsertReturning`, `Update`, `UpdateAffected`, `UpdateOne`, `UpdateReturning`, `Delete`, `DeleteAffected` (+ `*Context` variants) | | Transactions | `WithTx`, `WithTxValue` | -| Columns | `NormalCols`, `ContainsCol`, `ExprValues`, `ExprStringers`, `StringKSUIDExpr`, `BinaryKSUIDExpr`, `QueryCount`, `BuildQueryCountFn` | +| Columns | `NormalCols`, `ContainsCol`, `ExprValues`, `ExprPtrs`, `ExprStringers`, `StringKSUIDExpr`, `BinaryKSUIDExpr`, `QueryCount`, `BuildQueryCountFn` | | Partial update | `ApplyPtr`, `ApplyComplexPtr`, `ApplyInterfacePtr`, `ApplyVal` | | Pointers | `Ptr`, `Val`, `NowPtr`, `TrimPtr`, `TrimPtrToNil`, `IsZero` | | Types | `StringKSUID`, `BinaryKSUID`, `JSONB` | diff --git a/doc.go b/doc.go index 25df5d8..6988b89 100644 --- a/doc.go +++ b/doc.go @@ -17,7 +17,7 @@ // // Dialect-neutral type aliases ([Column], [ColumnList]) and helpers for column // lists ([NormalCols], [ContainsCol]) and expression building ([ExprValues], -// [StringKSUIDExpr], [BinaryKSUIDExpr]). +// [ExprPtrs], [StringKSUIDExpr], [BinaryKSUIDExpr]). // [QueryCount] and [BuildQueryCountFn] support pagination total counts. // // Partial-update helpers ([ApplyPtr], [ApplyVal]) track changed fields for diff --git a/jet_expr.go b/jet_expr.go index 373b184..6027abb 100644 --- a/jet_expr.go +++ b/jet_expr.go @@ -16,6 +16,19 @@ func ExprValues[T any](values []T, f func(T) Expression) []Expression { return expressions } +// ExprPtrs converts a list of pointers to Expression values, skipping nil +// entries. Function f receives the dereferenced value. +func ExprPtrs[T any](values []*T, f func(T) Expression) []Expression { + expressions := make([]Expression, 0, len(values)) + for _, v := range values { + if v == nil { + continue + } + expressions = append(expressions, f(*v)) + } + return expressions +} + // ExprStringers converts a list of fmt.Stringers to a list of Expression values. func ExprStringers(values []fmt.Stringer) []Expression { expressions := make([]Expression, len(values)) diff --git a/jet_expr_test.go b/jet_expr_test.go index 5500168..7400aa9 100644 --- a/jet_expr_test.go +++ b/jet_expr_test.go @@ -29,6 +29,20 @@ func TestBinaryKSUIDExpr(t *testing.T) { assert.Equal(t, nilExpr, NilBinaryKSUID.Expr()) } +func TestExprPtrs_StringKSUID(t *testing.T) { + a := NewStringKSUID() + b := NewStringKSUID() + exprs := ExprPtrs([]*StringKSUID{&a, nil, &b}, StringKSUIDExpr) + require.Len(t, exprs, 2) +} + +func TestExprPtrs_BinaryKSUID(t *testing.T) { + a := NewBinaryKSUID() + exprs := ExprPtrs([]*BinaryKSUID{&a, nil}, BinaryKSUIDExpr) + require.Len(t, exprs, 1) + assert.NotNil(t, exprs[0]) +} + func TestExprValues_StringKSUID(t *testing.T) { a := NewStringKSUID() b := NewStringKSUID()