Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: accept any non-empty string as a valid document ID #161

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,31 @@ func TestCreateCollectionByQuery(t *testing.T) {
})
}

func TestCustomID(t *testing.T) {
runCloverTest(t, func(t *testing.T, db *c.DB) {
collection := "test_custom_id"
err := db.CreateCollection(collection)
require.NoError(t, err)

id := "custom_id"
m := map[string]interface{}{
"_id": id,
"itWorks?": true,
}
doc := d.NewDocument()
doc.SetAll(m)

insertedID, err := db.InsertOne(collection, doc)
require.NoError(t, err)
require.Equal(t, id, insertedID)

foundDoc, err := db.FindById(collection, id)
require.NoError(t, err)
require.NotNil(t, foundDoc)
require.Equal(t, m, foundDoc.AsMap())
})
}

/*
func TestInMemoryMode(t *testing.T) {
db, err := c.Open("clover-db", c.InMemoryMode(true))
Expand Down
4 changes: 1 addition & 3 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"
"time"

"github.com/gofrs/uuid/v5"
"github.com/ostafen/clover/v2/internal"
"github.com/ostafen/clover/v2/util"
)
Expand Down Expand Up @@ -170,8 +169,7 @@ func (doc *Document) Unmarshal(v interface{}) error {
}

func isValidObjectId(id string) bool {
_, err := uuid.FromString(id)
return err == nil
return id != ""
}

func Validate(doc *Document) error {
Expand Down