genKindStruct.go 2.67 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 28 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

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 }}{}

		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 {
					return ipld.Undefined, nil
				}
				if x.{{ $field.Name }} == nil {
					return ipld.Nil, nil
				}
				{{- else if $field.IsOptional }}
				if x.{{ $field.Name }} == nil {
					return ipld.Undefined, nil
				}
				{{- else if $field.IsNullable }}
				if x.{{ $field.Name }} == nil {
					return ipld.Nil, nil
				}
				{{- end}}
				return x.{{ $field.Name }}, nil
			{{- end}}
			default:
				return nil, ipld.ErrNoSuchField{FieldName: key} /* FIXME would this belong in the typed package?  or..? */
			}
		}
	`, w, gk)
}

func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
	doTemplate(`
		func ({{ .Type.Name }}) MapIterator() ipld.MapIterator {
			return nil // TODO EmitNodeMethodMapIterator
		}
	`, 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)
}