Skip to content

Commit

Permalink
Merge GetSchemasByID into GetSchemas
Browse files Browse the repository at this point in the history
Function preserved as syntax sugar as the return type is handy
  • Loading branch information
AndrewSisley committed Feb 16, 2024
1 parent 8b1fecf commit d1e0691
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 63 deletions.
35 changes: 13 additions & 22 deletions cli/schema_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,20 @@ Example: view a single schema by version id
RunE: func(cmd *cobra.Command, args []string) error {
store := mustGetContextStore(cmd)

var schemas []client.SchemaDescription
switch {
case versionID != "":
schema, err := store.GetSchemaByVersionID(cmd.Context(), versionID)
if err != nil {
return err
}
return writeJSON(cmd, schema)

default:
options := client.SchemaFetchOptions{}
if root != "" {
options.Root = immutable.Some(root)
}
if name != "" {
options.Name = immutable.Some(name)
}
options := client.SchemaFetchOptions{}
if versionID != "" {
options.ID = immutable.Some(versionID)
}
if root != "" {
options.Root = immutable.Some(root)
}
if name != "" {
options.Name = immutable.Some(name)
}

s, err := store.GetSchemas(cmd.Context(), options)
if err != nil {
return err
}
schemas = s
schemas, err := store.GetSchemas(cmd.Context(), options)
if err != nil {
return err
}

return writeJSON(cmd, schemas)
Expand Down
3 changes: 3 additions & 0 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,7 @@ type SchemaFetchOptions struct {

// If provided, only schemas with this name will be returned.
Name immutable.Option[string]

// If provided, only the schema with this id will be returned.
ID immutable.Option[string]
}
18 changes: 17 additions & 1 deletion db/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ func (db *db) getSchemaByVersionID(
txn datastore.Txn,
versionID string,
) (client.SchemaDescription, error) {
return description.GetSchemaVersion(ctx, txn, versionID)
schemas, err := db.getSchemas(ctx, txn, client.SchemaFetchOptions{ID: immutable.Some(versionID)})
if err != nil {
return client.SchemaDescription{}, err
}

// schemas will always have length == 1 here
return schemas[0], nil
}

func (db *db) getSchemas(
Expand All @@ -288,6 +294,13 @@ func (db *db) getSchemas(
schemas := []client.SchemaDescription{}

switch {
case options.ID.HasValue():
schema, err := description.GetSchemaVersion(ctx, txn, options.ID.Value())
if err != nil {
return nil, err
}
schemas = append(schemas, schema)

case options.Root.HasValue():
var err error
schemas, err = description.GetSchemasByRoot(ctx, txn, options.Root.Value())
Expand All @@ -306,6 +319,9 @@ func (db *db) getSchemas(

result := []client.SchemaDescription{}
for _, schema := range schemas {
if options.Root.HasValue() && schema.Root != options.Root.Value() {
continue

Check warning on line 323 in db/schema.go

View check run for this annotation

Codecov / codecov/patch

db/schema.go#L323

Added line #L323 was not covered by tests
}
if options.Name.HasValue() && schema.Name != options.Name.Value() {
continue

Check warning on line 326 in db/schema.go

View check run for this annotation

Codecov / codecov/patch

db/schema.go#L326

Added line #L326 was not covered by tests
}
Expand Down
16 changes: 7 additions & 9 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,13 @@ func (c *Client) GetCollections(
}

func (c *Client) GetSchemaByVersionID(ctx context.Context, versionID string) (client.SchemaDescription, error) {
methodURL := c.http.baseURL.JoinPath("schema")
methodURL.RawQuery = url.Values{"version_id": []string{versionID}}.Encode()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, methodURL.String(), nil)
schemas, err := c.GetSchemas(ctx, client.SchemaFetchOptions{ID: immutable.Some(versionID)})
if err != nil {
return client.SchemaDescription{}, err
}
var schema client.SchemaDescription
if err := c.http.requestJson(req, &schema); err != nil {
return client.SchemaDescription{}, err
}
return schema, nil

// schemas will always have length == 1 here
return schemas[0], nil
}

func (c *Client) GetSchemas(
Expand All @@ -291,6 +286,9 @@ func (c *Client) GetSchemas(
) ([]client.SchemaDescription, error) {
methodURL := c.http.baseURL.JoinPath("schema")
params := url.Values{}
if options.ID.HasValue() {
params.Add("version_id", options.ID.Value())
}
if options.Root.HasValue() {
params.Add("root", options.Root.Value())
}
Expand Down
37 changes: 15 additions & 22 deletions http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,23 @@ func (s *storeHandler) GetCollection(rw http.ResponseWriter, req *http.Request)
func (s *storeHandler) GetSchema(rw http.ResponseWriter, req *http.Request) {
store := req.Context().Value(storeContextKey).(client.Store)

switch {
case req.URL.Query().Has("version_id"):
schema, err := store.GetSchemaByVersionID(req.Context(), req.URL.Query().Get("version_id"))
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}
responseJSON(rw, http.StatusOK, schema)
default:
options := client.SchemaFetchOptions{}
if req.URL.Query().Has("root") {
options.Root = immutable.Some(req.URL.Query().Get("root"))
}
if req.URL.Query().Has("name") {
options.Name = immutable.Some(req.URL.Query().Get("name"))
}
options := client.SchemaFetchOptions{}
if req.URL.Query().Has("version_id") {
options.ID = immutable.Some(req.URL.Query().Get("version_id"))
}
if req.URL.Query().Has("root") {
options.Root = immutable.Some(req.URL.Query().Get("root"))
}
if req.URL.Query().Has("name") {
options.Name = immutable.Some(req.URL.Query().Get("name"))
}

schema, err := store.GetSchemas(req.Context(), options)
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}
responseJSON(rw, http.StatusOK, schema)
schema, err := store.GetSchemas(req.Context(), options)
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}
responseJSON(rw, http.StatusOK, schema)
}

func (s *storeHandler) GetAllIndexes(rw http.ResponseWriter, req *http.Request) {
Expand Down
16 changes: 7 additions & 9 deletions tests/clients/cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,25 +310,23 @@ func (w *Wrapper) GetCollections(
}

func (w *Wrapper) GetSchemaByVersionID(ctx context.Context, versionID string) (client.SchemaDescription, error) {
args := []string{"client", "schema", "describe"}
args = append(args, "--version", versionID)

data, err := w.cmd.execute(ctx, args)
schemas, err := w.GetSchemas(ctx, client.SchemaFetchOptions{ID: immutable.Some(versionID)})
if err != nil {
return client.SchemaDescription{}, err
}
var schema client.SchemaDescription
if err := json.Unmarshal(data, &schema); err != nil {
return client.SchemaDescription{}, err
}
return schema, err

// schemas will always have length == 1 here
return schemas[0], nil
}

func (w *Wrapper) GetSchemas(
ctx context.Context,
options client.SchemaFetchOptions,
) ([]client.SchemaDescription, error) {
args := []string{"client", "schema", "describe"}
if options.ID.HasValue() {
args = append(args, "--version", options.ID.Value())
}
if options.Root.HasValue() {
args = append(args, "--root", options.Root.Value())
}
Expand Down

0 comments on commit d1e0691

Please sign in to comment.