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.
feat(gateway): preview dag-cbor/-json with links
- Loading branch information
Showing
13 changed files
with
323 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package assets | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
|
||
"github.com/ipld/go-ipld-prime/datamodel" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
) | ||
|
||
type ParsedNode struct { | ||
Keys []*ParsedNode | ||
Values []*ParsedNode | ||
Value string | ||
CID string | ||
} | ||
|
||
func ParseNode(node datamodel.Node) (*ParsedNode, error) { | ||
dag := &ParsedNode{} | ||
|
||
switch node.Kind() { | ||
case datamodel.Kind_Map: | ||
it := node.MapIterator() | ||
|
||
for !it.Done() { | ||
k, v, err := it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
kd, err := ParseNode(k) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vd, err := ParseNode(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dag.Keys = append(dag.Keys, kd) | ||
dag.Values = append(dag.Values, vd) | ||
} | ||
case datamodel.Kind_List: | ||
it := node.ListIterator() | ||
for !it.Done() { | ||
k, v, err := it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vd, err := ParseNode(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dag.Keys = append(dag.Keys, &ParsedNode{Value: fmt.Sprint(k)}) | ||
dag.Values = append(dag.Values, vd) | ||
} | ||
case datamodel.Kind_Bool: | ||
v, err := node.AsBool() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = fmt.Sprintf("%t", v) | ||
case datamodel.Kind_Int: | ||
v, err := node.AsInt() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = fmt.Sprintf("%d", v) | ||
case datamodel.Kind_Float: | ||
v, err := node.AsFloat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = fmt.Sprintf("%f", v) | ||
case datamodel.Kind_String: | ||
v, err := node.AsString() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = template.HTMLEscapeString(v) | ||
case datamodel.Kind_Bytes: | ||
v, err := node.AsBytes() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = fmt.Sprint(v) | ||
case datamodel.Kind_Link: | ||
lnk, err := node.AsLink() | ||
if err != nil { | ||
return nil, err | ||
} | ||
dag.Value = lnk.String() | ||
|
||
cl, isCid := lnk.(cidlink.Link) | ||
if isCid { | ||
dag.CID = cl.Cid.String() | ||
} | ||
case datamodel.Kind_Invalid: | ||
dag.Value = "INVALID" | ||
case datamodel.Kind_Null: | ||
dag.Value = "NULL" | ||
default: | ||
dag.Value = "UNKNOWN" | ||
} | ||
|
||
return dag, nil | ||
} |
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
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.
Oops, something went wrong.