unmarshal.go 4.06 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3 4
package dagcbor

import (
	"fmt"
Eric Myhre's avatar
Eric Myhre committed
5
	"math"
Eric Myhre's avatar
Eric Myhre committed
6

Eric Myhre's avatar
Eric Myhre committed
7
	cid "github.com/ipfs/go-cid"
Eric Myhre's avatar
Eric Myhre committed
8 9 10
	"github.com/polydawn/refmt/shared"
	"github.com/polydawn/refmt/tok"

Eric Myhre's avatar
Eric Myhre committed
11
	ipld "github.com/ipld/go-ipld-prime"
Eric Myhre's avatar
Eric Myhre committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)

// This should be identical to the general feature in the parent package,
// except for the `case tok.TBytes` block,
// which has dag-cbor's special sauce for detecting schemafree links.

func Unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource) (ipld.Node, error) {
	var tk tok.Token
	done, err := tokSrc.Step(&tk)
	if done || err != nil {
		return nil, err
	}
	return unmarshal(nb, tokSrc, &tk)
}

// starts with the first token already primed.  Necessary to get recursion
//  to flow right without a peek+unpeek system.
func unmarshal(nb ipld.NodeBuilder, tokSrc shared.TokenSource, tk *tok.Token) (ipld.Node, error) {
	// FUTURE: check for typed.NodeBuilder that's going to parse a Link (they can slurp any token kind they want).
	switch tk.Type {
	case tok.TMapOpen:
		mb, err := nb.CreateMap()
		if err != nil {
			return nil, err
		}
Eric Myhre's avatar
Eric Myhre committed
38 39 40 41 42 43 44
		expectLen := tk.Length
		if tk.Length == -1 {
			expectLen = math.MaxInt32
		}
		observedLen := 0
		var k string
		var v ipld.Node
Eric Myhre's avatar
Eric Myhre committed
45
		for {
46
			_, err := tokSrc.Step(tk)
Eric Myhre's avatar
Eric Myhre committed
47 48 49 50 51
			if err != nil {
				return nil, err
			}
			switch tk.Type {
			case tok.TMapClose:
Eric Myhre's avatar
Eric Myhre committed
52 53 54
				if expectLen != math.MaxInt32 && observedLen != expectLen {
					return nil, fmt.Errorf("unexpected mapClose before declared length")
				}
Eric Myhre's avatar
Eric Myhre committed
55 56 57 58 59 60
				return mb.Build()
			case tok.TString:
				// continue
			default:
				return nil, fmt.Errorf("unexpected %s token while expecting map key", tk.Type)
			}
Eric Myhre's avatar
Eric Myhre committed
61 62 63 64 65
			observedLen++
			if observedLen > expectLen {
				return nil, fmt.Errorf("unexpected continuation of map elements beyond declared length")
			}
			k = tk.Str
Eric Myhre's avatar
Eric Myhre committed
66
			// FUTURE: check for typed.NodeBuilder; need to specialize before recursing if so.
67
			// FUTURE: similar specialization needed for bind.Node as well -- perhaps this actually needs to live on NodeBuilder.
Eric Myhre's avatar
Eric Myhre committed
68
			v, err = Unmarshal(nb, tokSrc)
Eric Myhre's avatar
Eric Myhre committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
			if err != nil {
				return nil, err
			}
			kn, err := nb.CreateString(k)
			if err != nil {
				panic(err) // TODO: I'm no longer sure Insert should take a Node instead of string, but not recursing into reviewing that choice now.
			}
			if err := mb.Insert(kn, v); err != nil {
				return nil, err
			}
		}
	case tok.TMapClose:
		return nil, fmt.Errorf("unexpected mapClose token")
	case tok.TArrOpen:
		lb, err := nb.CreateList()
		if err != nil {
			return nil, err
		}
Eric Myhre's avatar
Eric Myhre committed
87 88 89 90 91
		expectLen := tk.Length
		if tk.Length == -1 {
			expectLen = math.MaxInt32
		}
		observedLen := 0
Eric Myhre's avatar
Eric Myhre committed
92
		for {
93
			_, err := tokSrc.Step(tk)
Eric Myhre's avatar
Eric Myhre committed
94 95 96 97 98
			if err != nil {
				return nil, err
			}
			switch tk.Type {
			case tok.TArrClose:
Eric Myhre's avatar
Eric Myhre committed
99 100 101
				if expectLen != math.MaxInt32 && observedLen != expectLen {
					return nil, fmt.Errorf("unexpected arrClose before declared length")
				}
Eric Myhre's avatar
Eric Myhre committed
102 103
				return lb.Build()
			default:
Eric Myhre's avatar
Eric Myhre committed
104 105 106 107
				observedLen++
				if observedLen > expectLen {
					return nil, fmt.Errorf("unexpected continuation of array elements beyond declared length")
				}
Eric Myhre's avatar
Eric Myhre committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
				// FUTURE: check for typed.NodeBuilder; need to specialize before recursing if so.
				//  N.B. when considering optionals for tuple-represented structs, keep in mind how murky that will get here.
				v, err := unmarshal(nb, tokSrc, tk)
				if err != nil {
					return nil, err
				}
				lb.Append(v)
			}
		}
	case tok.TArrClose:
		return nil, fmt.Errorf("unexpected arrClose token")
	case tok.TNull:
		return nb.CreateNull()
	case tok.TString:
		return nb.CreateString(tk.Str)
	case tok.TBytes:
		if !tk.Tagged {
			return nb.CreateBytes(tk.Bytes)
		}
		switch tk.Tag {
		case linkTag:
			elCid, err := cid.Cast(tk.Bytes)
			if err != nil {
				return nil, err
			}
			return nb.CreateLink(cidlink.Link{elCid})
		default:
			return nil, fmt.Errorf("unhandled cbor tag %d", tk.Tag)
		}
	case tok.TBool:
		return nb.CreateBool(tk.Bool)
	case tok.TInt:
		return nb.CreateInt(int(tk.Int)) // FIXME overflow check
	case tok.TUint:
		return nb.CreateInt(int(tk.Uint)) // FIXME overflow check
	case tok.TFloat64:
		return nb.CreateFloat(tk.Float64)
	default:
		panic("unreachable")
	}
}