Commit ec1434e7 authored by Eric Myhre's avatar Eric Myhre

Finish migrating to ident munge helper funcs.

Fixed at least one bug along the way (in iterators, which don't have
test coverage yet, so no test fix.  Still planning to cover those
via serialization, when we get that feature, "soon").

'go doc .' on the generated code now only lists one type per type in
the schema which seems like a good sanity heuristic; and
'go doc -u .' on the package now looks much more consistent.
(There's *8* types for every struct in the schema!  Uffdah.
But if that's what it takes to make a focused,
correctness-emphasizing library surface area, so be it.)
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent a84c7ec1
......@@ -32,7 +32,7 @@ func (gk generateKindStruct) EmitNodeType(w io.Writer) {
type {{ .Type | mungeTypeNodeIdent }} struct{
{{- range $field := .Type.Fields }}
{{ $field.Name }} {{if or $field.IsOptional $field.IsNullable }}*{{end}}{{ $field.Type.Name }}
{{ $field.Name }} {{if or $field.IsOptional $field.IsNullable }}*{{end}}{{ $field.Type | mungeTypeNodeIdent }}
{{- end}}
{{ range $field := .Type.Fields }}
{{- if and $field.IsOptional $field.IsNullable }}
......@@ -110,15 +110,15 @@ func (gk generateKindStruct) EmitNodeMethodLookup(w io.Writer) {
func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
doTemplate(`
func (x {{ .Type | mungeTypeNodeIdent }}) MapIterator() ipld.MapIterator {
return &_{{ .Type.Name }}__itr{&x, 0}
return &{{ .Type | mungeTypeNodeItrIdent }}{&x, 0}
}
type _{{ .Type.Name }}__itr struct {
type {{ .Type | mungeTypeNodeItrIdent }} struct {
node *{{ .Type | mungeTypeNodeIdent }}
idx int
}
func (itr *_{{ .Type.Name }}__itr) Next() (k ipld.Node, v ipld.Node, _ error) {
func (itr *{{ .Type | mungeTypeNodeItrIdent }}) Next() (k ipld.Node, v ipld.Node, _ error) {
if itr.idx >= {{ len .Type.Fields }} {
return nil, nil, ipld.ErrIteratorOverread{}
}
......@@ -154,7 +154,7 @@ func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
itr.idx++
return
}
func (itr *_{{ .Type.Name }}__itr) Done() bool {
func (itr *{{ .Type | mungeTypeNodeItrIdent }}) Done() bool {
return itr.idx >= {{ len .Type.Fields }}
}
......
......@@ -52,11 +52,11 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodCreateMap(w io.Writer) {
// REVIEW: 'x, ok := v.({{ $field.Type.Name }})' might need some stars in it... sometimes.
doTemplate(`
func (nb {{ .Type | mungeTypeNodebuilderIdent }}) CreateMap() (ipld.MapBuilder, error) {
return &{{ .Type.Name }}__MapBuilder{v:&{{ .Type | mungeTypeNodeIdent }}{}}, nil
return &{{ .Type | mungeTypeNodeMapBuilderIdent }}{v:&{{ .Type | mungeTypeNodeIdent }}{}}, nil
}
type {{ .Type.Name }}__MapBuilder struct{
v *{{ .Type.Name }}
type {{ .Type | mungeTypeNodeMapBuilderIdent }} struct{
v *{{ .Type | mungeTypeNodeIdent }}
{{- range $field := .Type.Fields }}
{{- if not $field.IsOptional }}
{{ $field.Name }}__isset bool
......@@ -64,7 +64,7 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodCreateMap(w io.Writer) {
{{- end}}
}
func (mb *{{ .Type.Name }}__MapBuilder) Insert(k, v ipld.Node) error {
func (mb *{{ .Type | mungeTypeNodeMapBuilderIdent }}) Insert(k, v ipld.Node) error {
ks, err := k.AsString()
if err != nil {
return ipld.ErrInvalidKey{"not a string: " + err.Error()}
......@@ -92,7 +92,7 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodCreateMap(w io.Writer) {
if !ok {
panic("need typed.Node for insertion into struct") // FIXME need an error type for this
}
x, ok := v.({{ $field.Type.Name }})
x, ok := v.({{ $field.Type | mungeTypeNodeIdent }})
if !ok {
panic("field '{{$field.Name}}' in type {{$type.Name}} is type {{$field.Type.Name}}; cannot assign "+tv.Type().Name()) // FIXME need an error type for this
}
......@@ -113,10 +113,10 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodCreateMap(w io.Writer) {
}
return nil
}
func (mb *{{ .Type.Name }}__MapBuilder) Delete(k ipld.Node) error {
func (mb *{{ .Type | mungeTypeNodeMapBuilderIdent }}) Delete(k ipld.Node) error {
panic("TODO later")
}
func (mb *{{ .Type.Name }}__MapBuilder) Build() (ipld.Node, error) {
func (mb *{{ .Type | mungeTypeNodeMapBuilderIdent }}) Build() (ipld.Node, error) {
{{- $type := .Type -}} {{- /* ranging modifies dot, unhelpfully */ -}}
{{- range $field := .Type.Fields }}
{{- if not $field.IsOptional }}
......
......@@ -26,7 +26,7 @@ func (gk generateStructReprMapNode) EmitNodeType(w io.Writer) {
var _ ipld.Node = {{ .Type | mungeTypeReprNodeIdent }}{}
type {{ .Type | mungeTypeReprNodeIdent }} struct{
n *{{ .Type.Name }}
n *{{ .Type | mungeTypeNodeIdent }}
}
`, w, gk)
......@@ -97,15 +97,15 @@ func (gk generateStructReprMapNode) EmitNodeMethodMapIterator(w io.Writer) {
// TODO : support for implicits is missing.
doTemplate(`
func (rn {{ .Type | mungeTypeReprNodeIdent }}) MapIterator() ipld.MapIterator {
return &_{{ .Type.Name }}__itr{rn.n, 0}
return &{{ .Type | mungeTypeReprNodeItrIdent }}{rn.n, 0}
}
type {{ .Type | mungeTypeReprNodeIdent }}Itr struct {
node *{{ .Type.Name }}
type {{ .Type | mungeTypeReprNodeItrIdent }} struct {
node *{{ .Type | mungeTypeNodeIdent }}
idx int
}
func (itr *{{ .Type | mungeTypeReprNodeIdent }}Itr) Next() (k ipld.Node, v ipld.Node, _ error) {
func (itr *{{ .Type | mungeTypeReprNodeItrIdent }}) Next() (k ipld.Node, v ipld.Node, _ error) {
if itr.idx >= {{ len .Type.Fields }} {
return nil, nil, ipld.ErrIteratorOverread{}
}
......@@ -144,7 +144,7 @@ func (gk generateStructReprMapNode) EmitNodeMethodMapIterator(w io.Writer) {
itr.idx++
return
}
func (itr *{{ .Type | mungeTypeReprNodeIdent }}Itr) Done() bool {
func (itr *{{ .Type | mungeTypeReprNodeItrIdent }}) Done() bool {
return itr.idx >= {{ len .Type.Fields }}
}
......@@ -186,7 +186,7 @@ func (gk generateStructReprMapNode) GetNodeBuilderGen() nodebuilderGenerator {
return generateStructReprMapNb{
gk.Type,
genKindedNbRejections_Map{
"_" + string(gk.Type.Name()) + "__ReprBuilder",
mungeTypeReprNodebuilderIdent(gk.Type),
string(gk.Type.Name()) + ".Representation.Builder",
},
}
......@@ -221,17 +221,17 @@ func (gk generateStructReprMapNb) EmitNodebuilderMethodCreateMap(w io.Writer) {
// TODO : support for implicits is missing.
doTemplate(`
func (nb {{ .Type | mungeTypeReprNodebuilderIdent }}) CreateMap() (ipld.MapBuilder, error) {
return &_{{ .Type.Name }}__ReprMapBuilder{v:&{{ .Type.Name }}{}}, nil
return &{{ .Type | mungeTypeReprNodeMapBuilderIdent }}{v:&{{ .Type | mungeTypeNodeIdent }}{}}, nil
}
type _{{ .Type.Name }}__ReprMapBuilder struct{
v *{{ .Type.Name }}
type {{ .Type | mungeTypeReprNodeMapBuilderIdent }} struct{
v *{{ .Type | mungeTypeNodeIdent }}
{{- range $field := .Type.Fields }}
{{ $field.Name }}__isset bool
{{- end}}
}
func (mb *_{{ .Type.Name }}__ReprMapBuilder) Insert(k, v ipld.Node) error {
func (mb *{{ .Type | mungeTypeReprNodeMapBuilderIdent }}) Insert(k, v ipld.Node) error {
ks, err := k.AsString()
if err != nil {
return ipld.ErrInvalidKey{"not a string: " + err.Error()}
......@@ -262,7 +262,7 @@ func (gk generateStructReprMapNb) EmitNodebuilderMethodCreateMap(w io.Writer) {
if !ok {
panic("need typed.Node for insertion into struct") // FIXME need an error type for this
}
x, ok := v.({{ $field.Type.Name }})
x, ok := v.({{ $field.Type | mungeTypeNodeIdent }})
if !ok {
panic("field '{{$field.Name}}' (key: '{{ $field | $type.RepresentationStrategy.GetFieldKey }}') in type {{$type.Name}} is type {{$field.Type.Name}}; cannot assign "+tv.Type().Name()) // FIXME need an error type for this
}
......@@ -282,10 +282,10 @@ func (gk generateStructReprMapNb) EmitNodebuilderMethodCreateMap(w io.Writer) {
}
return nil
}
func (mb *_{{ .Type.Name }}__ReprMapBuilder) Delete(k ipld.Node) error {
func (mb *{{ .Type | mungeTypeReprNodeMapBuilderIdent }}) Delete(k ipld.Node) error {
panic("TODO later")
}
func (mb *_{{ .Type.Name }}__ReprMapBuilder) Build() (ipld.Node, error) {
func (mb *{{ .Type | mungeTypeReprNodeMapBuilderIdent }}) Build() (ipld.Node, error) {
{{- $type := .Type -}} {{- /* ranging modifies dot, unhelpfully */ -}}
{{- range $field := .Type.Fields }}
{{- if not $field.IsOptional }}
......
......@@ -11,31 +11,38 @@ func mungeTypeNodeIdent(t schema.Type) string {
// future: something return a "_x__Node" might also make an appearance,
// which would address the fun detail that a gen'd struct type might not actually implement Node directly.
func mungeTypeNodeItrIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__Itr"
}
func mungeTypeNodebuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__NodeBuilder"
}
func mungeTypeNodeMapBuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__MapBuilder"
}
func mungeTypeNodeListBuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__ListBuilder"
}
func mungeTypeReprNodeIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__Repr"
}
func mungeTypeReprNodeItrIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__ReprItr"
}
func mungeTypeReprNodebuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__ReprBuilder"
}
// itr
// MapBuilder
// ListBuilder
// reprItr
// ReprMapBuilder
// ReprListBuilder
func mungeTypeReprNodeMapBuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__ReprMapBuilder"
}
// okay these are getting a little out of hand given how formulaic they are.
// the repr ones could all be glued onto the end of mungeTypeReprNodeIdent safely;
// problem with that is, the pattern doesn't always hold:
// things that are on the main node can't, because that symbol is exported.
func mungeTypeReprNodeListBuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__ReprListBuilder"
}
......@@ -22,10 +22,16 @@ func doTemplate(tmplstr string, w io.Writer, data interface{}) {
return a + b
},
"mungeTypeNodeIdent": mungeTypeNodeIdent,
"mungeTypeNodebuilderIdent": mungeTypeNodebuilderIdent,
"mungeTypeReprNodeIdent": mungeTypeReprNodeIdent,
"mungeTypeReprNodebuilderIdent": mungeTypeReprNodebuilderIdent,
"mungeTypeNodeIdent": mungeTypeNodeIdent,
"mungeTypeNodeItrIdent": mungeTypeNodeItrIdent,
"mungeTypeNodebuilderIdent": mungeTypeNodebuilderIdent,
"mungeTypeNodeMapBuilderIdent": mungeTypeNodeMapBuilderIdent,
"mungeTypeNodeListBuilderIdent": mungeTypeNodeListBuilderIdent,
"mungeTypeReprNodeIdent": mungeTypeReprNodeIdent,
"mungeTypeReprNodeItrIdent": mungeTypeReprNodeItrIdent,
"mungeTypeReprNodebuilderIdent": mungeTypeReprNodebuilderIdent,
"mungeTypeReprNodeMapBuilderIdent": mungeTypeReprNodeMapBuilderIdent,
"mungeTypeReprNodeListBuilderIdent": mungeTypeReprNodeListBuilderIdent,
}).
Parse(wish.Dedent(tmplstr)))
if err := tmpl.Execute(w, data); err != nil {
......
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