-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(key): WithParentKey added + WithIntID + WithStringID + WithKeyID
- Loading branch information
1 parent
7d9a076
commit 636dc02
Showing
4 changed files
with
130 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters