Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[State] Add missing Go examples #4127

Merged
merged 7 commits into from
May 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ Try getting state again. Note that no value is returned.

Below are code examples that leverage Dapr SDKs for saving and retrieving multiple states.

{{< tabs Dotnet Java Python Javascript "HTTP API (Bash)" "HTTP API (PowerShell)">}}
{{< tabs Dotnet Java Python Go Javascript "HTTP API (Bash)" "HTTP API (PowerShell)">}}

{{% codetab %}}

Expand Down Expand Up @@ -656,6 +656,53 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g

{{% codetab %}}

```go
// dependencies
import (
"context"
"log"
"math/rand"
"strconv"
"time"

dapr "github.com/dapr/go-sdk/client"
)

// code
func main() {
const STATE_STORE_NAME = "statestore"
rand.Seed(time.Now().UnixMicro())
for i := 0; i < 10; i++ {
orderId := rand.Intn(1000-1) + 1
client, err := dapr.NewClient()
if err != nil {
panic(err)
}
defer client.Close()
ctx := context.Background()
err = client.SaveState(ctx, STATE_STORE_NAME, "order_1", []byte(strconv.Itoa(orderId)), nil)
if err != nil {
panic(err)
}
keys := []string{"key1", "key2", "key3"}
items, err := client.GetBulkState(ctx, store, keys, nil, 100)
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

log.Println("Result after get:", string(result.Value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work, as this is an array of BulkStateItems, what's the objective of this println? show all the Items in the array? We might need to loop here.

Copy link
Collaborator Author

@hhunter-ms hhunter-ms May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@salaboy I'm least familiar with Go, so I'm not sure about the printIn haha I may have grabbed it when piecing together examples

Copy link
Member

@msfussell msfussell May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@salaboy
Yes, @hhunter-ms is asking the the equivalent of this below, but for bulkstate, which means iterating over each item and showing it's value. This is just for a simple example to show how the API works

		result, err := client.GetState(ctx, STATE_STORE_NAME, "order_1", nil)
		if err != nil {
			panic(err)
		}
		log.Println("Result after get:", string(result.Value))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, then @hhunter-ms something like this would work

                 items, err := client.GetBulkState(ctx, STATE_STORE_NAME, keys, nil, 100)
		if err != nil {
			panic(err)
		}
		for _, item := range items {
			log.Println("Item from GetBulkState:", string(item.Value))
		}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @salaboy! I've pushed that update

time.Sleep(2 * time.Second)
}
}
```

To launch a Dapr sidecar for the above example application, run a command similar to the following:

```bash
dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-grpc-port 60001 go run OrderProcessingService.go
```

{{% /codetab %}}

{{% codetab %}}

```javascript
//dependencies
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from '@dapr/dapr';
Expand Down Expand Up @@ -738,7 +785,7 @@ State transactions require a state store that supports multi-item transactions.

Below are code examples that leverage Dapr SDKs for performing state transactions.

{{< tabs Dotnet Java Python Javascript "HTTP API (Bash)" "HTTP API (PowerShell)">}}
{{< tabs Dotnet Java Python Go Javascript "HTTP API (Bash)" "HTTP API (PowerShell)">}}

{{% codetab %}}

Expand Down Expand Up @@ -893,6 +940,74 @@ dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-g

{{% codetab %}}

```go
// dependencies
import (
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved
"context"
"log"
"math/rand"
"strconv"
"time"

dapr "github.com/dapr/go-sdk/client"
)

// code
func main() {
const STATE_STORE_NAME = "statestore"
rand.Seed(time.Now().UnixMicro())
for i := 0; i < 10; i++ {
orderId := rand.Intn(1000-1) + 1
client, err := dapr.NewClient()
if err != nil {
panic(err)
}
defer client.Close()
ctx := context.Background()
err = client.SaveState(ctx, STATE_STORE_NAME, "order_1", []byte(strconv.Itoa(orderId)), nil)
if err != nil {
panic(err)
}
result, err := client.GetState(ctx, STATE_STORE_NAME, "order_1", nil)
if err != nil {
panic(err)
}

ops := make([]*dapr.StateOperation, 0)

op1 := &dapr.StateOperation{
Type: dapr.StateOperationTypeUpsert,
Item: &dapr.SetStateItem{
Key: "key1",
Value: []byte(data),
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved
},
}
op2 := &dapr.StateOperation{
Type: dapr.StateOperationTypeDelete,
Item: &dapr.SetStateItem{
Key: "key2",
},
}
ops = append(ops, op1, op2)
meta := map[string]string{}
err := testClient.ExecuteStateTransaction(ctx, store, meta, ops)
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

log.Println("Result after get:", string(result.Value))
time.Sleep(2 * time.Second)
}
}
```

To launch a Dapr sidecar for the above example application, run a command similar to the following:

```bash
dapr run --app-id orderprocessing --app-port 6001 --dapr-http-port 3601 --dapr-grpc-port 60001 go run OrderProcessingService.go
```

{{% /codetab %}}

{{% codetab %}}

```javascript
//dependencies
import { DaprClient, HttpMethod, CommunicationProtocolEnum } from '@dapr/dapr';
Expand Down
Loading