Commit 792f8937 authored by Eric Myhre's avatar Eric Myhre

Generate reasonable ReprKind method for kinded unions.

parent 8f18f5ba
...@@ -70,7 +70,26 @@ func (g unionReprKindedReprGenerator) EmitNodeTypeAssertions(w io.Writer) { ...@@ -70,7 +70,26 @@ func (g unionReprKindedReprGenerator) EmitNodeTypeAssertions(w io.Writer) {
} }
func (g unionReprKindedReprGenerator) EmitNodeMethodReprKind(w io.Writer) { func (g unionReprKindedReprGenerator) EmitNodeMethodReprKind(w io.Writer) {
// FIXME wow doTemplate(`
func (n *_{{ .Type | TypeSymbol }}__Repr) ReprKind() ipld.ReprKind {
{{- if (eq (.AdjCfg.UnionMemlayout .Type) "embedAll") }}
switch n.tag {
{{- range $i, $member := .Type.Members }}
case {{ add $i 1 }}:
return {{ $member.RepresentationBehavior | KindSymbol }}
{{- end}}
{{- else if (eq (.AdjCfg.UnionMemlayout .Type) "interface") }}
switch n2 := n.x.(type) {
{{- range $i, $member := .Type.Members }}
case {{ $member | TypeSymbol }}:
return {{ $member.RepresentationBehavior | KindSymbol }}
{{- end}}
{{- end}}
default:
panic("unreachable")
}
}
`, w, g.AdjCfg, g)
} }
// A bunch of these methods could be improved by doing a gen-time switch for whether any of the possible members are the relevant kind at all; // A bunch of these methods could be improved by doing a gen-time switch for whether any of the possible members are the relevant kind at all;
......
...@@ -54,6 +54,30 @@ func doTemplate(tmplstr string, w io.Writer, adjCfg *AdjunctCfg, data interface{ ...@@ -54,6 +54,30 @@ func doTemplate(tmplstr string, w io.Writer, adjCfg *AdjunctCfg, data interface{
panic("invalid enumeration value!") panic("invalid enumeration value!")
} }
}, },
"KindSymbol": func(k ipld.ReprKind) string {
switch k {
case ipld.ReprKind_Map:
return "ipld.ReprKind_Map"
case ipld.ReprKind_List:
return "ipld.ReprKind_List"
case ipld.ReprKind_Null:
return "ipld.ReprKind_Null"
case ipld.ReprKind_Bool:
return "ipld.ReprKind_Bool"
case ipld.ReprKind_Int:
return "ipld.ReprKind_Int"
case ipld.ReprKind_Float:
return "ipld.ReprKind_Float"
case ipld.ReprKind_String:
return "ipld.ReprKind_String"
case ipld.ReprKind_Bytes:
return "ipld.ReprKind_Bytes"
case ipld.ReprKind_Link:
return "ipld.ReprKind_Link"
default:
panic("invalid enumeration value!")
}
},
"add": func(a, b int) int { return a + b }, "add": func(a, b int) int { return a + b },
"title": func(s string) string { return strings.Title(s) }, "title": func(s string) string { return strings.Title(s) },
}). }).
......
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