Skip to content

Commit

Permalink
[UOD-1935] use 13 dotted value on empty name for message keys (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-gilles-ultra authored Jan 22, 2024
1 parent e453ed3 commit 1b4587d
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 3 deletions.
15 changes: 12 additions & 3 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,25 @@ type DecodeDBOp func(in *pbcodec.DBOp, blockNum uint32) (decodedDBOps *decodedDB

type ExtractKey func(*pbcodec.DBOp) string

// normalizeName return a 13 dotted string when the name value is empty otherwise it returns the given name
func normalizeName(name string) string {
if name == "" {
return "............."
} else {
return name
}
}

func extractFullKey(dbOp *pbcodec.DBOp) string {
return fmt.Sprintf("%s:%s", dbOp.Scope, dbOp.PrimaryKey)
return fmt.Sprintf("%s:%s", extractScope(dbOp), extractPrimaryKey(dbOp))
}

func extractScope(dbOp *pbcodec.DBOp) string {
return dbOp.Scope
return normalizeName(dbOp.Scope)
}

func extractPrimaryKey(dbOp *pbcodec.DBOp) string {
return dbOp.PrimaryKey
return normalizeName(dbOp.PrimaryKey)
}

func indexDbOps(gc ActionContext) []*IndexedEntry[*pbcodec.DBOp] {
Expand Down
167 changes: 167 additions & 0 deletions gen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package dkafka

import (
"fmt"
"testing"

pbcodec "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/codec/v1"
)

const emptyName = "............."

func Test_normalizeName(t *testing.T) {
type args struct {
name string
}
tests := []struct {
name string
args args
want string
}{
{
name: "non-empty",
args: args{
name: "eosio",
},
want: "eosio",
},
{
name: "empty",
args: args{
name: "",
},
want: emptyName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := normalizeName(tt.args.name); got != tt.want {
t.Errorf("normalizeName() = %v, want %v", got, tt.want)
}
})
}
}

func Test_extractFullKey(t *testing.T) {
type args struct {
dbOp *pbcodec.DBOp
}
tests := []struct {
name string
args args
want string
}{
{
name: "non-empty",
args: args{
dbOp: &pbcodec.DBOp{
Scope: "eosio",
PrimaryKey: "global",
},
},
want: "eosio:global",
},
{
name: "empty-scope",
args: args{
dbOp: &pbcodec.DBOp{
PrimaryKey: "global",
},
},
want: fmt.Sprintf("%s:global", emptyName),
},
{
name: "empty-primary-key",
args: args{
dbOp: &pbcodec.DBOp{
Scope: "eosio",
},
},
want: fmt.Sprintf("eosio:%s", emptyName),
},
{
name: "empty-scope-and-primary-key",
args: args{
dbOp: &pbcodec.DBOp{},
},
want: fmt.Sprintf("%s:%s", emptyName, emptyName),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := extractFullKey(tt.args.dbOp); got != tt.want {
t.Errorf("extractFullKey() = %v, want %v", got, tt.want)
}
})
}
}

func Test_extractScope(t *testing.T) {
type args struct {
dbOp *pbcodec.DBOp
}
tests := []struct {
name string
args args
want string
}{
{
name: "with-scope",
args: args{
dbOp: &pbcodec.DBOp{
Scope: "eosio",
},
},
want: "eosio",
},
{
name: "without-scope",
args: args{
dbOp: &pbcodec.DBOp{},
},
want: emptyName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := extractScope(tt.args.dbOp); got != tt.want {
t.Errorf("extractScope() = %v, want %v", got, tt.want)
}
})
}
}

func Test_extractPrimaryKey(t *testing.T) {
type args struct {
dbOp *pbcodec.DBOp
}
tests := []struct {
name string
args args
want string
}{
{
name: "with-primary-key",
args: args{
dbOp: &pbcodec.DBOp{
PrimaryKey: "global",
},
},
want: "global",
},
{
name: "without-primary-key",
args: args{
dbOp: &pbcodec.DBOp{},
},
want: emptyName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := extractPrimaryKey(tt.args.dbOp); got != tt.want {
t.Errorf("extractPrimaryKey() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 1b4587d

Please sign in to comment.