fluentBuilder.go 4.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package fluent

import (
	ipld "github.com/ipld/go-ipld-prime"
)

func Build(ns ipld.NodeStyle, fn func(NodeAssembler)) (ipld.Node, error) {
	nb := ns.NewBuilder()
	fna := WrapAssembler(nb)
	err := Recover(func() {
		fn(fna)
	})
	return nb.Build(), err
}

func MustBuild(ns ipld.NodeStyle, fn func(NodeAssembler)) ipld.Node {
	nb := ns.NewBuilder()
	fn(WrapAssembler(nb))
	return nb.Build()
}
21
func MustBuildMap(ns ipld.NodeStyle, sizeHint int, fn func(MapAssembler)) ipld.Node {
22 23
	return MustBuild(ns, func(fna NodeAssembler) { fna.CreateMap(sizeHint, fn) })
}
24
func MustBuildList(ns ipld.NodeStyle, sizeHint int, fn func(ListAssembler)) ipld.Node {
25 26 27 28 29 30 31 32 33 34 35 36 37
	return MustBuild(ns, func(fna NodeAssembler) { fna.CreateList(sizeHint, fn) })
}

func WrapAssembler(na ipld.NodeAssembler) NodeAssembler {
	return &nodeAssembler{na}
}

// NodeAssembler is the same as the interface in the core package, except:
// instead of returning errors, any error will cause panic
// (and you can collect these with `fluent.Recover`);
// and all recursive operations take a function as a parameter,
// within which you will receive another {Map,List,}NodeAssembler.
type NodeAssembler interface {
38 39
	CreateMap(sizeHint int, fn func(MapAssembler))
	CreateList(sizeHint int, fn func(ListAssembler))
40 41 42 43 44 45 46 47 48 49 50 51
	AssignNull()
	AssignBool(bool)
	AssignInt(int)
	AssignFloat(float64)
	AssignString(string)
	AssignBytes([]byte)
	AssignLink(ipld.Link)
	AssignNode(ipld.Node)

	Style() ipld.NodeStyle
}

52
// MapAssembler is the same as the interface in the core package, except:
53 54 55 56
// instead of returning errors, any error will cause panic
// (and you can collect these with `fluent.Recover`);
// and all recursive operations take a function as a parameter,
// within which you will receive another {Map,List,}NodeAssembler.
57
type MapAssembler interface {
58 59 60 61 62 63 64 65 66
	AssembleKey() NodeAssembler
	AssembleValue() NodeAssembler

	AssembleDirectly(k string) NodeAssembler

	KeyStyle() ipld.NodeStyle
	ValueStyle(k string) ipld.NodeStyle
}

67
// ListAssembler is the same as the interface in the core package, except:
68 69 70 71
// instead of returning errors, any error will cause panic
// (and you can collect these with `fluent.Recover`);
// and all recursive operations take a function as a parameter,
// within which you will receive another {Map,List,}NodeAssembler.
72
type ListAssembler interface {
73 74
	AssembleValue() NodeAssembler

75
	ValueStyle(idx int) ipld.NodeStyle
76 77 78 79 80 81
}

type nodeAssembler struct {
	na ipld.NodeAssembler
}

82
func (fna *nodeAssembler) CreateMap(sizeHint int, fn func(MapAssembler)) {
83 84 85 86 87 88 89 90 91
	if ma, err := fna.na.BeginMap(sizeHint); err != nil {
		panic(Error{err})
	} else {
		fn(&mapNodeAssembler{ma})
		if err := ma.Finish(); err != nil {
			panic(Error{err})
		}
	}
}
92
func (fna *nodeAssembler) CreateList(sizeHint int, fn func(ListAssembler)) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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
	if la, err := fna.na.BeginList(sizeHint); err != nil {
		panic(Error{err})
	} else {
		fn(&listNodeAssembler{la})
		if err := la.Finish(); err != nil {
			panic(Error{err})
		}
	}
}
func (fna *nodeAssembler) AssignNull() {
	if err := fna.na.AssignNull(); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) AssignBool(v bool) {
	if err := fna.na.AssignBool(v); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) AssignInt(v int) {
	if err := fna.na.AssignInt(v); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) AssignFloat(v float64) {
	if err := fna.na.AssignFloat(v); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) AssignString(v string) {
	if err := fna.na.AssignString(v); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) AssignBytes(v []byte) {
	if err := fna.na.AssignBytes(v); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) AssignLink(v ipld.Link) {
	if err := fna.na.AssignLink(v); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) AssignNode(v ipld.Node) {
	if err := fna.na.AssignNode(v); err != nil {
		panic(Error{err})
	}
}
func (fna *nodeAssembler) Style() ipld.NodeStyle {
	return fna.na.Style()
}

type mapNodeAssembler struct {
147
	ma ipld.MapAssembler
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}

func (fma *mapNodeAssembler) AssembleKey() NodeAssembler {
	return &nodeAssembler{fma.ma.AssembleKey()}
}
func (fma *mapNodeAssembler) AssembleValue() NodeAssembler {
	return &nodeAssembler{fma.ma.AssembleValue()}
}
func (fma *mapNodeAssembler) AssembleDirectly(k string) NodeAssembler {
	va, err := fma.ma.AssembleDirectly(k)
	if err != nil {
		panic(Error{err})
	}
	return &nodeAssembler{va}
}
func (fma *mapNodeAssembler) KeyStyle() ipld.NodeStyle {
	return fma.ma.KeyStyle()
}
func (fma *mapNodeAssembler) ValueStyle(k string) ipld.NodeStyle {
	return fma.ma.ValueStyle(k)
}

type listNodeAssembler struct {
171
	la ipld.ListAssembler
172 173 174 175 176
}

func (fla *listNodeAssembler) AssembleValue() NodeAssembler {
	return &nodeAssembler{fla.la.AssembleValue()}
}
177 178
func (fla *listNodeAssembler) ValueStyle(idx int) ipld.NodeStyle {
	return fla.la.ValueStyle(idx)
179
}