generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: visualize dag-cbor and dag-json
- Loading branch information
Showing
9 changed files
with
259 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package assets | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
|
||
"github.com/ipld/go-ipld-prime/datamodel" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
) | ||
|
||
type Node struct { | ||
node datamodel.Node | ||
path string | ||
} | ||
|
||
func NewNode(node datamodel.Node, path string) *Node { | ||
return &Node{node, path} | ||
} | ||
|
||
func (n *Node) IsValid() bool { | ||
return n.node.Kind() != datamodel.Kind_Invalid | ||
} | ||
|
||
func (n *Node) HasChildren() bool { | ||
return n.node.Kind() == datamodel.Kind_List || | ||
n.node.Kind() == datamodel.Kind_Map | ||
} | ||
|
||
func (n *Node) Cid() string { | ||
if n.node.Kind() != datamodel.Kind_Link { | ||
return "" | ||
} | ||
|
||
lnk, err := n.node.AsLink() | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
cl, isCid := lnk.(cidlink.Link) | ||
if !isCid { | ||
return "" | ||
} | ||
|
||
return cl.Cid.String() | ||
} | ||
|
||
func (n *Node) String() (string, error) { | ||
switch n.node.Kind() { | ||
case datamodel.Kind_Invalid: | ||
return "INVALID", nil | ||
case datamodel.Kind_Map: | ||
return "MAP", nil | ||
case datamodel.Kind_List: | ||
return "LIST", nil | ||
case datamodel.Kind_Null: | ||
return "NULL", nil | ||
case datamodel.Kind_Bool: | ||
v, err := n.node.AsBool() | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf("%t", v), nil | ||
case datamodel.Kind_Int: | ||
v, err := n.node.AsInt() | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf("%d", v), nil | ||
case datamodel.Kind_Float: | ||
v, err := n.node.AsFloat() | ||
if err != nil { | ||
return "", err | ||
} | ||
return fmt.Sprintf("%f", v), nil | ||
case datamodel.Kind_String: | ||
v, err := n.node.AsString() | ||
if err != nil { | ||
return "", err | ||
} | ||
return template.HTMLEscapeString(v), nil | ||
case datamodel.Kind_Bytes: | ||
return "BYTES", nil | ||
case datamodel.Kind_Link: | ||
lnk, err := n.node.AsLink() | ||
if err != nil { | ||
return "", err | ||
} | ||
return lnk.String(), nil | ||
default: | ||
return "UNKNOWN", nil | ||
} | ||
} | ||
|
||
func (n *Node) Children() (map[string]*Node, error) { | ||
m := map[string]*Node{} | ||
|
||
if n.node.Kind() == datamodel.Kind_List { | ||
it := n.node.ListIterator() | ||
for !it.Done() { | ||
k, v, err := it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key := fmt.Sprint(k) | ||
|
||
m[key] = NewNode(v, n.path+ipldToPathSeg(key)+"/") | ||
} | ||
} | ||
|
||
if n.node.Kind() == datamodel.Kind_Map { | ||
it := n.node.MapIterator() | ||
|
||
for !it.Done() { | ||
k, v, err := it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key, err := NewNode(k, n.path).String() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m[key] = NewNode(v, n.path+ipldToPathSeg(key)+"/") | ||
} | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func ipldToPathSeg(is string) string { | ||
if is == "" { // invalid, but don't panic | ||
return "" | ||
} | ||
if is[0] != '~' { | ||
return "~" + is | ||
} | ||
if len(is) < 2 { // invalid but panicking is bad | ||
return "~~i" | ||
} | ||
|
||
return "~~i" + is[1:] | ||
} |
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 |
---|---|---|
|
@@ -149,6 +149,10 @@ table { | |
width: 100%; | ||
} | ||
|
||
tr { | ||
background-color: white; | ||
} | ||
|
||
tr:first-child td { | ||
border-top: 0; | ||
} | ||
|
Binary file added
BIN
+742 Bytes
gateway/assets/test/dag/bafyreiagdtlc3xwhbeywzpwmxvwkogcujhlsm6f4cfdgpjpyu77gkubro4.block
Binary file not shown.
Binary file added
BIN
+238 Bytes
gateway/assets/test/dag/bafyreicnokmhmrnlp2wjhyk2haep4tqxiptwfrp2rrs7rzq7uk766chqvq.block
Binary file not shown.
Binary file added
BIN
+332 Bytes
gateway/assets/test/dag/bafyreihnpl7ami7esahkfdnemm6idx4r2n6u3apmtcrxlqwuapgjsciihy.block
Binary file not shown.
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