Skip to content

Commit

Permalink
add strong typing to document creation
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Jan 2, 2024
1 parent b774ff1 commit 709909e
Show file tree
Hide file tree
Showing 74 changed files with 1,177 additions and 1,050 deletions.
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 @@ Example: create from stdin
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) {
docs, err := client.NewDocsFromJSON(docData, col.Schema())
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
}
return col.Create(cmd.Context(), doc)
},
}
cmd.Flags().StringVarP(&file, "file", "f", "", "File containing document(s)")
Expand Down
2 changes: 1 addition & 1 deletion cli/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Example: update by keys
if err != nil {
return err
}
if err := doc.SetWithJSON([]byte(args[0])); err != nil {
if err := doc.SetWithJSON([]byte(args[0]), col.Schema()); err != nil {
return err
}
return col.Update(cmd.Context(), doc)
Expand Down
Loading

0 comments on commit 709909e

Please sign in to comment.