-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Relevant issue(s) Resolves #1624 ## Description This PR adds an API for listing schemas. The schema API routes have also been simplified to be more RESTful. ### HTTP `curl http://localhost:9181/api/v0/schema` #### Output ``` { "data": { "collections": [ { "name": "User", "id": "bafkreibpnvkvjqvg4skzlijka5xe63zeu74ivcjwd76q7yi65jdhwqhske", "fields": [ { "id": "1", "name": "age", "kind": "Int" }, { "id": "2", "name": "name", "kind": "String" }, { "id": "3", "name": "points", "kind": "Float" }, { "id": "4", "name": "verified", "kind": "Boolean" } ] } ], "result": "success" } } ``` ### CLI `defradb client schema list` #### Output ``` type User { name: String age: Int verified: Boolean points: Float } ``` ## Tasks - [x] I made sure the code is well commented, particularly hard-to-understand areas. - [x] I made sure the repository-held documentation is changed accordingly. - [x] I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in [tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)). - [x] I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ... ## How has this been tested? Automated test + manual testing Specify the platform(s) on which this was tested: - MacOS
- Loading branch information
Showing
8 changed files
with
305 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
httpapi "github.com/sourcenetwork/defradb/api/http" | ||
"github.com/sourcenetwork/defradb/config" | ||
"github.com/sourcenetwork/defradb/errors" | ||
) | ||
|
||
type schemaListResponse struct { | ||
Data struct { | ||
Collections []struct { | ||
Name string `json:"name"` | ||
ID string `json:"id"` | ||
Fields []struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Kind string `json:"kind"` | ||
} `json:"fields"` | ||
} `json:"collections"` | ||
} `json:"data"` | ||
Errors []struct { | ||
Message string `json:"message"` | ||
} `json:"errors"` | ||
} | ||
|
||
func MakeSchemaListCommand(cfg *config.Config) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List schema types with their respective fields", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
endpoint, err := httpapi.JoinPaths(cfg.API.AddressToURL(), httpapi.SchemaPath) | ||
if err != nil { | ||
return NewErrFailedToJoinEndpoint(err) | ||
} | ||
|
||
res, err := http.Get(endpoint.String()) | ||
if err != nil { | ||
return NewErrFailedToSendRequest(err) | ||
} | ||
defer res.Body.Close() //nolint:errcheck | ||
|
||
data, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return NewErrFailedToReadResponseBody(err) | ||
} | ||
|
||
var r schemaListResponse | ||
if err := json.Unmarshal(data, &r); err != nil { | ||
return NewErrFailedToUnmarshalResponse(err) | ||
} | ||
if len(r.Errors) > 0 { | ||
return errors.New("failed to list schemas", errors.NewKV("errors", r.Errors)) | ||
} | ||
|
||
for _, c := range r.Data.Collections { | ||
cmd.Printf("type %s {\n", c.Name) | ||
for _, f := range c.Fields { | ||
cmd.Printf("\t%s: %s\n", f.Name, f.Kind) | ||
} | ||
cmd.Printf("}\n") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.