-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SubscriptionDataSource allows querying of objects directly from the subscription results array. It is beneficial to use this data source when it is useful to query by name, possibly allowing earlier data sources to pick up the query first (e.g. local mocking). I've also updated the local fixed data source to be a prepend action instead of append, to ensure that any local mocks are always handled first. With this, the order of options registered should not impact query behaviour.
- Loading branch information
Paul Norton
committed
Jan 12, 2024
1 parent
2475242
commit a9329a9
Showing
3 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"olympos.io/encoding/edn" | ||
) | ||
|
||
// SubscriptionDataSource allows querying of objects directly from the subscription results array. | ||
// It is beneficial to use this data source when it is useful to query by name, | ||
// possibly allowing earlier data sources to pick up the query first. | ||
type SubscriptionDataSource struct { | ||
queryIndexes map[string]int | ||
subscriptionResults [][]edn.RawMessage | ||
} | ||
|
||
func NewSubscriptionDataSource(queryIndexes map[string]int, subscriptionResults [][]edn.RawMessage) SubscriptionDataSource { | ||
return SubscriptionDataSource{ | ||
queryIndexes: queryIndexes, | ||
subscriptionResults: subscriptionResults, | ||
} | ||
} | ||
|
||
func (ds SubscriptionDataSource) Query(_ context.Context, queryName string, _ string, _ map[string]interface{}, output interface{}) (*QueryResponse, error) { | ||
ix, ok := ds.queryIndexes[queryName] | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
if ix >= len(ds.subscriptionResults) { | ||
return nil, fmt.Errorf("can't get subscription query %s (index %d) from result length %d", queryName, ix, len(ds.subscriptionResults)) | ||
} | ||
|
||
err := edn.Unmarshal(ds.subscriptionResults[0][ix], output) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &QueryResponse{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters