Commit f6e469ce authored by Eric Myhre's avatar Eric Myhre

Ok, I have learned a hard lesson about embedding.

Fields in these structs are now named.  Embedding is reserved for
things that are very explicitly supposed to carry their methods along.

At some earlier point, I embedded the Type field in some of the
generate structs because it saved me a few keystrokes in refactoring
template strings.  This was not important.

Today I learned: if you have embeddings which cause a name to be
shadowed -- as the generateKindedRejections_String.Type field
could shadow the generateKindString.Type field, for example -- if those
resolve "neutrally" (I don't know if there's a better technical term
for this), then... in that case, the templating system will be like
"yeah, fine, whatev", and it works.  If you give one of those two
fields a *separate type*, the templating system will now reject it as
if no field of that name can be found.

I encountered this while working on the generator for structs (which I
*swear* I will commit any second now, when I stop finding prerequisite
yaks to shave -- there have been many) and trying to use a more
specialized TypeStruct instead of the Type interface.  Whee.

I get the logic of this.  The error message could be better.

Anyway: even though it technically didn't affect generateString (yet),
making the change here for consistency with what's to come
(including specializing the field even though there's currently no
actual need for it).
parent 83bbe5e1
......@@ -106,7 +106,7 @@ func (generateKindedRejections) emitNodeMethodAsLink(w io.Writer, t schema.Type)
// Embeddable to do all the "nope" methods at once.
type generateKindedRejections_String struct {
schema.Type // used so we can generate error messages with the type name.
Type schema.Type // used so we can generate error messages with the type name.
}
func (gk generateKindedRejections_String) EmitNodeMethodTraverseField(w io.Writer) {
......
......@@ -8,13 +8,13 @@ import (
func NewGeneratorForKindString(t schema.Type) typeGenerator {
return generateKindString{
t,
t.(schema.TypeString),
generateKindedRejections_String{t},
}
}
type generateKindString struct {
schema.Type
Type schema.TypeString
generateKindedRejections_String
// 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.
......@@ -22,7 +22,7 @@ type generateKindString struct {
func (gk generateKindString) EmitNodeMethodAsString(w io.Writer) {
doTemplate(`
func (x {{ .Name }}) AsString() (string, error) {
func (x {{ .Type.Name }}) AsString() (string, error) {
return x.x, nil
}
`, w, gk)
......
......@@ -6,16 +6,16 @@ import (
func (gk generateKindString) EmitNodeType(w io.Writer) {
doTemplate(`
var _ ipld.Node = {{ .Name }}{}
var _ ipld.Node = {{ .Type.Name }}{}
type {{ .Name }} struct{ x string }
type {{ .Type.Name }} struct{ x string }
`, w, gk)
}
func (gk generateKindString) EmitNodeMethodReprKind(w io.Writer) {
doTemplate(`
func ({{ .Name }}) ReprKind() ipld.ReprKind {
func ({{ .Type.Name }}) ReprKind() ipld.ReprKind {
return ipld.ReprKind_String
}
`, w, gk)
......@@ -24,43 +24,43 @@ func (gk generateKindString) EmitNodeMethodReprKind(w io.Writer) {
// FUTURE: consider breaking the nodebuilder methods down like the node methods are; a lot of the "nope" variants could be reused.
func (gk generateKindString) EmitNodeMethodNodeBuilder(w io.Writer) {
doTemplate(`
func ({{ .Name }}) NodeBuilder() ipld.NodeBuilder {
return {{ .Name }}__NodeBuilder{}
func ({{ .Type.Name }}) NodeBuilder() ipld.NodeBuilder {
return {{ .Type.Name }}__NodeBuilder{}
}
type {{ .Name }}__NodeBuilder struct{}
type {{ .Type.Name }}__NodeBuilder struct{}
func (nb {{ .Name }}__NodeBuilder) CreateMap() (ipld.MapBuilder, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateMap() (ipld.MapBuilder, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateMap", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) AmendMap() (ipld.MapBuilder, error) {
func (nb {{ .Type.Name }}__NodeBuilder) AmendMap() (ipld.MapBuilder, error) {
return nil, ipld.ErrWrongKind{MethodName: "AmendMap", AppropriateKind: ipld.ReprKindSet_JustMap, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) CreateList() (ipld.ListBuilder, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateList() (ipld.ListBuilder, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateList", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) AmendList() (ipld.ListBuilder, error) {
func (nb {{ .Type.Name }}__NodeBuilder) AmendList() (ipld.ListBuilder, error) {
return nil, ipld.ErrWrongKind{MethodName: "AmendList", AppropriateKind: ipld.ReprKindSet_JustList, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) CreateNull() (ipld.Node, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateNull() (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateNull", AppropriateKind: ipld.ReprKindSet_JustNull, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) CreateBool(v bool) (ipld.Node, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateBool(v bool) (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateBool", AppropriateKind: ipld.ReprKindSet_JustBool, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) CreateInt(v int) (ipld.Node, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateInt(v int) (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateInt", AppropriateKind: ipld.ReprKindSet_JustInt, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) CreateFloat(v float64) (ipld.Node, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateFloat(v float64) (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateFloat", AppropriateKind: ipld.ReprKindSet_JustFloat, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) CreateString(v string) (ipld.Node, error) {
return {{ .Name }}{v}, nil
func (nb {{ .Type.Name }}__NodeBuilder) CreateString(v string) (ipld.Node, error) {
return {{ .Type.Name }}{v}, nil
}
func (nb {{ .Name }}__NodeBuilder) CreateBytes(v []byte) (ipld.Node, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateBytes(v []byte) (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateBytes", AppropriateKind: ipld.ReprKindSet_JustBytes, ActualKind: ipld.ReprKind_String}
}
func (nb {{ .Name }}__NodeBuilder) CreateLink(v ipld.Link) (ipld.Node, error) {
func (nb {{ .Type.Name }}__NodeBuilder) CreateLink(v ipld.Link) (ipld.Node, error) {
return nil, ipld.ErrWrongKind{MethodName: "CreateLink", AppropriateKind: ipld.ReprKindSet_JustLink, ActualKind: ipld.ReprKind_String}
}
`, w, gk)
......
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