This repository has been archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix encoding tests and implement NodeWriter for memory.Node
- Loading branch information
Showing
5 changed files
with
155 additions
and
130 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,100 @@ | ||
package memory | ||
|
||
import ( | ||
"fmt" | ||
|
||
ipld "github.com/ipfs/go-ipld" | ||
) | ||
|
||
type Writer struct { | ||
Node Node | ||
stack []interface{} | ||
curKey string | ||
valPart []byte | ||
} | ||
|
||
func NewWriter() *Writer { | ||
return &Writer{Node{}, nil, "", nil} | ||
} | ||
|
||
func NewNodeFrom(r ipld.NodeReader) (Node, error) { | ||
w := NewWriter() | ||
err := ipld.Copy(r, w) | ||
return w.Node, err | ||
} | ||
|
||
func (w *Writer) WriteValue(val interface{}) error { | ||
if len(w.stack) == 0 { | ||
return fmt.Errorf("Cannot write value") | ||
} | ||
|
||
switch val.(type) { | ||
case string: | ||
val = string(append(w.valPart, []byte(val.(string))...)) | ||
case []byte: | ||
val = append(w.valPart, val.([]byte)...) | ||
} | ||
|
||
last := len(w.stack) - 1 | ||
switch w.stack[last].(type) { | ||
case *Node: | ||
(*w.stack[last].(*Node))[w.curKey] = val | ||
case []interface{}: | ||
w.stack[last] = append(w.stack[last].([]interface{}), val) | ||
default: | ||
panic("Currupted stack") | ||
} | ||
|
||
w.curKey = "" | ||
w.valPart = nil | ||
return nil | ||
} | ||
|
||
func (w *Writer) WriteValuePart(val []byte) error { | ||
w.valPart = append(w.valPart, val...) | ||
return nil | ||
} | ||
|
||
func (w *Writer) WriteBeginNode(n_elems int) error { | ||
if len(w.stack) == 0 { | ||
w.stack = append(w.stack, &w.Node) | ||
return nil | ||
} else { | ||
n := &Node{} | ||
err := w.WriteValue(n) | ||
w.stack = append(w.stack, n) | ||
return err | ||
} | ||
} | ||
|
||
func (w *Writer) WriteNodeKey(key string) error { | ||
w.curKey = key | ||
return nil | ||
} | ||
|
||
func (w *Writer) WriteEndNode() error { | ||
if len(w.stack) == 0 { | ||
return fmt.Errorf("Cannot end node") | ||
} | ||
|
||
w.stack = w.stack[:len(w.stack)-1] | ||
return nil | ||
} | ||
|
||
func (w *Writer) WriteBeginArray(n_elems int) error { | ||
if len(w.stack) == 0 { | ||
return fmt.Errorf("Cannot start array") | ||
} | ||
|
||
w.stack = append(w.stack, []interface{}{}) | ||
return nil | ||
} | ||
|
||
func (w *Writer) WriteEndArray() error { | ||
if len(w.stack) <= 1 { | ||
return fmt.Errorf("Cannot end array") | ||
} | ||
|
||
w.stack = w.stack[:len(w.stack)-1] | ||
return 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