Skip to content

Commit

Permalink
fix(key): WithParentKey added + WithIntID + WithStringID + WithKeyID
Browse files Browse the repository at this point in the history
  • Loading branch information
trakhimenok committed Sep 11, 2023
1 parent 7d9a076 commit 636dc02
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 71 deletions.
24 changes: 0 additions & 24 deletions dal/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,6 @@ func (k *Key) Validate() error {
return nil
}

// KeyOption defines contract for key option
type KeyOption = func(*Key) error

func setKeyOptions(key *Key, options ...KeyOption) error {
for _, o := range options {
if err := o(key); err != nil {
return err
}
}
return nil
}

func NewKeyWithParentAndID[T comparable](parent *Key, collection string, id T) (key *Key) {
key = NewKeyWithID(collection, id)
key.parent = parent
Expand All @@ -169,18 +157,6 @@ func NewKeyWithID[T comparable](collection string, id T) (key *Key) {
return key
}

// NewKeyWithOptions creates a new key with an ID
func NewKeyWithOptions(collection string, options ...KeyOption) (key *Key, err error) {
if collection == "" {
return nil, errors.New("collection is a required parameter")
}
key = &Key{collection: collection}
if err = setKeyOptions(key, options...); err != nil {
return nil, err
}
return key, err
}

func NewIncompleteKey(collection string, idKind reflect.Kind, parent *Key) *Key {
if idKind == reflect.Invalid {
panic("idKind == reflect.Invalid")
Expand Down
90 changes: 90 additions & 0 deletions dal/key_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package dal

import (
"context"
"errors"
"github.com/strongo/random"
"reflect"
)

// KeyOption defines contract for key option
type KeyOption = func(*Key) error

func setKeyOptions(key *Key, options ...KeyOption) error {
for _, o := range options {
if err := o(key); err != nil {
return err
}
}
return nil
}

// NewKeyWithOptions creates a new key with an ID
func NewKeyWithOptions(collection string, options ...KeyOption) (key *Key, err error) {
if collection == "" {
return nil, errors.New("collection is a required parameter")
}
key = &Key{collection: collection}
if err = setKeyOptions(key, options...); err != nil {
return nil, err
}
return key, err
}

var DefaultRandomStringIDLength = 16

// WithRandomStringID sets ID generator to random string
func WithRandomStringID(options ...randomStringOption) KeyOption {
var rso randomStringOptions
for _, setOption := range options {
setOption(&rso)
}
return func(key *Key) error {
key.IDKind = reflect.String
var ctx context.Context = nil // intentionally nil as not required by any option
return WithIDGenerator(ctx, func(_ context.Context, record Record) error {
length := rso.Length()
prefix := rso.Prefix()
key.ID = prefix + random.ID(length)
return nil
})(key)
}
}

//// WithParent sets Parent
//func WithParent[T comparable](collection string, id T, options ...KeyOption) KeyOption {
// return func(key *Key) (err error) {
// options = append(options, WithID(id))
// key.parent, err = NewKeyWithOptions(collection, options...)
// return err
// }
//}

// WithParentKey sets Parent key
func WithParentKey(parent *Key) KeyOption {
if parent == nil {
panic("parent == nil")
}
return func(key *Key) error {
key.parent = parent
return nil
}
}

// WithStringID sets ID as a predefined string
func WithStringID(id string) KeyOption {
return WithKeyID(id)
}

// WithIntID sets ID as a predefined int
func WithIntID(id int) KeyOption {
return WithKeyID(id)
}

// WithKeyID sets ID as a predefined value. It's advised to use WithIntID and WithStringID when possible.
func WithKeyID[T comparable](id T) KeyOption {
return func(key *Key) error {
key.ID = id
return nil
}
}
40 changes: 40 additions & 0 deletions dal/key_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dal

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestWithParentKey(t *testing.T) {
t.Run("nil_should_panic", func(t *testing.T) {
assert.Panics(t, func() {
WithParentKey(nil)
})
})
t.Run("not_nil_should_pass", func(t *testing.T) {
parentKey := NewKeyWithID("parent1", "id1")
option := WithParentKey(parentKey)
key := new(Key)
err := option(key)
assert.Nil(t, err)
assert.Same(t, parentKey, key.parent)
})
}

func TestWithStringID(t *testing.T) {
const id = "id1"
option := WithStringID(id)
key := new(Key)
err := option(key)
assert.Nil(t, err)
assert.Equal(t, id, key.ID)
}

func TestWithIntID(t *testing.T) {
const id = 123
option := WithIntID(id)
key := new(Key)
err := option(key)
assert.Nil(t, err)
assert.Equal(t, id, key.ID)
}
47 changes: 0 additions & 47 deletions dal/tx_inserter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package dal
import (
"context"
"fmt"
"github.com/strongo/random"
"reflect"
)

// Inserter defines a function to insert a single record into database
Expand Down Expand Up @@ -94,51 +92,6 @@ func WithIDGenerator(ctx context.Context, g IDGenerator) KeyOption {
}
}

var DefaultRandomStringIDLength = 16

// WithRandomStringID sets ID generator to random string
func WithRandomStringID(options ...randomStringOption) KeyOption {
var rso randomStringOptions
for _, setOption := range options {
setOption(&rso)
}
return func(key *Key) error {
key.IDKind = reflect.String
var ctx context.Context = nil // intentionally nil as not required by any option
return WithIDGenerator(ctx, func(_ context.Context, record Record) error {
length := rso.Length()
prefix := rso.Prefix()
key.ID = prefix + random.ID(length)
return nil
})(key)
}
}

//// WithParent sets Parent
//func WithParent[T comparable](collection string, id T, options ...KeyOption) KeyOption {
// return func(key *Key) (err error) {
// options = append(options, WithID(id))
// key.parent, err = NewKeyWithOptions(collection, options...)
// return err
// }
//}

//// WithParentKey sets Parent key
//func WithParentKey(parent *Key) KeyOption {
// return func(key *Key) error {
// key.parent = parent
// return nil
// }
//}

// WithStringID sets ID as a predefined string
func WithStringID(id string) KeyOption {
return func(key *Key) error {
key.ID = id
return nil
}
}

// InsertWithRandomID inserts a record with a random ID
func InsertWithRandomID(
ctx context.Context,
Expand Down

0 comments on commit 636dc02

Please sign in to comment.