-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Schema list API #1625
Merged
Merged
feat: Schema list API #1625
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f95747b
add schema list api route. add String method to FieldKind type.
nasdf 69b9271
add schema list cli command. ignore generated fields when listing sch…
nasdf cc23fdc
Merge branch 'develop' into nasdf/feat/schema-list
nasdf fc9e9e1
simplify schema api routes
nasdf f48888b
add list schema http api test.
nasdf 57c29ef
Merge branch 'develop' into nasdf/feat/schema-list
nasdf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: I would be really nice to have a unit test for this handler to see what the expected output is and that we catch future changes to the schema structure. Ideally the error paths would also be tested. You can base yourself off of the other unit test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I added tests for the error path as well as the happy path.