package dagpb

// Code generated by go-ipld-prime gengo.  DO NOT EDIT.

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

type _PBNode struct {
	Links _PBLinks
	Data  _Bytes
}
type PBNode = *_PBNode

func (n _PBNode) FieldLinks() PBLinks {
	return &n.Links
}
func (n _PBNode) FieldData() Bytes {
	return &n.Data
}

type _PBNode__Maybe struct {
	m schema.Maybe
	v PBNode
}
type MaybePBNode = *_PBNode__Maybe

func (m MaybePBNode) IsNull() bool {
	return m.m == schema.Maybe_Null
}
func (m MaybePBNode) IsAbsent() bool {
	return m.m == schema.Maybe_Absent
}
func (m MaybePBNode) Exists() bool {
	return m.m == schema.Maybe_Value
}
func (m MaybePBNode) AsNode() ipld.Node {
	switch m.m {
	case schema.Maybe_Absent:
		return ipld.Absent
	case schema.Maybe_Null:
		return ipld.Null
	case schema.Maybe_Value:
		return m.v
	default:
		panic("unreachable")
	}
}
func (m MaybePBNode) Must() PBNode {
	if !m.Exists() {
		panic("unbox of a maybe rejected")
	}
	return m.v
}

var (
	fieldName__PBNode_Links = _String{"Links"}
	fieldName__PBNode_Data  = _String{"Data"}
)
var _ ipld.Node = (PBNode)(&_PBNode{})
var _ schema.TypedNode = (PBNode)(&_PBNode{})

func (PBNode) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Map
}
func (n PBNode) LookupByString(key string) (ipld.Node, error) {
	switch key {
	case "Links":
		return &n.Links, nil
	case "Data":
		return &n.Data, nil
	default:
		return nil, schema.ErrNoSuchField{Type: nil /*TODO*/, Field: ipld.PathSegmentOfString(key)}
	}
}
func (n PBNode) LookupByNode(key ipld.Node) (ipld.Node, error) {
	ks, err := key.AsString()
	if err != nil {
		return nil, err
	}
	return n.LookupByString(ks)
}
func (PBNode) LookupByIndex(idx int) (ipld.Node, error) {
	return mixins.Map{"dagpb.PBNode"}.LookupByIndex(0)
}
func (n PBNode) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) {
	return n.LookupByString(seg.String())
}
func (n PBNode) MapIterator() ipld.MapIterator {
	return &_PBNode__MapItr{n, 0}
}

type _PBNode__MapItr struct {
	n   PBNode
	idx int
}

func (itr *_PBNode__MapItr) Next() (k ipld.Node, v ipld.Node, _ error) {
	if itr.idx >= 2 {
		return nil, nil, ipld.ErrIteratorOverread{}
	}
	switch itr.idx {
	case 0:
		k = &fieldName__PBNode_Links
		v = &itr.n.Links
	case 1:
		k = &fieldName__PBNode_Data
		v = &itr.n.Data
	default:
		panic("unreachable")
	}
	itr.idx++
	return
}
func (itr *_PBNode__MapItr) Done() bool {
	return itr.idx >= 2
}

func (PBNode) ListIterator() ipld.ListIterator {
	return nil
}
func (PBNode) Length() int {
	return 2
}
func (PBNode) IsAbsent() bool {
	return false
}
func (PBNode) IsNull() bool {
	return false
}
func (PBNode) AsBool() (bool, error) {
	return mixins.Map{"dagpb.PBNode"}.AsBool()
}
func (PBNode) AsInt() (int, error) {
	return mixins.Map{"dagpb.PBNode"}.AsInt()
}
func (PBNode) AsFloat() (float64, error) {
	return mixins.Map{"dagpb.PBNode"}.AsFloat()
}
func (PBNode) AsString() (string, error) {
	return mixins.Map{"dagpb.PBNode"}.AsString()
}
func (PBNode) AsBytes() ([]byte, error) {
	return mixins.Map{"dagpb.PBNode"}.AsBytes()
}
func (PBNode) AsLink() (ipld.Link, error) {
	return mixins.Map{"dagpb.PBNode"}.AsLink()
}
func (PBNode) Prototype() ipld.NodePrototype {
	return _PBNode__Prototype{}
}

type _PBNode__Prototype struct{}

func (_PBNode__Prototype) NewBuilder() ipld.NodeBuilder {
	var nb _PBNode__Builder
	nb.Reset()
	return &nb
}

type _PBNode__Builder struct {
	_PBNode__Assembler
}

func (nb *_PBNode__Builder) Build() ipld.Node {
	if *nb.m != schema.Maybe_Value {
		panic("invalid state: cannot call Build on an assembler that's not finished")
	}
	return nb.w
}
func (nb *_PBNode__Builder) Reset() {
	var w _PBNode
	var m schema.Maybe
	*nb = _PBNode__Builder{_PBNode__Assembler{w: &w, m: &m}}
}

type _PBNode__Assembler struct {
	w     *_PBNode
	m     *schema.Maybe
	state maState
	s     int
	f     int

	cm       schema.Maybe
	ca_Links _PBLinks__Assembler
	ca_Data  _Bytes__Assembler
}

func (na *_PBNode__Assembler) reset() {
	na.state = maState_initial
	na.s = 0
	na.ca_Links.reset()
	na.ca_Data.reset()
}

var (
	fieldBit__PBNode_Links       = 1 << 0
	fieldBit__PBNode_Data        = 1 << 1
	fieldBits__PBNode_sufficient = 0 + 1<<0 + 1<<1
)

func (na *_PBNode__Assembler) BeginMap(int) (ipld.MapAssembler, error) {
	switch *na.m {
	case schema.Maybe_Value, schema.Maybe_Null:
		panic("invalid state: cannot assign into assembler that's already finished")
	case midvalue:
		panic("invalid state: it makes no sense to 'begin' twice on the same assembler!")
	}
	*na.m = midvalue
	if na.w == nil {
		na.w = &_PBNode{}
	}
	return na, nil
}
func (_PBNode__Assembler) BeginList(sizeHint int) (ipld.ListAssembler, error) {
	return mixins.MapAssembler{"dagpb.PBNode"}.BeginList(0)
}
func (na *_PBNode__Assembler) AssignNull() error {
	switch *na.m {
	case allowNull:
		*na.m = schema.Maybe_Null
		return nil
	case schema.Maybe_Absent:
		return mixins.MapAssembler{"dagpb.PBNode"}.AssignNull()
	case schema.Maybe_Value, schema.Maybe_Null:
		panic("invalid state: cannot assign into assembler that's already finished")
	case midvalue:
		panic("invalid state: cannot assign null into an assembler that's already begun working on recursive structures!")
	}
	panic("unreachable")
}
func (_PBNode__Assembler) AssignBool(bool) error {
	return mixins.MapAssembler{"dagpb.PBNode"}.AssignBool(false)
}
func (_PBNode__Assembler) AssignInt(int) error {
	return mixins.MapAssembler{"dagpb.PBNode"}.AssignInt(0)
}
func (_PBNode__Assembler) AssignFloat(float64) error {
	return mixins.MapAssembler{"dagpb.PBNode"}.AssignFloat(0)
}
func (_PBNode__Assembler) AssignString(string) error {
	return mixins.MapAssembler{"dagpb.PBNode"}.AssignString("")
}
func (_PBNode__Assembler) AssignBytes([]byte) error {
	return mixins.MapAssembler{"dagpb.PBNode"}.AssignBytes(nil)
}
func (_PBNode__Assembler) AssignLink(ipld.Link) error {
	return mixins.MapAssembler{"dagpb.PBNode"}.AssignLink(nil)
}
func (na *_PBNode__Assembler) AssignNode(v ipld.Node) error {
	if v.IsNull() {
		return na.AssignNull()
	}
	if v2, ok := v.(*_PBNode); ok {
		switch *na.m {
		case schema.Maybe_Value, schema.Maybe_Null:
			panic("invalid state: cannot assign into assembler that's already finished")
		case midvalue:
			panic("invalid state: cannot assign null into an assembler that's already begun working on recursive structures!")
		}
		if na.w == nil {
			na.w = v2
			*na.m = schema.Maybe_Value
			return nil
		}
		*na.w = *v2
		*na.m = schema.Maybe_Value
		return nil
	}
	if v.ReprKind() != ipld.ReprKind_Map {
		return ipld.ErrWrongKind{TypeName: "dagpb.PBNode", MethodName: "AssignNode", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: v.ReprKind()}
	}
	itr := v.MapIterator()
	for !itr.Done() {
		k, v, err := itr.Next()
		if err != nil {
			return err
		}
		if err := na.AssembleKey().AssignNode(k); err != nil {
			return err
		}
		if err := na.AssembleValue().AssignNode(v); err != nil {
			return err
		}
	}
	return na.Finish()
}
func (_PBNode__Assembler) Prototype() ipld.NodePrototype {
	return _PBNode__Prototype{}
}
func (ma *_PBNode__Assembler) valueFinishTidy() bool {
	switch ma.f {
	case 0:
		switch ma.cm {
		case schema.Maybe_Value:
			ma.ca_Links.w = nil
			ma.cm = schema.Maybe_Absent
			ma.state = maState_initial
			return true
		default:
			return false
		}
	case 1:
		switch ma.cm {
		case schema.Maybe_Value:
			ma.ca_Data.w = nil
			ma.cm = schema.Maybe_Absent
			ma.state = maState_initial
			return true
		default:
			return false
		}
	default:
		panic("unreachable")
	}
}
func (ma *_PBNode__Assembler) AssembleEntry(k string) (ipld.NodeAssembler, error) {
	switch ma.state {
	case maState_initial:
		// carry on
	case maState_midKey:
		panic("invalid state: AssembleEntry cannot be called when in the middle of assembling another key")
	case maState_expectValue:
		panic("invalid state: AssembleEntry cannot be called when expecting start of value assembly")
	case maState_midValue:
		if !ma.valueFinishTidy() {
			panic("invalid state: AssembleEntry cannot be called when in the middle of assembling a value")
		} // if tidy success: carry on
	case maState_finished:
		panic("invalid state: AssembleEntry cannot be called on an assembler that's already finished")
	}
	switch k {
	case "Links":
		if ma.s&fieldBit__PBNode_Links != 0 {
			return nil, ipld.ErrRepeatedMapKey{&fieldName__PBNode_Links}
		}
		ma.s += fieldBit__PBNode_Links
		ma.state = maState_midValue
		ma.f = 0
		ma.ca_Links.w = &ma.w.Links
		ma.ca_Links.m = &ma.cm
		return &ma.ca_Links, nil
	case "Data":
		if ma.s&fieldBit__PBNode_Data != 0 {
			return nil, ipld.ErrRepeatedMapKey{&fieldName__PBNode_Data}
		}
		ma.s += fieldBit__PBNode_Data
		ma.state = maState_midValue
		ma.f = 1
		ma.ca_Data.w = &ma.w.Data
		ma.ca_Data.m = &ma.cm
		return &ma.ca_Data, nil
	default:
		return nil, ipld.ErrInvalidKey{TypeName: "dagpb.PBNode", Key: &_String{k}}
	}
}
func (ma *_PBNode__Assembler) AssembleKey() ipld.NodeAssembler {
	switch ma.state {
	case maState_initial:
		// carry on
	case maState_midKey:
		panic("invalid state: AssembleKey cannot be called when in the middle of assembling another key")
	case maState_expectValue:
		panic("invalid state: AssembleKey cannot be called when expecting start of value assembly")
	case maState_midValue:
		if !ma.valueFinishTidy() {
			panic("invalid state: AssembleKey cannot be called when in the middle of assembling a value")
		} // if tidy success: carry on
	case maState_finished:
		panic("invalid state: AssembleKey cannot be called on an assembler that's already finished")
	}
	ma.state = maState_midKey
	return (*_PBNode__KeyAssembler)(ma)
}
func (ma *_PBNode__Assembler) AssembleValue() ipld.NodeAssembler {
	switch ma.state {
	case maState_initial:
		panic("invalid state: AssembleValue cannot be called when no key is primed")
	case maState_midKey:
		panic("invalid state: AssembleValue cannot be called when in the middle of assembling a key")
	case maState_expectValue:
		// carry on
	case maState_midValue:
		panic("invalid state: AssembleValue cannot be called when in the middle of assembling another value")
	case maState_finished:
		panic("invalid state: AssembleValue cannot be called on an assembler that's already finished")
	}
	ma.state = maState_midValue
	switch ma.f {
	case 0:
		ma.ca_Links.w = &ma.w.Links
		ma.ca_Links.m = &ma.cm
		return &ma.ca_Links
	case 1:
		ma.ca_Data.w = &ma.w.Data
		ma.ca_Data.m = &ma.cm
		return &ma.ca_Data
	default:
		panic("unreachable")
	}
}
func (ma *_PBNode__Assembler) Finish() error {
	switch ma.state {
	case maState_initial:
		// carry on
	case maState_midKey:
		panic("invalid state: Finish cannot be called when in the middle of assembling a key")
	case maState_expectValue:
		panic("invalid state: Finish cannot be called when expecting start of value assembly")
	case maState_midValue:
		if !ma.valueFinishTidy() {
			panic("invalid state: Finish cannot be called when in the middle of assembling a value")
		} // if tidy success: carry on
	case maState_finished:
		panic("invalid state: Finish cannot be called on an assembler that's already finished")
	}
	//FIXME check if all required fields are set
	ma.state = maState_finished
	*ma.m = schema.Maybe_Value
	return nil
}
func (ma *_PBNode__Assembler) KeyPrototype() ipld.NodePrototype {
	return _String__Prototype{}
}
func (ma *_PBNode__Assembler) ValuePrototype(k string) ipld.NodePrototype {
	panic("todo structbuilder mapassembler valueprototype")
}

type _PBNode__KeyAssembler _PBNode__Assembler

func (_PBNode__KeyAssembler) BeginMap(sizeHint int) (ipld.MapAssembler, error) {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.BeginMap(0)
}
func (_PBNode__KeyAssembler) BeginList(sizeHint int) (ipld.ListAssembler, error) {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.BeginList(0)
}
func (na *_PBNode__KeyAssembler) AssignNull() error {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.AssignNull()
}
func (_PBNode__KeyAssembler) AssignBool(bool) error {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.AssignBool(false)
}
func (_PBNode__KeyAssembler) AssignInt(int) error {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.AssignInt(0)
}
func (_PBNode__KeyAssembler) AssignFloat(float64) error {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.AssignFloat(0)
}
func (ka *_PBNode__KeyAssembler) AssignString(k string) error {
	if ka.state != maState_midKey {
		panic("misuse: KeyAssembler held beyond its valid lifetime")
	}
	switch k {
	case "Links":
		if ka.s&fieldBit__PBNode_Links != 0 {
			return ipld.ErrRepeatedMapKey{&fieldName__PBNode_Links}
		}
		ka.s += fieldBit__PBNode_Links
		ka.state = maState_expectValue
		ka.f = 0
	case "Data":
		if ka.s&fieldBit__PBNode_Data != 0 {
			return ipld.ErrRepeatedMapKey{&fieldName__PBNode_Data}
		}
		ka.s += fieldBit__PBNode_Data
		ka.state = maState_expectValue
		ka.f = 1
	default:
		return ipld.ErrInvalidKey{TypeName: "dagpb.PBNode", Key: &_String{k}}
	}
	return nil
}
func (_PBNode__KeyAssembler) AssignBytes([]byte) error {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.AssignBytes(nil)
}
func (_PBNode__KeyAssembler) AssignLink(ipld.Link) error {
	return mixins.StringAssembler{"dagpb.PBNode.KeyAssembler"}.AssignLink(nil)
}
func (ka *_PBNode__KeyAssembler) AssignNode(v ipld.Node) error {
	if v2, err := v.AsString(); err != nil {
		return err
	} else {
		return ka.AssignString(v2)
	}
}
func (_PBNode__KeyAssembler) Prototype() ipld.NodePrototype {
	return _String__Prototype{}
}
func (PBNode) Type() schema.Type {
	return nil /*TODO:typelit*/
}
func (n PBNode) Representation() ipld.Node {
	return (*_PBNode__Repr)(n)
}

type _PBNode__Repr _PBNode

var (
	fieldName__PBNode_Links_serial = _String{"Links"}
	fieldName__PBNode_Data_serial  = _String{"Data"}
)
var _ ipld.Node = &_PBNode__Repr{}

func (_PBNode__Repr) ReprKind() ipld.ReprKind {
	return ipld.ReprKind_Map
}
func (n *_PBNode__Repr) LookupByString(key string) (ipld.Node, error) {
	switch key {
	case "Links":
		return n.Links.Representation(), nil
	case "Data":
		return n.Data.Representation(), nil
	default:
		return nil, schema.ErrNoSuchField{Type: nil /*TODO*/, Field: ipld.PathSegmentOfString(key)}
	}
}
func (n *_PBNode__Repr) LookupByNode(key ipld.Node) (ipld.Node, error) {
	ks, err := key.AsString()
	if err != nil {
		return nil, err
	}
	return n.LookupByString(ks)
}
func (_PBNode__Repr) LookupByIndex(idx int) (ipld.Node, error) {
	return mixins.Map{"dagpb.PBNode.Repr"}.LookupByIndex(0)
}
func (n _PBNode__Repr) LookupBySegment(seg ipld.PathSegment) (ipld.Node, error) {
	return n.LookupByString(seg.String())
}
func (n *_PBNode__Repr) MapIterator() ipld.MapIterator {
	return &_PBNode__ReprMapItr{n, 0}
}

type _PBNode__ReprMapItr struct {
	n   *_PBNode__Repr
	idx int
}

func (itr *_PBNode__ReprMapItr) Next() (k ipld.Node, v ipld.Node, _ error) {
	if itr.idx >= 2 {
		return nil, nil, ipld.ErrIteratorOverread{}
	}
	switch itr.idx {
	case 0:
		k = &fieldName__PBNode_Links_serial
		v = itr.n.Links.Representation()
	case 1:
		k = &fieldName__PBNode_Data_serial
		v = itr.n.Data.Representation()
	default:
		panic("unreachable")
	}
	itr.idx++
	return
}
func (itr *_PBNode__ReprMapItr) Done() bool {
	return itr.idx >= 2
}
func (_PBNode__Repr) ListIterator() ipld.ListIterator {
	return nil
}
func (rn *_PBNode__Repr) Length() int {
	l := 2
	return l
}
func (_PBNode__Repr) IsAbsent() bool {
	return false
}
func (_PBNode__Repr) IsNull() bool {
	return false
}
func (_PBNode__Repr) AsBool() (bool, error) {
	return mixins.Map{"dagpb.PBNode.Repr"}.AsBool()
}
func (_PBNode__Repr) AsInt() (int, error) {
	return mixins.Map{"dagpb.PBNode.Repr"}.AsInt()
}
func (_PBNode__Repr) AsFloat() (float64, error) {
	return mixins.Map{"dagpb.PBNode.Repr"}.AsFloat()
}
func (_PBNode__Repr) AsString() (string, error) {
	return mixins.Map{"dagpb.PBNode.Repr"}.AsString()
}
func (_PBNode__Repr) AsBytes() ([]byte, error) {
	return mixins.Map{"dagpb.PBNode.Repr"}.AsBytes()
}
func (_PBNode__Repr) AsLink() (ipld.Link, error) {
	return mixins.Map{"dagpb.PBNode.Repr"}.AsLink()
}
func (_PBNode__Repr) Prototype() ipld.NodePrototype {
	return _PBNode__ReprPrototype{}
}

type _PBNode__ReprPrototype struct{}

func (_PBNode__ReprPrototype) NewBuilder() ipld.NodeBuilder {
	var nb _PBNode__ReprBuilder
	nb.Reset()
	return &nb
}

type _PBNode__ReprBuilder struct {
	_PBNode__ReprAssembler
}

func (nb *_PBNode__ReprBuilder) Build() ipld.Node {
	if *nb.m != schema.Maybe_Value {
		panic("invalid state: cannot call Build on an assembler that's not finished")
	}
	return nb.w
}
func (nb *_PBNode__ReprBuilder) Reset() {
	var w _PBNode
	var m schema.Maybe
	*nb = _PBNode__ReprBuilder{_PBNode__ReprAssembler{w: &w, m: &m}}
}

type _PBNode__ReprAssembler struct {
	w     *_PBNode
	m     *schema.Maybe
	state maState
	s     int
	f     int

	cm       schema.Maybe
	ca_Links _PBLinks__ReprAssembler
	ca_Data  _Bytes__ReprAssembler
}

func (na *_PBNode__ReprAssembler) reset() {
	na.state = maState_initial
	na.s = 0
	na.ca_Links.reset()
	na.ca_Data.reset()
}
func (na *_PBNode__ReprAssembler) BeginMap(int) (ipld.MapAssembler, error) {
	switch *na.m {
	case schema.Maybe_Value, schema.Maybe_Null:
		panic("invalid state: cannot assign into assembler that's already finished")
	case midvalue:
		panic("invalid state: it makes no sense to 'begin' twice on the same assembler!")
	}
	*na.m = midvalue
	if na.w == nil {
		na.w = &_PBNode{}
	}
	return na, nil
}
func (_PBNode__ReprAssembler) BeginList(sizeHint int) (ipld.ListAssembler, error) {
	return mixins.MapAssembler{"dagpb.PBNode.Repr"}.BeginList(0)
}
func (na *_PBNode__ReprAssembler) AssignNull() error {
	switch *na.m {
	case allowNull:
		*na.m = schema.Maybe_Null
		return nil
	case schema.Maybe_Absent:
		return mixins.MapAssembler{"dagpb.PBNode.Repr.Repr"}.AssignNull()
	case schema.Maybe_Value, schema.Maybe_Null:
		panic("invalid state: cannot assign into assembler that's already finished")
	case midvalue:
		panic("invalid state: cannot assign null into an assembler that's already begun working on recursive structures!")
	}
	panic("unreachable")
}
func (_PBNode__ReprAssembler) AssignBool(bool) error {
	return mixins.MapAssembler{"dagpb.PBNode.Repr"}.AssignBool(false)
}
func (_PBNode__ReprAssembler) AssignInt(int) error {
	return mixins.MapAssembler{"dagpb.PBNode.Repr"}.AssignInt(0)
}
func (_PBNode__ReprAssembler) AssignFloat(float64) error {
	return mixins.MapAssembler{"dagpb.PBNode.Repr"}.AssignFloat(0)
}
func (_PBNode__ReprAssembler) AssignString(string) error {
	return mixins.MapAssembler{"dagpb.PBNode.Repr"}.AssignString("")
}
func (_PBNode__ReprAssembler) AssignBytes([]byte) error {
	return mixins.MapAssembler{"dagpb.PBNode.Repr"}.AssignBytes(nil)
}
func (_PBNode__ReprAssembler) AssignLink(ipld.Link) error {
	return mixins.MapAssembler{"dagpb.PBNode.Repr"}.AssignLink(nil)
}
func (na *_PBNode__ReprAssembler) AssignNode(v ipld.Node) error {
	if v.IsNull() {
		return na.AssignNull()
	}
	if v2, ok := v.(*_PBNode); ok {
		switch *na.m {
		case schema.Maybe_Value, schema.Maybe_Null:
			panic("invalid state: cannot assign into assembler that's already finished")
		case midvalue:
			panic("invalid state: cannot assign null into an assembler that's already begun working on recursive structures!")
		}
		if na.w == nil {
			na.w = v2
			*na.m = schema.Maybe_Value
			return nil
		}
		*na.w = *v2
		*na.m = schema.Maybe_Value
		return nil
	}
	if v.ReprKind() != ipld.ReprKind_Map {
		return ipld.ErrWrongKind{TypeName: "dagpb.PBNode.Repr", MethodName: "AssignNode", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: v.ReprKind()}
	}
	itr := v.MapIterator()
	for !itr.Done() {
		k, v, err := itr.Next()
		if err != nil {
			return err
		}
		if err := na.AssembleKey().AssignNode(k); err != nil {
			return err
		}
		if err := na.AssembleValue().AssignNode(v); err != nil {
			return err
		}
	}
	return na.Finish()
}
func (_PBNode__ReprAssembler) Prototype() ipld.NodePrototype {
	return _PBNode__ReprPrototype{}
}
func (ma *_PBNode__ReprAssembler) valueFinishTidy() bool {
	switch ma.f {
	case 0:
		switch ma.cm {
		case schema.Maybe_Value:
			ma.cm = schema.Maybe_Absent
			ma.state = maState_initial
			return true
		default:
			return false
		}
	case 1:
		switch ma.cm {
		case schema.Maybe_Value:
			ma.cm = schema.Maybe_Absent
			ma.state = maState_initial
			return true
		default:
			return false
		}
	default:
		panic("unreachable")
	}
}
func (ma *_PBNode__ReprAssembler) AssembleEntry(k string) (ipld.NodeAssembler, error) {
	switch ma.state {
	case maState_initial:
		// carry on
	case maState_midKey:
		panic("invalid state: AssembleEntry cannot be called when in the middle of assembling another key")
	case maState_expectValue:
		panic("invalid state: AssembleEntry cannot be called when expecting start of value assembly")
	case maState_midValue:
		if !ma.valueFinishTidy() {
			panic("invalid state: AssembleEntry cannot be called when in the middle of assembling a value")
		} // if tidy success: carry on
	case maState_finished:
		panic("invalid state: AssembleEntry cannot be called on an assembler that's already finished")
	}
	switch k {
	case "Links":
		if ma.s&fieldBit__PBNode_Links != 0 {
			return nil, ipld.ErrRepeatedMapKey{&fieldName__PBNode_Links_serial}
		}
		ma.s += fieldBit__PBNode_Links
		ma.state = maState_midValue
		ma.f = 0
		ma.ca_Links.w = &ma.w.Links
		ma.ca_Links.m = &ma.cm
		return &ma.ca_Links, nil
	case "Data":
		if ma.s&fieldBit__PBNode_Data != 0 {
			return nil, ipld.ErrRepeatedMapKey{&fieldName__PBNode_Data_serial}
		}
		ma.s += fieldBit__PBNode_Data
		ma.state = maState_midValue
		ma.f = 1
		ma.ca_Data.w = &ma.w.Data
		ma.ca_Data.m = &ma.cm
		return &ma.ca_Data, nil
	default:
		return nil, ipld.ErrInvalidKey{TypeName: "dagpb.PBNode.Repr", Key: &_String{k}}
	}
}
func (ma *_PBNode__ReprAssembler) AssembleKey() ipld.NodeAssembler {
	switch ma.state {
	case maState_initial:
		// carry on
	case maState_midKey:
		panic("invalid state: AssembleKey cannot be called when in the middle of assembling another key")
	case maState_expectValue:
		panic("invalid state: AssembleKey cannot be called when expecting start of value assembly")
	case maState_midValue:
		if !ma.valueFinishTidy() {
			panic("invalid state: AssembleKey cannot be called when in the middle of assembling a value")
		} // if tidy success: carry on
	case maState_finished:
		panic("invalid state: AssembleKey cannot be called on an assembler that's already finished")
	}
	ma.state = maState_midKey
	return (*_PBNode__ReprKeyAssembler)(ma)
}
func (ma *_PBNode__ReprAssembler) AssembleValue() ipld.NodeAssembler {
	switch ma.state {
	case maState_initial:
		panic("invalid state: AssembleValue cannot be called when no key is primed")
	case maState_midKey:
		panic("invalid state: AssembleValue cannot be called when in the middle of assembling a key")
	case maState_expectValue:
		// carry on
	case maState_midValue:
		panic("invalid state: AssembleValue cannot be called when in the middle of assembling another value")
	case maState_finished:
		panic("invalid state: AssembleValue cannot be called on an assembler that's already finished")
	}
	ma.state = maState_midValue
	switch ma.f {
	case 0:
		ma.ca_Links.w = &ma.w.Links
		ma.ca_Links.m = &ma.cm
		return &ma.ca_Links
	case 1:
		ma.ca_Data.w = &ma.w.Data
		ma.ca_Data.m = &ma.cm
		return &ma.ca_Data
	default:
		panic("unreachable")
	}
}
func (ma *_PBNode__ReprAssembler) Finish() error {
	switch ma.state {
	case maState_initial:
		// carry on
	case maState_midKey:
		panic("invalid state: Finish cannot be called when in the middle of assembling a key")
	case maState_expectValue:
		panic("invalid state: Finish cannot be called when expecting start of value assembly")
	case maState_midValue:
		if !ma.valueFinishTidy() {
			panic("invalid state: Finish cannot be called when in the middle of assembling a value")
		} // if tidy success: carry on
	case maState_finished:
		panic("invalid state: Finish cannot be called on an assembler that's already finished")
	}
	//FIXME check if all required fields are set
	ma.state = maState_finished
	*ma.m = schema.Maybe_Value
	return nil
}
func (ma *_PBNode__ReprAssembler) KeyPrototype() ipld.NodePrototype {
	return _String__Prototype{}
}
func (ma *_PBNode__ReprAssembler) ValuePrototype(k string) ipld.NodePrototype {
	panic("todo structbuilder mapassembler repr valueprototype")
}

type _PBNode__ReprKeyAssembler _PBNode__ReprAssembler

func (_PBNode__ReprKeyAssembler) BeginMap(sizeHint int) (ipld.MapAssembler, error) {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.BeginMap(0)
}
func (_PBNode__ReprKeyAssembler) BeginList(sizeHint int) (ipld.ListAssembler, error) {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.BeginList(0)
}
func (na *_PBNode__ReprKeyAssembler) AssignNull() error {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.AssignNull()
}
func (_PBNode__ReprKeyAssembler) AssignBool(bool) error {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.AssignBool(false)
}
func (_PBNode__ReprKeyAssembler) AssignInt(int) error {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.AssignInt(0)
}
func (_PBNode__ReprKeyAssembler) AssignFloat(float64) error {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.AssignFloat(0)
}
func (ka *_PBNode__ReprKeyAssembler) AssignString(k string) error {
	if ka.state != maState_midKey {
		panic("misuse: KeyAssembler held beyond its valid lifetime")
	}
	switch k {
	case "Links":
		if ka.s&fieldBit__PBNode_Links != 0 {
			return ipld.ErrRepeatedMapKey{&fieldName__PBNode_Links_serial}
		}
		ka.s += fieldBit__PBNode_Links
		ka.state = maState_expectValue
		ka.f = 0
	case "Data":
		if ka.s&fieldBit__PBNode_Data != 0 {
			return ipld.ErrRepeatedMapKey{&fieldName__PBNode_Data_serial}
		}
		ka.s += fieldBit__PBNode_Data
		ka.state = maState_expectValue
		ka.f = 1
	default:
		return ipld.ErrInvalidKey{TypeName: "dagpb.PBNode.Repr", Key: &_String{k}}
	}
	return nil
}
func (_PBNode__ReprKeyAssembler) AssignBytes([]byte) error {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.AssignBytes(nil)
}
func (_PBNode__ReprKeyAssembler) AssignLink(ipld.Link) error {
	return mixins.StringAssembler{"dagpb.PBNode.Repr.KeyAssembler"}.AssignLink(nil)
}
func (ka *_PBNode__ReprKeyAssembler) AssignNode(v ipld.Node) error {
	if v2, err := v.AsString(); err != nil {
		return err
	} else {
		return ka.AssignString(v2)
	}
}
func (_PBNode__ReprKeyAssembler) Prototype() ipld.NodePrototype {
	return _String__Prototype{}
}
