Allow custom function comments in specs

This commit is contained in:
2019-05-14 21:15:18 -06:00
parent e6b8f04dcf
commit f80148fdf6
5 changed files with 35 additions and 12 deletions

View File

@ -19,6 +19,10 @@ func identifier(s string) string {
return strings.Join(words, "")
}
func formatComment(s string) string {
return "// " + strings.TrimSpace(s) + "\n"
}
type AttribTypeInfo struct {
Template string
Imports []string
@ -70,24 +74,31 @@ type Spec interface {
}
type AttribSpec struct {
Name string `json:"name"`
Type string `json:"type"`
Name string `json:"name"`
Type string `json:"type"`
Comment string `json:"comment"`
}
func (spec AttribSpec) Generate() Def {
name := spec.Name
ident := identifier(spec.Name)
comment := fmt.Sprintf("// %s creates a \"%s\" attribute\n", ident, name)
var comment string
if spec.Comment != "" {
comment = spec.Comment
} else {
comment = fmt.Sprintf("%s creates a \"%s\" attribute", ident, name)
}
template := attribTypes[spec.Type].Template
return Def{
Source: comment + fmt.Sprintf(template, ident, name),
Source: formatComment(comment) + fmt.Sprintf(template, ident, name),
Imports: attribTypes[spec.Type].Imports,
}
}
type ElemSpec struct {
Name string `json:"name"`
Void bool `json:"void"`
Name string `json:"name"`
Void bool `json:"void"`
Comment string `json:"comment"`
}
func (spec ElemSpec) Generate() Def {
@ -115,13 +126,18 @@ func (spec ElemSpec) Generate() Def {
name := spec.Name
ident := identifier(spec.Name)
comment := fmt.Sprintf("// %s creates a <%s> element.\n", ident, name)
var comment string
if spec.Comment != "" {
comment = spec.Comment
} else {
comment = fmt.Sprintf("%s creates a <%s> element.", ident, name)
}
template := parentTemplate
if spec.Void {
template = voidTemplate
}
return Def{
Source: comment + fmt.Sprintf(template, ident, name),
Source: formatComment(comment) + fmt.Sprintf(template, ident, name),
}
}