Commit 68b3383d authored by Eric Myhre's avatar Eric Myhre

gen: rearrange files.

The previous names started to seem more questionable as we start
generating additional code which is natively typed rather than
interface constrained.  Now the latter stuff is in a file that
includes 'Node' as part of the filename.  This seems better.
parent 41d79374
......@@ -18,13 +18,13 @@ import (
// 5 is emitted from another `nodeGenerator` instance.
// 4 and 6 are emitted from two distinct `nodebuilderGenerator` instances.
// you'll find a file in this package per kind
// (schema level kind, not data model level reprkind)...
// sparse cross-product with their representation strategy (more or less)
// (it's more... idunnoyet. hopefully we have implstrats and reprstrats,
// and those combine over an interface so it's not a triple cross product...
// and hopefully that interface is nodebuilder,
// because I dunno why it wouldn't be unless we goof on perf somehow).
// file patterns in this package:
//
// - for each kind:
// - `genKind{Kind}.go` -- has emitters for the native type parts (1, 2, 7).
// - `genKind{Kind}Node.go` -- has emitters for the typed node parts (3, 4), and the entrypoint to (5).
// - for each representation that kind can have:
// - `genKind{Kind}Repr{ReprStrat}.go` -- has emitters for (5, 6).
// typedNodeGenerator declares a standard names for a bunch of methods for generating
// code for our schema types. There's still numerous places where other casts
......
......@@ -85,47 +85,3 @@ func (gk generateKindString) EmitNativeMaybe(w io.Writer) {
`, w, gk)
}
func (gk generateKindString) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = {{ .Type | mungeTypeNodeIdent }}{}
var _ typed.Node = {{ .Type | mungeTypeNodeIdent }}{}
`, w, gk)
}
func (gk generateKindString) EmitTypedNodeMethodType(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Type() schema.Type {
return nil /*TODO:typelit*/
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_String
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) AsString() (string, error) {
return x.x, nil
}
`, w, gk)
}
func (gk generateKindString) EmitTypedNodeMethodRepresentation(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Representation() ipld.Node {
panic("TODO representation")
}
`, w, gk)
}
func (gk generateKindString) GetRepresentationNodeGen() nodeGenerator {
return nil // TODO of course
}
......@@ -6,14 +6,44 @@ import (
"github.com/ipld/go-ipld-prime/schema"
)
func (gk generateKindString) GetNodeBuilderGen() nodebuilderGenerator {
return generateNbKindString{
gk.Type,
genKindedNbRejections_String{
mungeTypeNodebuilderIdent(gk.Type),
string(gk.Type.Name()) + ".Builder",
},
}
func (gk generateKindString) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = {{ .Type | mungeTypeNodeIdent }}{}
var _ typed.Node = {{ .Type | mungeTypeNodeIdent }}{}
`, w, gk)
}
func (gk generateKindString) EmitTypedNodeMethodType(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Type() schema.Type {
return nil /*TODO:typelit*/
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_String
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) AsString() (string, error) {
return x.x, nil
}
`, w, gk)
}
func (gk generateKindString) EmitTypedNodeMethodRepresentation(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Representation() ipld.Node {
panic("TODO representation")
}
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodNodeBuilder(w io.Writer) {
......@@ -24,6 +54,16 @@ func (gk generateKindString) EmitNodeMethodNodeBuilder(w io.Writer) {
`, w, gk)
}
func (gk generateKindString) GetNodeBuilderGen() nodebuilderGenerator {
return generateNbKindString{
gk.Type,
genKindedNbRejections_String{
mungeTypeNodebuilderIdent(gk.Type),
string(gk.Type.Name()) + ".Builder",
},
}
}
type generateNbKindString struct {
Type schema.TypeString
genKindedNbRejections_String
......@@ -50,3 +90,7 @@ func (gk generateNbKindString) EmitNodebuilderMethodCreateString(w io.Writer) {
}
`, w, gk)
}
func (gk generateKindString) GetRepresentationNodeGen() nodeGenerator {
return nil // TODO of course
}
......@@ -96,153 +96,3 @@ func (gk generateKindStruct) EmitNativeMaybe(w io.Writer) {
`, w, gk)
}
func (gk generateKindStruct) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = {{ .Type | mungeTypeNodeIdent }}{}
var _ typed.Node = {{ .Type | mungeTypeNodeIdent }}{}
`, w, gk)
}
func (gk generateKindStruct) EmitTypedNodeMethodType(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Type() schema.Type {
return nil /*TODO:typelit*/
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Map
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodLookupString(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) LookupString(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.Undef, nil
}
if x.{{ $field.Name }} == nil {
return ipld.Null, nil
}
{{- else if $field.IsOptional }}
if x.{{ $field.Name }} == nil {
return ipld.Undef, nil
}
{{- else if $field.IsNullable }}
if x.{{ $field.Name }} == nil {
return ipld.Null, nil
}
{{- end}}
{{- if or $field.IsOptional $field.IsNullable }}
return *x.{{ $field.Name }}, nil
{{- else}}
return x.{{ $field.Name }}, nil
{{- end}}
{{- end}}
default:
return nil, typed.ErrNoSuchField{Type: nil /*TODO*/, FieldName: key}
}
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodLookup(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) Lookup(key ipld.Node) (ipld.Node, error) {
ks, err := key.AsString()
if err != nil {
return nil, ipld.ErrInvalidKey{"got " + key.ReprKind().String() + ", need string"}
}
return x.LookupString(ks)
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) MapIterator() ipld.MapIterator {
return &{{ .Type | mungeTypeNodeItrIdent }}{&x, 0}
}
type {{ .Type | mungeTypeNodeItrIdent }} struct {
node *{{ .Type | mungeTypeNodeIdent }}
idx int
}
func (itr *{{ .Type | mungeTypeNodeItrIdent }}) 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 | mungeTypeNodeItrIdent }}) Done() bool {
return itr.idx >= {{ len .Type.Fields }}
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodLength(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Length() int {
return {{ len .Type.Fields }}
}
`, w, gk)
}
func (gk generateKindStruct) EmitTypedNodeMethodRepresentation(w io.Writer) {
doTemplate(`
func (n {{ .Type | mungeTypeNodeIdent }}) Representation() ipld.Node {
return {{ .Type | mungeTypeReprNodeIdent }}{&n}
}
`, w, gk)
}
func (gk generateKindStruct) GetRepresentationNodeGen() nodeGenerator {
switch gk.Type.RepresentationStrategy().(type) {
case schema.StructRepresentation_Map:
return getStructRepresentationMapNodeGen(gk.Type)
default:
panic("missing case in switch for repr strategy for structs")
}
}
......@@ -6,14 +6,145 @@ import (
"github.com/ipld/go-ipld-prime/schema"
)
func (gk generateKindStruct) GetNodeBuilderGen() nodebuilderGenerator {
return generateNbKindStruct{
gk.Type,
genKindedNbRejections_Map{
mungeTypeNodebuilderIdent(gk.Type),
string(gk.Type.Name()) + ".Builder",
},
}
func (gk generateKindStruct) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = {{ .Type | mungeTypeNodeIdent }}{}
var _ typed.Node = {{ .Type | mungeTypeNodeIdent }}{}
`, w, gk)
}
func (gk generateKindStruct) EmitTypedNodeMethodType(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Type() schema.Type {
return nil /*TODO:typelit*/
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Map
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodLookupString(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) LookupString(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.Undef, nil
}
if x.{{ $field.Name }} == nil {
return ipld.Null, nil
}
{{- else if $field.IsOptional }}
if x.{{ $field.Name }} == nil {
return ipld.Undef, nil
}
{{- else if $field.IsNullable }}
if x.{{ $field.Name }} == nil {
return ipld.Null, nil
}
{{- end}}
{{- if or $field.IsOptional $field.IsNullable }}
return *x.{{ $field.Name }}, nil
{{- else}}
return x.{{ $field.Name }}, nil
{{- end}}
{{- end}}
default:
return nil, typed.ErrNoSuchField{Type: nil /*TODO*/, FieldName: key}
}
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodLookup(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) Lookup(key ipld.Node) (ipld.Node, error) {
ks, err := key.AsString()
if err != nil {
return nil, ipld.ErrInvalidKey{"got " + key.ReprKind().String() + ", need string"}
}
return x.LookupString(ks)
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) MapIterator() ipld.MapIterator {
return &{{ .Type | mungeTypeNodeItrIdent }}{&x, 0}
}
type {{ .Type | mungeTypeNodeItrIdent }} struct {
node *{{ .Type | mungeTypeNodeIdent }}
idx int
}
func (itr *{{ .Type | mungeTypeNodeItrIdent }}) 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 | mungeTypeNodeItrIdent }}) Done() bool {
return itr.idx >= {{ len .Type.Fields }}
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodLength(w io.Writer) {
doTemplate(`
func ({{ .Type | mungeTypeNodeIdent }}) Length() int {
return {{ len .Type.Fields }}
}
`, w, gk)
}
func (gk generateKindStruct) EmitTypedNodeMethodRepresentation(w io.Writer) {
doTemplate(`
func (n {{ .Type | mungeTypeNodeIdent }}) Representation() ipld.Node {
return {{ .Type | mungeTypeReprNodeIdent }}{&n}
}
`, w, gk)
}
func (gk generateKindStruct) EmitNodeMethodNodeBuilder(w io.Writer) {
......@@ -24,6 +155,16 @@ func (gk generateKindStruct) EmitNodeMethodNodeBuilder(w io.Writer) {
`, w, gk)
}
func (gk generateKindStruct) GetNodeBuilderGen() nodebuilderGenerator {
return generateNbKindStruct{
gk.Type,
genKindedNbRejections_Map{
mungeTypeNodebuilderIdent(gk.Type),
string(gk.Type.Name()) + ".Builder",
},
}
}
type generateNbKindStruct struct {
Type schema.TypeStruct
genKindedNbRejections_Map
......@@ -164,3 +305,12 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodAmendMap(w io.Writer) {
}
`, w, gk)
}
func (gk generateKindStruct) GetRepresentationNodeGen() nodeGenerator {
switch gk.Type.RepresentationStrategy().(type) {
case schema.StructRepresentation_Map:
return getStructRepresentationMapNodeGen(gk.Type)
default:
panic("missing case in switch for repr strategy for structs")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment