Skip to content

Commit 24e5eb5

Browse files
Deprecation of Single (#652)
* Deprecate Single in favour of SingleWithContext function for context-aware record fetching.
1 parent 2d9dd78 commit 24e5eb5

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

neo4j/result_helpers.go

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ func SingleT[T any](result Result, mapper func(*Record) (T, error)) (T, error) {
4747
return mapper(single)
4848
}
4949

50+
// SingleWithContext returns one and only one record from the result stream. Any error passed in
51+
// or reported while navigating the result stream is returned without any conversion.
52+
// If the result stream contains zero or more than one records error is returned.
53+
//
54+
// result, err := session.Run(...)
55+
// record, err := neo4j.SingleWithContext(ctx, result, err)
56+
//
57+
// It accepts a context.Context, which may be canceled or carry a deadline, to control the overall record fetching
58+
// execution time.
59+
func SingleWithContext(ctx context.Context, result ResultWithContext, err error) (*Record, error) {
60+
if err != nil {
61+
return nil, err
62+
}
63+
return result.Single(ctx)
64+
}
65+
66+
// Single returns one and only one record from the result stream. Any error passed in
67+
// or reported while navigating the result stream is returned without any conversion.
68+
// If the result stream contains zero or more than one records error is returned.
69+
//
70+
// record, err := neo4j.Single(session.Run(...))
71+
//
72+
// Deprecated: use SingleWithContext instead (the entry point of context-aware
73+
// APIs is NewDriverWithContext)
74+
func Single(result Result, err error) (*Record, error) {
75+
if err != nil {
76+
return nil, err
77+
}
78+
return result.Single()
79+
}
80+
5081
// CollectTWithContext maps the records to a slice of T with the provided mapper function.
5182
// It relies on ResultWithContext.Collect and propagate its error, if any.
5283
// It accepts a context.Context, which may be canceled or carry a deadline, to control the overall record fetching
@@ -72,16 +103,19 @@ func CollectT[T any](result Result, mapper func(*Record) (T, error)) ([]T, error
72103
return mapAll(records, mapper)
73104
}
74105

75-
// Single returns one and only one record from the result stream. Any error passed in
76-
// or reported while navigating the result stream is returned without any conversion.
77-
// If the result stream contains zero or more than one records error is returned.
106+
// CollectWithContext aggregates the records into a slice.
107+
// It relies on ResultWithContext.Collect and propagate its error, if any.
78108
//
79-
// record, err := neo4j.Single(session.Run(...))
80-
func Single(result Result, err error) (*Record, error) {
109+
// result, err := session.Run(...)
110+
// records, err := neo4j.CollectWithContext(ctx, result, err)
111+
//
112+
// It accepts a context.Context, which may be canceled or carry a deadline, to control the overall record fetching
113+
// execution time.
114+
func CollectWithContext(ctx context.Context, result ResultWithContext, err error) ([]*Record, error) {
81115
if err != nil {
82116
return nil, err
83117
}
84-
return result.Single()
118+
return result.Collect(ctx)
85119
}
86120

87121
// Collect aggregates the records into a slice.
@@ -98,21 +132,6 @@ func Collect(result Result, err error) ([]*Record, error) {
98132
return result.Collect()
99133
}
100134

101-
// CollectWithContext aggregates the records into a slice.
102-
// It relies on ResultWithContext.Collect and propagate its error, if any.
103-
//
104-
// result, err := session.Run(...)
105-
// records, err := neo4j.CollectWithContext(ctx, result, err)
106-
//
107-
// It accepts a context.Context, which may be canceled or carry a deadline, to control the overall record fetching
108-
// execution time.
109-
func CollectWithContext(ctx context.Context, result ResultWithContext, err error) ([]*Record, error) {
110-
if err != nil {
111-
return nil, err
112-
}
113-
return result.Collect(ctx)
114-
}
115-
116135
// AsRecords passes any existing error or casts from to a slice of records.
117136
// Use in combination with Collect and transactional functions:
118137
//

0 commit comments

Comments
 (0)