Skip to content

Commit

Permalink
fix: docs examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Feb 22, 2023
1 parent 3b006fa commit 832655d
Show file tree
Hide file tree
Showing 77 changed files with 44,388 additions and 865 deletions.
18 changes: 17 additions & 1 deletion cmd/expand/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package expand
import (
"fmt"

"google.golang.org/protobuf/encoding/protojson"

"github.com/ory/keto/ketoapi"

rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"
Expand Down Expand Up @@ -53,7 +55,7 @@ func NewExpandCmd() *cobra.Command {
tree = ketoapi.TreeFromProto[*ketoapi.RelationTuple](resp.Tree)
}

cmdx.PrintJSONAble(cmd, tree)
cmdx.PrintJSONAble(cmd, &pbJSONValue{resp})
switch flagx.MustGetString(cmd, cmdx.FlagFormat) {
case string(cmdx.FormatDefault), "":
if tree == nil && !flagx.MustGetBool(cmd, cmdx.FlagQuiet) {
Expand All @@ -76,3 +78,17 @@ func NewExpandCmd() *cobra.Command {
func RegisterCommandsRecursive(parent *cobra.Command) {
parent.AddCommand(NewExpandCmd())
}

type pbJSONValue struct{ *rts.ExpandResponse }

func (v *pbJSONValue) MarshalJSON() ([]byte, error) {
marshaler := &protojson.MarshalOptions{EmitUnpopulated: true}
return marshaler.Marshal(v.Tree)
}
func (v *pbJSONValue) String() string {
var tree *ketoapi.Tree[*ketoapi.RelationTuple]
if v.Tree != nil {
tree = ketoapi.TreeFromProto[*ketoapi.RelationTuple](v.Tree)
}
return tree.String()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,145 @@
{
"children": [
{
"children": [],
"subject": {
"id": "laura"
},
"tuple": {
"namespace": "",
"object": "",
"relation": "",
"subject_id": "maureen"
"subject": {
"id": "laura"
},
"subject_id": "laura"
},
"type": "leaf"
},
{
"children": [],
"subject": {
"set": {
"namespace": "directories",
"object": "/photos",
"relation": "owner"
}
},
"tuple": {
"namespace": "",
"object": "",
"relation": "",
"subject": {
"set": {
"namespace": "directories",
"object": "/photos",
"relation": "owner"
}
},
"subject_set": {
"namespace": "directories",
"object": "/photos",
"relation": "owner"
}
},
"type": "leaf"
}
],
"subject": {
"set": {
"namespace": "directories",
"object": "/photos",
"relation": "access"
}
},
"tuple": {
"namespace": "",
"object": "",
"relation": "",
"subject": {
"set": {
"namespace": "directories",
"object": "/photos",
"relation": "access"
}
},
"subject_set": {
"namespace": "files",
"object": "/photos/beach.jpg",
"relation": "owner"
"namespace": "directories",
"object": "/photos",
"relation": "access"
}
},
"type": "union"
},
{
"children": [
{
"tuple": {
"namespace": "",
"object": "",
"relation": "",
"subject_id": "laura"
"children": [],
"subject": {
"id": "maureen"
},
"type": "leaf"
},
{
"tuple": {
"namespace": "",
"object": "",
"relation": "",
"subject_set": {
"namespace": "directories",
"object": "/photos",
"relation": "owner"
}
"subject": {
"id": "maureen"
},
"subject_id": "maureen"
},
"type": "leaf"
}
],
"subject": {
"set": {
"namespace": "files",
"object": "/photos/beach.jpg",
"relation": "owner"
}
},
"tuple": {
"namespace": "",
"object": "",
"relation": "",
"subject": {
"set": {
"namespace": "files",
"object": "/photos/beach.jpg",
"relation": "owner"
}
},
"subject_set": {
"namespace": "directories",
"object": "/photos",
"relation": "access"
"namespace": "files",
"object": "/photos/beach.jpg",
"relation": "owner"
}
},
"type": "union"
}
],
"subject": {
"set": {
"namespace": "files",
"object": "/photos/beach.jpg",
"relation": "access"
}
},
"tuple": {
"namespace": "",
"object": "",
"relation": "",
"subject": {
"set": {
"namespace": "files",
"object": "/photos/beach.jpg",
"relation": "access"
}
},
"subject_set": {
"namespace": "files",
"object": "/photos/beach.jpg",
"relation": "access"
}
},
"type": "union"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ expandRequest.setMaxDepth(3)
// helper to get a nice result
const subjectJSON = (subject) => {
if (subject.hasId()) {
return { subject_id: subject.getId() }
return {
subject_id: subject.getId(),
subject: {
id: subject.getId(),
},
}
}
const set = subject.getSet()
return {
Expand All @@ -33,6 +38,33 @@ const subjectJSON = (subject) => {
object: set.getObject(),
relation: set.getRelation(),
},
subject: {
set: {
namespace: set.getNamespace(),
object: set.getObject(),
relation: set.getRelation(),
},
},
}
}

const newSubjectJSON = (subject) => {
if (subject.hasId()) {
return {
subject: {
id: subject.getId(),
},
}
}
const set = subject.getSet()
return {
subject: {
set: {
namespace: set.getNamespace(),
object: set.getObject(),
relation: set.getRelation(),
},
},
}
}

Expand All @@ -41,6 +73,7 @@ const prettyTree = (tree) => {
const [nodeType, tuple, children] = [
tree.getNodeType(),
{
...newSubjectJSON(tree.getSubject()),
tuple: {
namespace: "",
object: "",
Expand All @@ -52,7 +85,7 @@ const prettyTree = (tree) => {
]
switch (nodeType) {
case expand.NodeType.NODE_TYPE_LEAF:
return { type: "leaf", ...tuple }
return { type: "leaf", children: [], ...tuple }
case expand.NodeType.NODE_TYPE_UNION:
return { type: "union", children: children.map(prettyTree), ...tuple }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ package main

import (
"context"
"encoding/json"
"os"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/encoding/protojson"

"github.com/ory/keto/ketoapi"
rts "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2"
)

Expand All @@ -31,11 +30,12 @@ func main() {
panic(err)
}

tree := ketoapi.TreeFromProto[*ketoapi.RelationTuple](res.Tree)

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(tree); err != nil {
panic(err.Error())
marshaler := protojson.MarshalOptions{EmitUnpopulated: true}
bs, err := marshaler.Marshal(res.Tree)
if err != nil {
panic(err)
}
if _, err = os.Stdout.Write(bs); err != nil {
panic(err)
}
}
4 changes: 3 additions & 1 deletion ketoapi/enc_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ func (q *RelationQuery) ToProto() *rts.RelationQuery {
func (t *Tree[NodeT]) ToProto() *rts.SubjectTree {
res := &rts.SubjectTree{
NodeType: t.Type.ToProto(),
Children: make([]*rts.SubjectTree, len(t.Children)),
}
if len(t.Children) > 0 {
res.Children = make([]*rts.SubjectTree, len(t.Children))
}
res.Tuple = t.Tuple.ToProto()
// nolint - fill deprecated field
Expand Down
Loading

0 comments on commit 832655d

Please sign in to comment.