Skip to content

Commit

Permalink
Merge pull request #60 from FootprintAI/feat---allow-customize-docid
Browse files Browse the repository at this point in the history
feat: allow customize docid. Return empty Collection when cid is not found
  • Loading branch information
hsinatfootprintai authored Oct 27, 2024
2 parents 3b5b8a3 + d62e82f commit b3e5da4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
33 changes: 28 additions & 5 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,22 @@ func (r *RestColServiceServerService) GetCollection(ctx context.Context, req *ap
cid = collectionsmodel.NewCollectionIDFromStr(req.CollectionId)
mc, err := r.collectionCURD.GetLatestSchema(ctx, "", cid)
if err != nil {
ismyerr, myerr := sderrors.As(err)
if ismyerr && myerr.Code() == sderrors.CodeNotFound {
return &apppb.GetCollectionResponse{}, nil
}
return nil, err
}
resp := &apppb.GetCollectionResponse{
XMetadata: collectionsmodel.NewPbCollectionMetadata(mc),
Description: mc.Summary,
CollectionType: mc.Type.Proto(),
Schemas: collectionsmodel.NewPbSchemaFields(mc.Schemas[0]),
XMetadata: collectionsmodel.NewPbCollectionMetadata(mc),
}
if mc == nil {
return resp, nil
}
resp.Description = mc.Summary
resp.CollectionType = mc.Type.Proto()
if mc.Schemas != nil {
resp.Schemas = collectionsmodel.NewPbSchemaFields(mc.Schemas[0])
}
return resp, nil
}
Expand Down Expand Up @@ -230,9 +239,18 @@ func (r *RestColServiceServerService) CreateDocument(ctx context.Context, req *a
docSchema = inputDataSchema
}
}
var docId documentsmodel.DocumentID
if req.DocumentId == nil {
docId = documentsmodel.NewDocumentID()
} else {
docId, err = documentsmodel.Parse(*req.DocumentId)
if err != nil {
return nil, err
}
}

docModel := &documentsmodel.ModelDocument{
ID: documentsmodel.NewDocumentID(),
ID: docId,
Data: documentsmodel.NewModelDocumentData(valueHolder),
ModelCollectionID: cid,
ModelCollection: collectionsmodel.NewModelCollection(
Expand Down Expand Up @@ -279,6 +297,11 @@ func (r *RestColServiceServerService) GetDocument(ctx context.Context, req *appp

func (r *RestColServiceServerService) filterDocWithSelectedFields(doc *documentsmodel.ModelDocument, selectedFields []string) (*structpb.Value, error) {
r.log.Info("query doc with fields:%+v\n", selectedFields)

if doc.Data == nil {
return nil, nil
}

if len(selectedFields) == 0 {
// no selectedFields, return all

Expand Down
20 changes: 11 additions & 9 deletions pkg/models/documents/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func (m ModelDocument) TableName() string {
return "restcol-documents"
}

type DocumentID uuid.UUID
type DocumentID struct {
S string
}

func (d DocumentID) String() string {
return uuid.UUID(d).String()
return d.S
}

var (
Expand All @@ -63,18 +65,18 @@ func (d *DocumentID) Scan(value interface{}) error {
}

func Parse(s string) (DocumentID, error) {
innerUuid, err := uuid.Parse(s)
if err != nil {
fmt.Printf("parse doc uuid failed, err:+%v, s:%s\n", err, s)
return DocumentID{}, err
}
return DocumentID(innerUuid), nil
//innerUuid, err := uuid.Parse(s)
//if err != nil {
// fmt.Printf("parse doc uuid failed, err:+%v, s:%s\n", err, s)
// return DocumentID{}, err
//}
return DocumentID{S: s}, nil

}

func NewDocumentID() DocumentID {
uid, _ := uuid.NewV7()
return DocumentID(uid)
return DocumentID{S: uid.String()}
}

type ModelDocumentData struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *CollectionCURD) Get(ctx context.Context, tableName string, cid appmodel
}
if err := db.
Where("id = ?", cid.String()).
Find(record).Error; err != nil {
First(record).Error; err != nil {
return nil, storage.WrapStorageError(err)
}
return record, nil
Expand Down

0 comments on commit b3e5da4

Please sign in to comment.