Skip to content

Commit

Permalink
refactor: Add strong typing to document creation (sourcenetwork#2161)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves sourcenetwork#935 
Resolves sourcenetwork#1703 

## Description

This PR adds strong document typing to document creation by using the
field descriptions to determine Go types. Datetime is now properly
supported and formatting is enforced.

Note that a lot of docIDs have changed as a result of this refactor.
  • Loading branch information
fredcarle authored Jan 5, 2024
1 parent ca215f3 commit e580868
Show file tree
Hide file tree
Showing 80 changed files with 1,261 additions and 1,199 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
Loading

0 comments on commit e580868

Please sign in to comment.