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

refactor: Add strong typing to document creation #2161

33 changes: 8 additions & 25 deletions cli/collection_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package cli

import (
"encoding/json"
"io"
"os"

Expand Down Expand Up @@ -66,35 +65,19 @@
return ErrNoDocOrFile
}

var docMap any
if err := json.Unmarshal(docData, &docMap); err != nil {
return err
}

switch t := docMap.(type) {
case map[string]any:
doc, err := client.NewDocFromMap(t)
if client.IsJSONArray(docData) {
fredcarle marked this conversation as resolved.
Show resolved Hide resolved
docs, err := client.NewDocsFromJSON(docData, col.Schema())

Check warning on line 69 in cli/collection_create.go

View check run for this annotation

Codecov / codecov/patch

cli/collection_create.go#L69

Added line #L69 was not covered by tests
if err != nil {
return err
}
return col.Create(cmd.Context(), doc)
case []any:
docs := make([]*client.Document, len(t))
for i, v := range t {
docMap, ok := v.(map[string]any)
if !ok {
return ErrInvalidDocument
}
doc, err := client.NewDocFromMap(docMap)
if err != nil {
return err
}
docs[i] = doc
}
return col.CreateMany(cmd.Context(), docs)
default:
return ErrInvalidDocument
}

doc, err := client.NewDocFromJSON(docData, col.Schema())
if err != nil {
return err
}

Check warning on line 79 in cli/collection_create.go

View check run for this annotation

Codecov / codecov/patch

cli/collection_create.go#L78-L79

Added lines #L78 - L79 were not covered by tests
return col.Create(cmd.Context(), doc)
},
}
cmd.Flags().StringVarP(&file, "file", "f", "", "File containing document(s)")
Expand Down
Loading
Loading