Skip to content

Commit

Permalink
chore: use updated repo generator v2 (#316)
Browse files Browse the repository at this point in the history
* chore: use updated repo generator v2


Co-authored-by: Chris Price <cprice404@users.noreply.github.com>
  • Loading branch information
bruuuuuuuce and cprice404 authored Jun 1, 2023
1 parent 651f787 commit f4927c0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 58 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ jobs:
token: ${{ secrets.MOMENTO_MACHINE_USER_GITHUB_TOKEN }}

- name: Verify README generation
uses: momentohq/standards-and-practices/github-actions/oss-readme-template@gh-actions-v1
uses: momentohq/standards-and-practices/github-actions/oss-readme-template@gh-actions-v2
with:
project_status: official
project_stability: stable
project_type: sdk
sdk_language: Go
usage_example_path: ./examples/scalar-example/main.go
template_file: ./README.template.md
output_file: ./README.md
dev_docs_slug: go

- name: Verify CONTRIBUTING generation
uses: momentohq/standards-and-practices/github-actions/oss-readme-template@gh-actions-v1
uses: momentohq/standards-and-practices/github-actions/oss-readme-template@gh-actions-v2
with:
project_status: official
project_stability: stable
project_type: other
sdk_language: Go
template_file: CONTRIBUTING.template.md
output_file: ./CONTRIBUTING.md
dev_docs_slug: go

test:
uses: ./.github/workflows/test.yml
Expand Down
13 changes: 8 additions & 5 deletions .github/workflows/on-push-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ jobs:
token: ${{ secrets.MOMENTO_MACHINE_USER_GITHUB_TOKEN }}

- name: Generate README
uses: momentohq/standards-and-practices/github-actions/generate-and-commit-oss-readme@gh-actions-v1
uses: momentohq/standards-and-practices/github-actions/generate-and-commit-oss-readme@gh-actions-v2
with:
project_status: official
project_stability: stable
project_type: sdk
sdk_language: Go
usage_example_path: ./examples/scalar-example/main.go
template_file: ./README.template.md
output_file: ./README.md
dev_docs_slug: go

- name: Generate CONTRIBUTING
uses: momentohq/standards-and-practices/github-actions/generate-and-commit-oss-readme@gh-actions-v1
uses: momentohq/standards-and-practices/github-actions/generate-and-commit-oss-readme@gh-actions-v2
with:
project_status: official
project_stability: stable
project_type: other
sdk_language: Go
template_file: CONTRIBUTING.template.md
output_file: CONTRIBUTING.md
template_file: ./CONTRIBUTING.template.md
output_file: ./CONTRIBUTING.md
dev_docs_slug: go

test:
needs: [readme]
Expand Down
60 changes: 10 additions & 50 deletions README.template.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,33 @@
{{ ossHeader }}

## Getting Started :running:
## Packages

### Requirements

- [Go](https://go.dev/dl/)
- A Momento Auth Token is required, you can generate one using
the [Momento CLI](https://github.com/momentohq/momento-cli)

### Examples

Check out full working code in the [examples](./examples/README.md) directory of this repository!

### Installation
The Momento Golang SDK is available here on github: [momentohq/client-sdk-go](https://github.com/momentohq/client-sdk-go).

```bash
go get github.com/momentohq/client-sdk-go
```

### Usage
## Usage

Checkout our [examples](./examples/README.md) directory for complete examples of how to use the SDK.

Here is a quickstart you can use in your own project:

```go
{{ usageExampleCode }}
{% include "./examples/readme.go" %}
```

### Error Handling
## Getting Started and Documentation

The preferred way of interpreting the return values from `CacheClient` methods is using a `switch` statement to match and handle the specific response type.
Here's a quick example:
Documentation is available on the [Momento Docs website](https://docs.momentohq.com).

```go
switch r := resp.(type) {
case *momento.GetHit:
log.Printf("Lookup resulted in cahce HIT. value=%s\n", r.ValueString())
default:
// you can handle other cases via pattern matching in other `switch case`, or a default case
// via the `default` block. For each return value your IDE should be able to give you code
// completion indicating the other possible "case"; in this case, `momento.GetMiss`.
}
```
## Examples

Using this approach, you get a type-safe `GetHit` object in the case of a cache hit.
But if the cache read results in a Miss, you'll also get a type-safe object that you can use to get more info about what happened.

In cases where you get an error response, it can be treated as `momentoErr` using `As` method and it always include an `momentoErr.Code` that you can use to check the error type:

```go
_, err := client.Set(ctx, &momento.SetRequest{
CacheName: cacheName,
Key: momento.String(key),
Value: momento.String(value),
})

if err != nil {
var momentoErr momento.MomentoError
if errors.As(err, &momentoErr) {
if momentoErr.Code() != momento.TimeoutError {
// this would represent a client-side timeout, and you could fall back to your original data source
}
}
}
```
Check out full working code in the [examples](./examples/README.md) directory of this repository!

### Tuning
## Developing

Coming soon...
If you are interested in contributing to the SDK, please see the [CONTRIBUTING](./CONTRIBUTING.md) docs.

{{ ossFooter }}
58 changes: 58 additions & 0 deletions examples/readme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"log"
"time"

"github.com/google/uuid"
"github.com/momentohq/client-sdk-go/auth"
"github.com/momentohq/client-sdk-go/config"
"github.com/momentohq/client-sdk-go/config/logger"
"github.com/momentohq/client-sdk-go/momento"
"github.com/momentohq/client-sdk-go/responses"
)

func main() {
context := context.Background()
configuration := config.LaptopLatestWithLogger(logger.NewNoopMomentoLoggerFactory()).WithClientTimeout(15 * time.Second)
credentialProvider, err := auth.NewEnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN")
if err != nil {
panic(err)
}
defaultTtl := time.Duration(9999)

client, err := momento.NewCacheClient(configuration, credentialProvider, defaultTtl)
if err != nil {
panic(err)
}

key := uuid.NewString()
value := uuid.NewString()
log.Printf("Setting key: %s, value: %s\n", key, value)
_, _ = client.Set(context, &momento.SetRequest{
CacheName: "cache-name",
Key: momento.String(key),
Value: momento.String(value),
Ttl: time.Duration(9999),
})

if err != nil {
panic(err)
}

resp, err := client.Get(context, &momento.GetRequest{
CacheName: "cache-name",
Key: momento.String(key),
})
if err != nil {
panic(err)
}

switch r := resp.(type) {
case *responses.GetHit:
log.Printf("Lookup resulted in cache HIT. value=%s\n", r.ValueString())
case *responses.GetMiss:
log.Printf("Look up did not find a value key=%s", key)
}
}

0 comments on commit f4927c0

Please sign in to comment.