-
Notifications
You must be signed in to change notification settings - Fork 53
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: Allow users to add Views #2114
Changes from all commits
c64af4c
ae0b5fe
3eaa012
c0d1322
1484964
f946430
4afcb22
db5e804
45acb10
0bcc4c1
1f19bab
2e4c1ee
8af55c7
3244b57
b9e90e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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 ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func MakeViewCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "view", | ||
Short: "Manage views within a running DefraDB instance", | ||
Long: "Manage (add) views withing a running DefraDB instance", | ||
} | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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 "github.com/spf13/cobra" | ||
|
||
func MakeViewAddCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "add [query] [sdl]", | ||
Short: "Add new view", | ||
Long: `Add new database view. | ||
|
||
Example: add from an argument string: | ||
defradb client view add 'Foo { name, ...}' 'type Foo { ... }' | ||
|
||
Learn more about the DefraDB GraphQL Schema Language on https://docs.source.network.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
store := mustGetStoreContext(cmd) | ||
|
||
if len(args) != 2 { | ||
return ErrViewAddMissingArgs | ||
} | ||
|
||
query := args[0] | ||
sdl := args[1] | ||
|
||
defs, err := store.AddView(cmd.Context(), query, sdl) | ||
if err != nil { | ||
return err | ||
} | ||
return writeJSON(cmd, defs) | ||
}, | ||
} | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
package request | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/sourcenetwork/immutable" | ||
) | ||
|
||
|
@@ -107,3 +109,88 @@ func (s *Select) validateGroupBy() []error { | |
|
||
return result | ||
} | ||
|
||
// selectJson is a private object used for handling json deserialization | ||
// of `Select` objects. | ||
type selectJson struct { | ||
Field | ||
DocKeys immutable.Option[[]string] | ||
CID immutable.Option[string] | ||
Root SelectionType | ||
Limit immutable.Option[uint64] | ||
Offset immutable.Option[uint64] | ||
OrderBy immutable.Option[OrderBy] | ||
GroupBy immutable.Option[GroupBy] | ||
Filter immutable.Option[Filter] | ||
ShowDeleted bool | ||
|
||
// Properties above this line match the `Select` object and | ||
// are deserialized using the normal/default logic. | ||
// Properties below this line require custom logic in `UnmarshalJSON` | ||
// in order to be deserialized correctly. | ||
Comment on lines
+127
to
+130
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Although I like this comment, I find that overtime this might be hard to enforce / maintain. Perhaps in addition to this add a different prefix to variable names to the ones that require custom logic? i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current solution for sure has it's flaws, but I'm not sure I like that suggestion. I'll add a task to re-visit and see how I feel about it again in a little bit :)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't like the suggestion, and will leave as-is unless anyone continues this thread :) |
||
|
||
Fields []map[string]json.RawMessage | ||
} | ||
|
||
func (s *Select) UnmarshalJSON(bytes []byte) error { | ||
var selectMap selectJson | ||
err := json.Unmarshal(bytes, &selectMap) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.Field = selectMap.Field | ||
s.DocKeys = selectMap.DocKeys | ||
s.CID = selectMap.CID | ||
s.Root = selectMap.Root | ||
s.Limit = selectMap.Limit | ||
s.Offset = selectMap.Offset | ||
s.OrderBy = selectMap.OrderBy | ||
s.GroupBy = selectMap.GroupBy | ||
s.Filter = selectMap.Filter | ||
s.ShowDeleted = selectMap.ShowDeleted | ||
s.Fields = make([]Selection, len(selectMap.Fields)) | ||
|
||
for i, field := range selectMap.Fields { | ||
fieldJson, err := json.Marshal(field) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: why do you need to marshal the field first? Can't you unmarshal directly from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
var fieldValue Selection | ||
// We detect which concrete type each `Selection` object is by detecting | ||
// non-nillable fields, if the key is present it must be of that type. | ||
// They must be non-nillable as nil values may have their keys omitted from | ||
// the json. This also relies on the fields being unique. We may wish to change | ||
// this later to custom-serialize with a `_type` property. | ||
if _, ok := field["Root"]; ok { | ||
// This must be a Select, as only the `Select` type has a `Root` field | ||
var fieldSelect Select | ||
err := json.Unmarshal(fieldJson, &fieldSelect) | ||
if err != nil { | ||
return err | ||
} | ||
fieldValue = &fieldSelect | ||
} else if _, ok := field["Targets"]; ok { | ||
// This must be an Aggregate, as only the `Aggregate` type has a `Targets` field | ||
var fieldAggregate Aggregate | ||
err := json.Unmarshal(fieldJson, &fieldAggregate) | ||
if err != nil { | ||
return err | ||
} | ||
fieldValue = &fieldAggregate | ||
} else { | ||
// This must be a Field | ||
var fieldField Field | ||
err := json.Unmarshal(fieldJson, &fieldField) | ||
if err != nil { | ||
return err | ||
} | ||
fieldValue = &fieldField | ||
} | ||
|
||
s.Fields[i] = fieldValue | ||
} | ||
|
||
return nil | ||
} |
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.
nitpick: 2 spaces
Also on the next line I think it should be "schema-only"
And on the next line again 2 spaces
Also in one the last line 2 spaces
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.
😆 https://www.independent.co.uk/tech/one-space-or-two-spaces-after-a-full-stop-scientists-have-finally-found-the-answer-a8337646.html
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.