Skip to content

Commit 0a1bb0f

Browse files
authored
[docs] Add query state and actors examples (#431)
* add some examples - need review Signed-off-by: Hannah Hunter <[email protected]> * attempt to add go examples for query state and actors Signed-off-by: Hannah Hunter <[email protected]> * move to a different branch Signed-off-by: Hannah Hunter <[email protected]> * add back crypto Signed-off-by: Hannah Hunter <[email protected]> --------- Signed-off-by: Hannah Hunter <[email protected]>
1 parent 04dc71f commit 0a1bb0f

File tree

1 file changed

+100
-2
lines changed
  • daprdocs/content/en/go-sdk-docs/go-client

1 file changed

+100
-2
lines changed

daprdocs/content/en/go-sdk-docs/go-client/_index.md

+100-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,44 @@ meta := map[string]string{}
142142
err := testClient.ExecuteStateTransaction(ctx, store, meta, ops)
143143
```
144144

145-
For a full guide on state management, visit [How-To: Save & get state]({{< ref howto-get-save-state.md >}}).
145+
Retrieve, filter, and sort key/value data stored in your statestore using `QueryState`.
146+
147+
```go
148+
// Define the query string
149+
query := `{
150+
"filter": {
151+
"EQ": { "value.Id": "1" }
152+
},
153+
"sort": [
154+
{
155+
"key": "value.Balance",
156+
"order": "DESC"
157+
}
158+
]
159+
}`
160+
161+
// Use the client to query the state
162+
queryResponse, err := c.QueryState(ctx, "querystore", query)
163+
if err != nil {
164+
log.Fatal(err)
165+
}
166+
167+
fmt.Printf("Got %d\n", len(queryResponse))
146168

169+
for _, account := range queryResponse {
170+
var data Account
171+
err := account.Unmarshal(&data)
172+
if err != nil {
173+
log.Fatal(err)
174+
}
175+
176+
fmt.Printf("Account: %s has %f\n", data.ID, data.Balance)
177+
}
178+
```
179+
180+
> **Note:** Query state API is currently in alpha
181+
182+
For a full guide on state management, visit [How-To: Save & get state]({{< ref howto-get-save-state.md >}}).
147183

148184
### Publish Messages
149185
To publish data onto a topic, the Dapr Go client provides a simple method:
@@ -159,6 +195,7 @@ For a full guide on pub/sub, visit [How-To: Publish & subscribe]({{< ref howto-p
159195

160196
### Output Bindings
161197

198+
162199
The Dapr Go client SDK provides two methods to invoke an operation on a Dapr-defined binding. Dapr supports input, output, and bidirectional bindings.
163200

164201
For simple, output-only binding:
@@ -183,6 +220,67 @@ out, err := client.InvokeBinding(ctx, in)
183220

184221
For a full guide on output bindings, visit [How-To: Use bindings]({{< ref howto-bindings.md >}}).
185222

223+
### Actors
224+
225+
Use the Dapr Go client SDK to write actors.
226+
227+
```go
228+
// MyActor represents an example actor type.
229+
type MyActor struct {
230+
actors.Actor
231+
}
232+
233+
// MyActorMethod is a method that can be invoked on MyActor.
234+
func (a *MyActor) MyActorMethod(ctx context.Context, req *actors.Message) (string, error) {
235+
log.Printf("Received message: %s", req.Data)
236+
return "Hello from MyActor!", nil
237+
}
238+
239+
func main() {
240+
// Create a Dapr client
241+
daprClient, err := client.NewClient()
242+
if err != nil {
243+
log.Fatal("Error creating Dapr client: ", err)
244+
}
245+
246+
// Register the actor type with Dapr
247+
actors.RegisterActor(&MyActor{})
248+
249+
// Create an actor client
250+
actorClient := actors.NewClient(daprClient)
251+
252+
// Create an actor ID
253+
actorID := actors.NewActorID("myactor")
254+
255+
// Get or create the actor
256+
err = actorClient.SaveActorState(context.Background(), "myactorstore", actorID, map[string]interface{}{"data": "initial state"})
257+
if err != nil {
258+
log.Fatal("Error saving actor state: ", err)
259+
}
260+
261+
// Invoke a method on the actor
262+
resp, err := actorClient.InvokeActorMethod(context.Background(), "myactorstore", actorID, "MyActorMethod", &actors.Message{Data: []byte("Hello from client!")})
263+
if err != nil {
264+
log.Fatal("Error invoking actor method: ", err)
265+
}
266+
267+
log.Printf("Response from actor: %s", resp.Data)
268+
269+
// Wait for a few seconds before terminating
270+
time.Sleep(5 * time.Second)
271+
272+
// Delete the actor
273+
err = actorClient.DeleteActor(context.Background(), "myactorstore", actorID)
274+
if err != nil {
275+
log.Fatal("Error deleting actor: ", err)
276+
}
277+
278+
// Close the Dapr client
279+
daprClient.Close()
280+
}
281+
```
282+
283+
For a full guide on actors, visit [the Actors building block documentation]({{< ref actors >}}).
186284

187285
### Secret Management
188286

@@ -320,4 +418,4 @@ out, err := sdkClient.Decrypt(context.Background(), rf, dapr.EncryptOptions{
320418
For a full guide on cryptography, visit [How-To: Use the cryptography APIs]({{< ref howto-cryptography.md >}}).
321419

322420
## Related links
323-
[Go SDK Examples](https://github.com/dapr/go-sdk/tree/main/examples)
421+
[Go SDK Examples](https://github.com/dapr/go-sdk/tree/main/examples)

0 commit comments

Comments
 (0)