Skip to content

Commit

Permalink
Example proto for incremented float values
Browse files Browse the repository at this point in the history
- Fix parsing `OK` value returned from the storage
  • Loading branch information
ehsundar committed Dec 30, 2023
1 parent 169344d commit bbb90cf
Show file tree
Hide file tree
Showing 12 changed files with 491 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/protoc-gen-go-kvstore/internal/generators/kvstore.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *{{$codeSafeName|untitle}}Storage) Set(ctx context.Context, key *{{$pair
return 0, err
}

if v != "" {
if v != "" && v != "OK" {
{{if $pairSpecs.ValueSpecs.NumericInt}}
return strconv.ParseInt(v, 10, 64)
{{else}}
Expand Down Expand Up @@ -213,7 +213,7 @@ func (s *{{$codeSafeName|untitle}}Storage) Set(ctx context.Context, key *{{$pair
return nil, err
}

if v != "" {
if v != "" && v != "OK" {
msg := &{{$pairSpecs.ValueSpecs.MessageName}}{}
err = msg.unmarshal(v)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions examples/balance/app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

func main() {
// see test file to usage examples
// you may try your custom usage of kvstore here
return
}
53 changes: 53 additions & 0 deletions examples/balance/app/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"testing"

"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/suite"

"github.com/ehsundar/kvstore"
"github.com/ehsundar/kvstore/examples/balance"
)

type BalanceTestSuite struct {
suite.Suite

r *redis.Client
}

func TestBalanceTestSuite(t *testing.T) {
suite.Run(t, new(BalanceTestSuite))
}

func (s *BalanceTestSuite) SetupSuite() {
r := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: "localhost:6379",
ClientName: "tests",
})

_, err := r.FlushAll(context.Background()).Result()
s.Nil(err)

s.r = r
}

func (s *BalanceTestSuite) TearDownTest() {
_, err := s.r.FlushAll(context.Background()).Result()
s.Nil(err)
}

func (s *BalanceTestSuite) TestShouldReadWriteToStorageRespectingTTL() {
ctx := context.Background()
storage := balance.NewBalanceStore(s.r)

bal, err := storage.Set(ctx, &balance.BalanceKey{Username: "username"}, 11.11, kvstore.WithRetrieveDisabled())
s.Nil(err)
s.InDelta(0, bal, 0.001)

bal, err = storage.Incr(ctx, &balance.BalanceKey{Username: "username"}, -3.01)
s.Nil(err)
s.InDelta(8.1, bal, 0.001)
}
211 changes: 211 additions & 0 deletions examples/balance/balance.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/balance/balance.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

package balance;

option go_package = "github.com/ehsundar/kvstore/examples/balance";

import "kvstore/options.proto";

message BalanceKey {
option (kvstore.key_options) = {};

string username = 1;
}

message BalanceValue {
option (kvstore.value_options) = {
numeral: {}
};

double balance = 1;
}
Loading

0 comments on commit bbb90cf

Please sign in to comment.