Commit a84c7ec1 authored by Eric Myhre's avatar Eric Myhre

DRYing some type ident munges.

I'm still aiming to keep this as simple and un-clever as possible,
because putting lipstick on a pig -- this is all about to become
strings which get shoveled back to a compiler parser anyway -- is
not useful, and becomes antiuseful if it obstructs readability...

But I'm starting to find these elements are repeated enough that
it will help rather than hurt readability to extract some things.

Also, since the munges have recently started to appear in both go code
source as well as in the templates, that starts to put more weight in
favor of extracting a function for it, which keeps the two syntactic
universes from drifting on this subject.

At the same time, moved all NodeBuilders to being unexported (by using
idents prefixed with a "_").  I looked at the godoc for the generated
code and felt this is looking like a wiser choice than exporting.

We'll need to export more methods for getting initial instances of the
now-unexported stuff... but we should be adding those anyway, so this
is not an argument against unexporting.

Some additional type idents around iterators and map builders have not
yet been hoisted to DRYed munge methods.  I'm debating if that's useful
(as you can see in the comments in that file), but leaning towards
it being more parsimoneous to just go for it.  So that'll probably be
the next commit.
Signed-off-by: default avatarEric Myhre <hash@exultant.us>
parent 116a4db7
......@@ -45,7 +45,7 @@ func TestScalarUnmarshal(t *testing.T) {
tb := &TokenSourceBucket{tokens: []tok.Token{
{Type: tok.TString, Str: "zooooom"},
}}
nb := String__NodeBuilder{}
nb := _String__NodeBuilder{}
n, err := encoding.Unmarshal(nb, tb)
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_String)
......@@ -66,28 +66,28 @@ func TestGeneratedStructs(t *testing.T) {
)
t.Run("type-level build and read", func(t *testing.T) {
t.Run("all fields set", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
mb, err := _Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f3"), plz(String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f4"), plz(String__NodeBuilder{}.CreateString("d")))
mb.Insert(ipldfree.String("f1"), plz(_String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(_String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f3"), plz(_String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f4"), plz(_String__NodeBuilder{}.CreateString("d")))
n, err := mb.Build()
v0 = n.(typed.Node)
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
t.Run("using null nullable", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
mb, err := _Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f3"), plz(String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f1"), plz(_String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(_String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f3"), plz(_String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f4"), ipld.Null)
n, err := mb.Build()
v1 = n.(typed.Node)
......@@ -95,62 +95,62 @@ func TestGeneratedStructs(t *testing.T) {
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 4)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, ipld.Null)
})
t.Run("using null optional nullable", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
mb, err := _Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f1"), plz(_String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(_String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f3"), ipld.Null)
mb.Insert(ipldfree.String("f4"), plz(String__NodeBuilder{}.CreateString("d")))
mb.Insert(ipldfree.String("f4"), plz(_String__NodeBuilder{}.CreateString("d")))
n, err := mb.Build()
v2 = n.(typed.Node)
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 4)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, ipld.Null)
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
t.Run("using skipped optional", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
mb, err := _Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f3"), plz(String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f4"), plz(String__NodeBuilder{}.CreateString("d")))
mb.Insert(ipldfree.String("f1"), plz(_String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f3"), plz(_String__NodeBuilder{}.CreateString("c")))
mb.Insert(ipldfree.String("f4"), plz(_String__NodeBuilder{}.CreateString("d")))
n, err := mb.Build()
v3 = n.(typed.Node)
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 4)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, ipld.Undef)
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
t.Run("using skipped optional nullable", func(t *testing.T) {
mb, err := Stroct__NodeBuilder{}.CreateMap()
mb, err := _Stroct__NodeBuilder{}.CreateMap()
Require(t, err, ShouldEqual, nil)
mb.Insert(ipldfree.String("f1"), plz(String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f4"), plz(String__NodeBuilder{}.CreateString("d")))
mb.Insert(ipldfree.String("f1"), plz(_String__NodeBuilder{}.CreateString("a")))
mb.Insert(ipldfree.String("f2"), plz(_String__NodeBuilder{}.CreateString("b")))
mb.Insert(ipldfree.String("f4"), plz(_String__NodeBuilder{}.CreateString("d")))
n, err := mb.Build()
v4 = n.(typed.Node)
Wish(t, err, ShouldEqual, nil)
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 4)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, ipld.Undef)
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
})
t.Run("representation read", func(t *testing.T) {
......@@ -159,19 +159,19 @@ func TestGeneratedStructs(t *testing.T) {
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 4)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
t.Run("using null nullable", func(t *testing.T) {
n := v1.Representation()
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 4)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, ipld.Null)
})
t.Run("using null optional nullable", func(t *testing.T) {
......@@ -179,30 +179,30 @@ func TestGeneratedStructs(t *testing.T) {
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 4)
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, ipld.Null)
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
t.Run("using skipped optional", func(t *testing.T) {
n := v3.Representation()
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 3) // note this is shorter, even though it's not at the type level!
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, erp(n.LookupString("f2")), ShouldEqual, ipld.ErrNotExists{ipld.PathSegmentOfString("f2")})
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f3")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("c")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
t.Run("using skipped optional nullable", func(t *testing.T) {
n := v4.Representation()
Wish(t, n.ReprKind(), ShouldEqual, ipld.ReprKind_Map)
Wish(t, n.Length(), ShouldEqual, 3) // note this is shorter, even though it's not at the type level!
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("b")))
Wish(t, plz(n.LookupString("f1")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("a")))
Wish(t, plz(n.LookupString("f2")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("b")))
Wish(t, erp(n.LookupString("f3")), ShouldEqual, ipld.ErrNotExists{ipld.PathSegmentOfString("f3")})
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(String__NodeBuilder{}.CreateString("d")))
Wish(t, plz(n.LookupString("f4")), ShouldEqual, plz(_String__NodeBuilder{}.CreateString("d")))
})
// TODO will need even more cases to probe implicits
})
......@@ -237,7 +237,7 @@ func TestStructUnmarshal(t *testing.T) {
{Type: tok.TString, Str: "f4"}, {Type: tok.TString, Str: "d"},
{Type: tok.TMapClose},
}}
nb := Stroct__NodeBuilder{}
nb := _Stroct__NodeBuilder{}
n, err := encoding.Unmarshal(nb, tb)
// ... asserts ...
})
......
......@@ -10,7 +10,7 @@ func NewGeneratorForKindString(t schema.Type) typedNodeGenerator {
return generateKindString{
t.(schema.TypeString),
generateKindedRejections_String{
string(t.Name()),
mungeTypeNodeIdent(t),
string(t.Name()),
},
}
......@@ -25,17 +25,17 @@ type generateKindString struct {
func (gk generateKindString) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = {{ .Type.Name }}{}
var _ typed.Node = {{ .Type.Name }}{}
var _ ipld.Node = {{ .Type | mungeTypeNodeIdent }}{}
var _ typed.Node = {{ .Type | mungeTypeNodeIdent }}{}
type {{ .Type.Name }} struct{ x string }
type {{ .Type | mungeTypeNodeIdent }} struct{ x string }
`, w, gk)
}
func (gk generateKindString) EmitTypedNodeMethodType(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) Type() schema.Type {
func ({{ .Type | mungeTypeNodeIdent }}) Type() schema.Type {
return nil /*TODO:typelit*/
}
`, w, gk)
......@@ -43,7 +43,7 @@ func (gk generateKindString) EmitTypedNodeMethodType(w io.Writer) {
func (gk generateKindString) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) ReprKind() ipld.ReprKind {
func ({{ .Type | mungeTypeNodeIdent }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_String
}
`, w, gk)
......@@ -51,7 +51,7 @@ func (gk generateKindString) EmitNodeMethodReprKind(w io.Writer) {
func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
doTemplate(`
func (x {{ .Type.Name }}) AsString() (string, error) {
func (x {{ .Type | mungeTypeNodeIdent }}) AsString() (string, error) {
return x.x, nil
}
`, w, gk)
......@@ -59,7 +59,7 @@ func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
func (gk generateKindString) EmitTypedNodeMethodRepresentation(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) Representation() ipld.Node {
func ({{ .Type | mungeTypeNodeIdent }}) Representation() ipld.Node {
panic("TODO representation")
}
`, w, gk)
......
......@@ -10,7 +10,7 @@ func (gk generateKindString) GetNodeBuilderGen() nodebuilderGenerator {
return generateNbKindString{
gk.Type,
genKindedNbRejections_String{
string(gk.Type.Name()) + "__NodeBuilder",
mungeTypeNodebuilderIdent(gk.Type),
string(gk.Type.Name()) + ".Builder",
},
}
......@@ -18,8 +18,8 @@ func (gk generateKindString) GetNodeBuilderGen() nodebuilderGenerator {
func (gk generateKindString) EmitNodeMethodNodeBuilder(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) NodeBuilder() ipld.NodeBuilder {
return {{ .Type.Name }}__NodeBuilder{}
func ({{ .Type | mungeTypeNodeIdent }}) NodeBuilder() ipld.NodeBuilder {
return {{ .Type | mungeTypeNodebuilderIdent }}{}
}
`, w, gk)
}
......@@ -31,14 +31,14 @@ type generateNbKindString struct {
func (gk generateNbKindString) EmitNodebuilderType(w io.Writer) {
doTemplate(`
type {{ .Type.Name }}__NodeBuilder struct{}
type {{ .Type | mungeTypeNodebuilderIdent }} struct{}
`, w, gk)
}
func (gk generateNbKindString) EmitNodebuilderMethodCreateString(w io.Writer) {
doTemplate(`
func (nb {{ .Type.Name }}__NodeBuilder) CreateString(v string) (ipld.Node, error) {
return {{ .Type.Name }}{v}, nil
func (nb {{ .Type | mungeTypeNodebuilderIdent }}) CreateString(v string) (ipld.Node, error) {
return {{ .Type | mungeTypeNodeIdent }}{v}, nil
}
`, w, gk)
}
......@@ -10,7 +10,7 @@ func NewGeneratorForKindStruct(t schema.Type) typedNodeGenerator {
return generateKindStruct{
t.(schema.TypeStruct),
generateKindedRejections_Map{
string(t.Name()),
mungeTypeNodeIdent(t),
string(t.Name()),
},
}
......@@ -27,10 +27,10 @@ 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 }}{}
var _ typed.Node = {{ .Type.Name }}{}
var _ ipld.Node = {{ .Type | mungeTypeNodeIdent }}{}
var _ typed.Node = {{ .Type | mungeTypeNodeIdent }}{}
type {{ .Type.Name }} struct{
type {{ .Type | mungeTypeNodeIdent }} struct{
{{- range $field := .Type.Fields }}
{{ $field.Name }} {{if or $field.IsOptional $field.IsNullable }}*{{end}}{{ $field.Type.Name }}
{{- end}}
......@@ -46,7 +46,7 @@ func (gk generateKindStruct) EmitNodeType(w io.Writer) {
func (gk generateKindStruct) EmitTypedNodeMethodType(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) Type() schema.Type {
func ({{ .Type | mungeTypeNodeIdent }}) Type() schema.Type {
return nil /*TODO:typelit*/
}
`, w, gk)
......@@ -54,7 +54,7 @@ func (gk generateKindStruct) EmitTypedNodeMethodType(w io.Writer) {
func (gk generateKindStruct) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) ReprKind() ipld.ReprKind {
func ({{ .Type | mungeTypeNodeIdent }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Map
}
`, w, gk)
......@@ -62,7 +62,7 @@ func (gk generateKindStruct) EmitNodeMethodReprKind(w io.Writer) {
func (gk generateKindStruct) EmitNodeMethodLookupString(w io.Writer) {
doTemplate(`
func (x {{ .Type.Name }}) LookupString(key string) (ipld.Node, error) {
func (x {{ .Type | mungeTypeNodeIdent }}) LookupString(key string) (ipld.Node, error) {
switch key {
{{- range $field := .Type.Fields }}
case "{{ $field.Name }}":
......@@ -97,7 +97,7 @@ func (gk generateKindStruct) EmitNodeMethodLookupString(w io.Writer) {
func (gk generateKindStruct) EmitNodeMethodLookup(w io.Writer) {
doTemplate(`
func (x {{ .Type.Name }}) Lookup(key ipld.Node) (ipld.Node, error) {
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"}
......@@ -109,12 +109,12 @@ func (gk generateKindStruct) EmitNodeMethodLookup(w io.Writer) {
func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
doTemplate(`
func (x {{ .Type.Name }}) MapIterator() ipld.MapIterator {
func (x {{ .Type | mungeTypeNodeIdent }}) MapIterator() ipld.MapIterator {
return &_{{ .Type.Name }}__itr{&x, 0}
}
type _{{ .Type.Name }}__itr struct {
node *{{ .Type.Name }}
node *{{ .Type | mungeTypeNodeIdent }}
idx int
}
......@@ -163,7 +163,7 @@ func (gk generateKindStruct) EmitNodeMethodMapIterator(w io.Writer) {
func (gk generateKindStruct) EmitNodeMethodLength(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) Length() int {
func ({{ .Type | mungeTypeNodeIdent }}) Length() int {
return {{ len .Type.Fields }}
}
`, w, gk)
......@@ -171,8 +171,8 @@ func (gk generateKindStruct) EmitNodeMethodLength(w io.Writer) {
func (gk generateKindStruct) EmitTypedNodeMethodRepresentation(w io.Writer) {
doTemplate(`
func (n {{ .Type.Name }}) Representation() ipld.Node {
return _{{ .Type.Name }}__Repr{&n}
func (n {{ .Type | mungeTypeNodeIdent }}) Representation() ipld.Node {
return {{ .Type | mungeTypeReprNodeIdent }}{&n}
}
`, w, gk)
}
......
......@@ -10,7 +10,7 @@ func (gk generateKindStruct) GetNodeBuilderGen() nodebuilderGenerator {
return generateNbKindStruct{
gk.Type,
genKindedNbRejections_Map{
string(gk.Type.Name()) + "__NodeBuilder",
mungeTypeNodebuilderIdent(gk.Type),
string(gk.Type.Name()) + ".Builder",
},
}
......@@ -18,8 +18,8 @@ func (gk generateKindStruct) GetNodeBuilderGen() nodebuilderGenerator {
func (gk generateKindStruct) EmitNodeMethodNodeBuilder(w io.Writer) {
doTemplate(`
func ({{ .Type.Name }}) NodeBuilder() ipld.NodeBuilder {
return {{ .Type.Name }}__NodeBuilder{}
func ({{ .Type | mungeTypeNodeIdent }}) NodeBuilder() ipld.NodeBuilder {
return {{ .Type | mungeTypeNodebuilderIdent }}{}
}
`, w, gk)
}
......@@ -31,7 +31,7 @@ type generateNbKindStruct struct {
func (gk generateNbKindStruct) EmitNodebuilderType(w io.Writer) {
doTemplate(`
type {{ .Type.Name }}__NodeBuilder struct{}
type {{ .Type | mungeTypeNodebuilderIdent }} struct{}
`, w, gk)
}
......@@ -51,8 +51,8 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodCreateMap(w io.Writer) {
// because it gets wedged in with other logic tables around optionality.
// REVIEW: 'x, ok := v.({{ $field.Type.Name }})' might need some stars in it... sometimes.
doTemplate(`
func (nb {{ .Type.Name }}__NodeBuilder) CreateMap() (ipld.MapBuilder, error) {
return &{{ .Type.Name }}__MapBuilder{v:&{{ .Type.Name }}{}}, nil
func (nb {{ .Type | mungeTypeNodebuilderIdent }}) CreateMap() (ipld.MapBuilder, error) {
return &{{ .Type.Name }}__MapBuilder{v:&{{ .Type | mungeTypeNodeIdent }}{}}, nil
}
type {{ .Type.Name }}__MapBuilder struct{
......@@ -135,7 +135,7 @@ func (gk generateNbKindStruct) EmitNodebuilderMethodCreateMap(w io.Writer) {
func (gk generateNbKindStruct) EmitNodebuilderMethodAmendMap(w io.Writer) {
doTemplate(`
func (nb {{ .Type.Name }}__NodeBuilder) AmendMap() (ipld.MapBuilder, error) {
func (nb {{ .Type | mungeTypeNodebuilderIdent }}) AmendMap() (ipld.MapBuilder, error) {
panic("TODO later")
}
`, w, gk)
......
......@@ -10,7 +10,7 @@ func getStructRepresentationMapNodeGen(t schema.TypeStruct) nodeGenerator {
return generateStructReprMapNode{
t,
generateKindedRejections_Map{
"_" + string(t.Name()) + "__Repr",
mungeTypeReprNodeIdent(t),
string(t.Name()) + ".Representation",
},
}
......@@ -23,9 +23,9 @@ type generateStructReprMapNode struct {
func (gk generateStructReprMapNode) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = _{{ .Type.Name }}__Repr{}
var _ ipld.Node = {{ .Type | mungeTypeReprNodeIdent }}{}
type _{{ .Type.Name }}__Repr struct{
type {{ .Type | mungeTypeReprNodeIdent }} struct{
n *{{ .Type.Name }}
}
......@@ -34,7 +34,7 @@ func (gk generateStructReprMapNode) EmitNodeType(w io.Writer) {
func (gk generateStructReprMapNode) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func (_{{ .Type.Name }}__Repr) ReprKind() ipld.ReprKind {
func ({{ .Type | mungeTypeReprNodeIdent }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_Map
}
`, w, gk)
......@@ -44,7 +44,7 @@ func (gk generateStructReprMapNode) EmitNodeMethodLookupString(w io.Writer) {
// almost idential to the type-level one, just with different strings in the switch.
// TODO : support for implicits is missing.
doTemplate(`
func (rn _{{ .Type.Name }}__Repr) LookupString(key string) (ipld.Node, error) {
func (rn {{ .Type | mungeTypeReprNodeIdent }}) LookupString(key string) (ipld.Node, error) {
switch key {
{{- $type := .Type -}} {{- /* ranging modifies dot, unhelpfully */ -}}
{{- range $field := .Type.Fields }}
......@@ -80,7 +80,7 @@ func (gk generateStructReprMapNode) EmitNodeMethodLookupString(w io.Writer) {
func (gk generateStructReprMapNode) EmitNodeMethodLookup(w io.Writer) {
doTemplate(`
func (rn _{{ .Type.Name }}__Repr) Lookup(key ipld.Node) (ipld.Node, error) {
func (rn {{ .Type | mungeTypeReprNodeIdent }}) Lookup(key ipld.Node) (ipld.Node, error) {
ks, err := key.AsString()
if err != nil {
return nil, ipld.ErrInvalidKey{"got " + key.ReprKind().String() + ", need string"}
......@@ -96,16 +96,16 @@ func (gk generateStructReprMapNode) EmitNodeMethodMapIterator(w io.Writer) {
// use 'continue' statements to skip past optionals which are undefined.
// TODO : support for implicits is missing.
doTemplate(`
func (rn _{{ .Type.Name }}__Repr) MapIterator() ipld.MapIterator {
func (rn {{ .Type | mungeTypeReprNodeIdent }}) MapIterator() ipld.MapIterator {
return &_{{ .Type.Name }}__itr{rn.n, 0}
}
type _{{ .Type.Name }}__ReprItr struct {
type {{ .Type | mungeTypeReprNodeIdent }}Itr struct {
node *{{ .Type.Name }}
idx int
}
func (itr *_{{ .Type.Name }}__ReprItr) Next() (k ipld.Node, v ipld.Node, _ error) {
func (itr *{{ .Type | mungeTypeReprNodeIdent }}Itr) 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.Name }}__ReprItr) Done() bool {
func (itr *{{ .Type | mungeTypeReprNodeIdent }}Itr) Done() bool {
return itr.idx >= {{ len .Type.Fields }}
}
......@@ -156,7 +156,7 @@ func (gk generateStructReprMapNode) EmitNodeMethodLength(w io.Writer) {
// This is fun: it has to count down for any unset optional fields.
// TODO : support for implicits is missing.
doTemplate(`
func (rn _{{ .Type.Name }}__Repr) Length() int {
func (rn {{ .Type | mungeTypeReprNodeIdent }}) Length() int {
l := {{ len .Type.Fields }}
{{- range $field := .Type.Fields }}
{{- if and $field.IsOptional $field.IsNullable }}
......@@ -176,8 +176,8 @@ func (gk generateStructReprMapNode) EmitNodeMethodLength(w io.Writer) {
func (gk generateStructReprMapNode) EmitNodeMethodNodeBuilder(w io.Writer) {
doTemplate(`
func (_{{ .Type.Name }}__Repr) NodeBuilder() ipld.NodeBuilder {
return _{{ .Type.Name }}__ReprBuilder{}
func ({{ .Type | mungeTypeReprNodeIdent }}) NodeBuilder() ipld.NodeBuilder {
return {{ .Type | mungeTypeReprNodebuilderIdent }}{}
}
`, w, gk)
}
......@@ -203,7 +203,7 @@ func (gk generateStructReprMapNb) EmitNodebuilderType(w io.Writer) {
// (We *could* munge the reprkind in for debug symbol reading,
// but at present it hasn't seemed warranted.)
doTemplate(`
type _{{ .Type.Name }}__ReprBuilder struct{}
type {{ .Type | mungeTypeReprNodebuilderIdent }} struct{}
`, w, gk)
}
......@@ -220,7 +220,7 @@ func (gk generateStructReprMapNb) EmitNodebuilderMethodCreateMap(w io.Writer) {
// yielded a huge matrix of concerns and no single clear gradient.
// TODO : support for implicits is missing.
doTemplate(`
func (nb _{{ .Type.Name }}__ReprBuilder) CreateMap() (ipld.MapBuilder, error) {
func (nb {{ .Type | mungeTypeReprNodebuilderIdent }}) CreateMap() (ipld.MapBuilder, error) {
return &_{{ .Type.Name }}__ReprMapBuilder{v:&{{ .Type.Name }}{}}, nil
}
......@@ -304,7 +304,7 @@ func (gk generateStructReprMapNb) EmitNodebuilderMethodCreateMap(w io.Writer) {
func (gk generateStructReprMapNb) EmitNodebuilderMethodAmendMap(w io.Writer) {
doTemplate(`
func (nb _{{ .Type.Name }}__ReprBuilder) AmendMap() (ipld.MapBuilder, error) {
func (nb {{ .Type | mungeTypeReprNodebuilderIdent }}) AmendMap() (ipld.MapBuilder, error) {
panic("TODO later")
}
`, w, gk)
......
package gengo
import (
"github.com/ipld/go-ipld-prime/schema"
)
func mungeTypeNodeIdent(t schema.Type) string {
return string(t.Name())
}
// 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 mungeTypeNodebuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__NodeBuilder"
}
func mungeTypeReprNodeIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__Repr"
}
func mungeTypeReprNodebuilderIdent(t schema.Type) string {
return "_" + string(t.Name()) + "__ReprBuilder"
}
// itr
// MapBuilder
// ListBuilder
// reprItr
// ReprMapBuilder
// ReprListBuilder
// 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.
......@@ -21,6 +21,11 @@ func doTemplate(tmplstr string, w io.Writer, data interface{}) {
"Add": func(a, b int) int {
return a + b
},
"mungeTypeNodeIdent": mungeTypeNodeIdent,
"mungeTypeNodebuilderIdent": mungeTypeNodebuilderIdent,
"mungeTypeReprNodeIdent": mungeTypeReprNodeIdent,
"mungeTypeReprNodebuilderIdent": mungeTypeReprNodebuilderIdent,
}).
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