Skip to content

Commit

Permalink
apply more feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Jan 3, 2024
1 parent 23c1ffa commit ea2e8e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
16 changes: 9 additions & 7 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package client

import (
"encoding/json"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -116,14 +117,11 @@ func NewDocFromMap(data map[string]any, sd SchemaDescription) (*Document, error)
return doc, nil
}

var jsonArrayPattern = regexp.MustCompile(`^\s*\[.*\]\s*$`)

// IsJSONArray returns true if the given byte array is a JSON Array.
func IsJSONArray(obj []byte) bool {
v, err := fastjson.ParseBytes(obj)
if err != nil {
return false
}
_, err = v.Array()
return err == nil
return jsonArrayPattern.Match(obj)
}

// NewFromJSON creates a new instance of a Document from a raw JSON object byte array.
Expand Down Expand Up @@ -320,8 +318,10 @@ func getArray[T any](
}

return arr, nil
case []T:
return val, nil
default:
return val.([]T), nil
return []T{}, nil
}
}

Expand Down Expand Up @@ -354,6 +354,8 @@ func getNillableArray[T any](
}

return arr, nil
case []immutable.Option[T]:
return val, nil
default:
return []immutable.Option[T]{}, nil
}
Expand Down
3 changes: 2 additions & 1 deletion http/client_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ func (c *Collection) Get(ctx context.Context, key client.DocKey, showDeleted boo
if err != nil {
return nil, err
}
doc, err := client.NewDocFromJSON(data, c.def.Schema)
doc := client.NewDocWithKey(key, c.def.Schema)
err = doc.SetWithJSON(data)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ea2e8e1

Please sign in to comment.