genKindStruct.go 3.7 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3
package gengo

import (
Eric Myhre's avatar
Eric Myhre committed
4 5
	"io"

Eric Myhre's avatar
Eric Myhre committed
6 7 8
	"github.com/ipld/go-ipld-prime/schema"
)

Eric Myhre's avatar
Eric Myhre committed
9 10 11 12 13 14 15
func NewGeneratorForKindStruct(t schema.Type) typeGenerator {
	return generateKindStruct{
		t.(schema.TypeStruct),
		generateKindedRejections_Map{t},
	}
}

Eric Myhre's avatar
Eric Myhre committed
16
type generateKindStruct struct {
Eric Myhre's avatar
Eric Myhre committed
17 18
	Type schema.TypeStruct
	generateKindedRejections_Map
Eric Myhre's avatar
Eric Myhre committed
19 20 21
	// FUTURE: probably some adjunct config data should come with here as well.
	// FUTURE: perhaps both a global one (e.g. output package name) and a per-type one.
}
Eric Myhre's avatar
Eric Myhre committed
22 23 24 25 26 27

func (gk generateKindStruct) EmitNodeType(w io.Writer) {
	// Observe that we get a '*' if a field is *either* nullable *or* optional;
	//  and we get an extra bool for the second cardinality +1'er if both are true.
	doTemplate(`
		var _ ipld.Node = {{ .Type.Name }}{}
28
		var _ typed.Node = typed.Node(nil) // TODO
Eric Myhre's avatar
Eric Myhre committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

		type {{ .Type.Name }} struct{
			{{- range $field := .Type.Fields }}
			{{ $field.Name }} {{if or $field.IsOptional $field.IsNullable }}*{{end}}{{ $field.Type.Name }}
			{{- end}}
			{{ range $field := .Type.Fields }}
			{{- if and $field.IsOptional $field.IsNullable }}
			{{ $field.Name }}__exists bool
			{{- end}}
			{{- end}}
		}

	`, w, gk)
}

func (gk generateKindStruct) EmitNodeMethodReprKind(w io.Writer) {
	doTemplate(`
		func ({{ .Type.Name }}) ReprKind() ipld.ReprKind {
			return ipld.ReprKind_Map
		}
	`, w, gk)
}

func (gk generateKindStruct) EmitNodeMethodTraverseField(w io.Writer) {
	doTemplate(`
		func (x {{ .Type.Name }}) TraverseField(key string) (ipld.Node, error) {
			switch key {
			{{- range $field := .Type.Fields }}
			case "{{ $field.Name }}":
				{{- if and $field.IsOptional $field.IsNullable }}
				if !x.{{ $field.Name }}__exists {
60
					return ipld.Undef, nil
Eric Myhre's avatar
Eric Myhre committed
61 62
				}
				if x.{{ $field.Name }} == nil {
63
					return ipld.Null, nil
Eric Myhre's avatar
Eric Myhre committed
64 65 66
				}
				{{- else if $field.IsOptional }}
				if x.{{ $field.Name }} == nil {
67
					return ipld.Undef, nil
Eric Myhre's avatar
Eric Myhre committed
68 69 70
				}
				{{- else if $field.IsNullable }}
				if x.{{ $field.Name }} == nil {
71
					return ipld.Null, nil
Eric Myhre's avatar
Eric Myhre committed
72 73 74 75 76
				}
				{{- end}}
				return x.{{ $field.Name }}, nil
			{{- end}}
			default:
77
				return nil, typed.ErrNoSuchField{Type: nil /*TODO*/, FieldName: key}
Eric Myhre's avatar
Eric Myhre committed
78 79 80 81 82 83 84
			}
		}
	`, w, gk)
}

func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
	doTemplate(`
85 86
		func (x {{ .Type.Name }}) MapIterator() ipld.MapIterator {
			return &_{{ .Type.Name }}__itr{&x, 0}
Eric Myhre's avatar
Eric Myhre committed
87
		}
88 89 90 91 92 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

		type _{{ .Type.Name }}__itr struct {
			node *{{ .Type.Name }}
			idx  int
		}

		func (itr *_{{ .Type.Name }}__itr) Next() (k ipld.Node, v ipld.Node, _ error) {
			if itr.idx >= {{ len .Type.Fields }} {
				return nil, nil, ipld.ErrIteratorOverread{}
			}
			switch itr.idx {
			{{- range $i, $field := .Type.Fields }}
			case {{ $i }}:
				k = String{"{{ $field.Name }}"}
				{{- if and $field.IsOptional $field.IsNullable }}
				if !itr.node.{{ $field.Name }}__exists {
					v = ipld.Undef
					break
				}
				if itr.node.{{ $field.Name }} == nil {
					v = ipld.Null
					break
				}
				{{- else if $field.IsOptional }}
				if itr.node.{{ $field.Name }} == nil {
					v = ipld.Undef
					break
				}
				{{- else if $field.IsNullable }}
				if itr.node.{{ $field.Name }} == nil {
					v = ipld.Null
					break
				}
				{{- end}}
				v = itr.node.{{ $field.Name }}
			{{- end}}
			default:
				panic("unreachable")
			}
			itr.idx++
			return
		}
		func (itr *_{{ .Type.Name }}__itr) Done() bool {
			return itr.idx >= {{ len .Type.Fields }}
		}

Eric Myhre's avatar
Eric Myhre committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	`, w, gk)
}

func (gk generateKindStruct) EmitNodeMethodLength(w io.Writer) {
	doTemplate(`
		func ({{ .Type.Name }}) Length() int {
			return {{ len .Type.Fields }}
		}
	`, w, gk)
}

func (gk generateKindStruct) EmitNodeMethodNodeBuilder(w io.Writer) {
	doTemplate(`
		func ({{ .Type.Name }}) NodeBuilder() ipld.NodeBuilder {
			return nil // TODO EmitNodeMethodNodeBuilder
		}
	`, w, gk)
}