Use unified JSON file for codegen input
This commit is contained in:
@ -1,21 +1,23 @@
|
||||
# codegen
|
||||
|
||||
codegen reads simple descriptions of HTML elements and attributes from
|
||||
`elements.txt` and `attribs.txt`, respectively, and generates Go functions to
|
||||
create them.
|
||||
codegen reads JSON descriptions of HTML elements and attributes, and generates
|
||||
Go functions to create them.
|
||||
|
||||
The input file consists of a JSON object like the following, which includes all
|
||||
options for attribute types and element emptiness:
|
||||
|
||||
The files contains one description per line. Each attribute description is in
|
||||
the form
|
||||
```
|
||||
attribute-name attribute-type
|
||||
{
|
||||
"elements": [
|
||||
{"name": "div"},
|
||||
{"name": "img", "empty": true},
|
||||
],
|
||||
"attributes": [
|
||||
{"name": "src", "type": "string"},
|
||||
{"name": "disabled", "type": "bool"}
|
||||
]
|
||||
}
|
||||
```
|
||||
where `attribute-name` is the lowercase name of the attribute, and
|
||||
`attribute-type` is either `string` for normal attributes (e.g. `id`, 'style')
|
||||
or `bool` for boolean attributes (e.g. `disabled`). Each element description is
|
||||
in the form
|
||||
```
|
||||
tag-name element-type
|
||||
```
|
||||
where `tag-name` is the lowercase name of the element, and `element-type` is
|
||||
either `empty` for empty elements (e.g. `img`, 'br') or `parent` for elements
|
||||
that can have child nodes (e.g. 'div', 'body').
|
||||
|
||||
The `"empty"` key can be omitted from an element definition and defaults to
|
||||
`false`.
|
||||
|
@ -1,104 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const packageName = "hatmill"
|
||||
const headerFmt = `// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/codegen
|
||||
// DO NOT EDIT!
|
||||
|
||||
func genElements() {
|
||||
const (
|
||||
elementInputPath = "elements.txt"
|
||||
elementOutputPath = "elements.go"
|
||||
package %s
|
||||
`
|
||||
|
||||
parentType = "parent"
|
||||
emptyType = "empty"
|
||||
|
||||
parentTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...Attrib) func(children ...Term) *ParentElement {
|
||||
return func(children ...Term) *ParentElement {
|
||||
return &ParentElement{
|
||||
EmptyElement: EmptyElement{
|
||||
TagName: "%[2]s",
|
||||
Attribs: attribs,
|
||||
},
|
||||
Children: children,
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
emptyTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...Attrib) EmptyElement {
|
||||
return EmptyElement{
|
||||
TagName: "%[2]s",
|
||||
Attribs: attribs,
|
||||
}
|
||||
}
|
||||
`
|
||||
)
|
||||
inputFile, err := os.Open(elementInputPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer inputFile.Close()
|
||||
|
||||
var output bytes.Buffer
|
||||
|
||||
fmt.Fprintln(&output, "// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/elementgen")
|
||||
fmt.Fprintln(&output, "// DO NOT EDIT!\n")
|
||||
fmt.Fprintln(&output, "package ", packageName, "\n")
|
||||
|
||||
scanner := bufio.NewScanner(inputFile)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
var tagName, elemType string
|
||||
_, err = fmt.Sscanf(line, "%s %s", &tagName, &elemType)
|
||||
if err != nil {
|
||||
log.Fatalf("error parsing input line: %s", err)
|
||||
}
|
||||
|
||||
var template string
|
||||
switch elemType {
|
||||
case parentType:
|
||||
template = parentTemplate
|
||||
case emptyType:
|
||||
template = emptyTemplate
|
||||
default:
|
||||
log.Fatal("unknown element type: ", elemType)
|
||||
}
|
||||
fmt.Fprintf(&output, template, strings.Title(tagName), tagName)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatalf("error scanning input: %s", err)
|
||||
}
|
||||
|
||||
formatted, err := format.Source(output.Bytes())
|
||||
if err != nil {
|
||||
log.Fatalf("error formatting output: %s", err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(elementOutputPath, formatted, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("error writing output: %s", err)
|
||||
}
|
||||
func fileHeader(packageName string, needImport bool) string {
|
||||
header := fmt.Sprintf(headerFmt, packageName)
|
||||
if needImport {
|
||||
header += `import . "gitlab.codemonkeysoftware.net/b/hatmill"` + "\n"
|
||||
}
|
||||
return header
|
||||
}
|
||||
|
||||
func genAttribs() {
|
||||
const (
|
||||
inputPath = "attribs.txt"
|
||||
outputPath = "attribs.go"
|
||||
type AttribType int
|
||||
|
||||
const (
|
||||
String AttribType = iota
|
||||
Bool
|
||||
)
|
||||
|
||||
func (t *AttribType) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var typeName string
|
||||
err := json.Unmarshal(data, &typeName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("type property must be a string")
|
||||
}
|
||||
switch typeName {
|
||||
case "bool":
|
||||
*t = Bool
|
||||
case "string":
|
||||
*t = String
|
||||
default:
|
||||
return fmt.Errorf("unrecognized attribute type %s", typeName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttribDef struct {
|
||||
Name string `json:"name"`
|
||||
Type AttribType `json:"type"`
|
||||
}
|
||||
|
||||
func (def AttribDef) String() string {
|
||||
const (
|
||||
boolType = "bool"
|
||||
stringType = "string"
|
||||
|
||||
@ -117,58 +78,95 @@ func genAttribs() {
|
||||
`
|
||||
)
|
||||
|
||||
inputFile, err := os.Open(inputPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer inputFile.Close()
|
||||
var template string
|
||||
switch def.Type {
|
||||
case Bool:
|
||||
template = boolTemplate
|
||||
case String:
|
||||
template = stringTemplate
|
||||
default:
|
||||
panic(fmt.Errorf("unknown attribute type: %s", def.Type))
|
||||
}
|
||||
|
||||
var output bytes.Buffer
|
||||
return fmt.Sprintf(template, strings.Title(def.Name), def.Name)
|
||||
}
|
||||
|
||||
fmt.Fprintln(&output, "// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/internal/attribgen")
|
||||
fmt.Fprintln(&output, "// DO NOT EDIT!\n")
|
||||
fmt.Fprintln(&output, "package ", packageName, "\n")
|
||||
type ElemDef struct {
|
||||
Name string `json:"name"`
|
||||
Empty bool `json:"empty"`
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(inputFile)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
func (def ElemDef) String() string {
|
||||
const (
|
||||
parentTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...Attrib) func(children ...Term) *ParentElement {
|
||||
return func(children ...Term) *ParentElement {
|
||||
return &ParentElement{
|
||||
EmptyElement: EmptyElement{
|
||||
TagName: "%[2]s",
|
||||
Attribs: attribs,
|
||||
},
|
||||
Children: children,
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
emptyTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...Attrib) EmptyElement {
|
||||
return EmptyElement{
|
||||
TagName: "%[2]s",
|
||||
Attribs: attribs,
|
||||
}
|
||||
}
|
||||
`
|
||||
)
|
||||
|
||||
var attribName, attribType string
|
||||
_, err = fmt.Sscanf(line, "%s %s", &attribName, &attribType)
|
||||
if err != nil {
|
||||
log.Fatalf("error parsing input line: %s", err)
|
||||
}
|
||||
template := parentTemplate
|
||||
if def.Empty {
|
||||
template = emptyTemplate
|
||||
}
|
||||
return fmt.Sprintf(template, strings.Title(def.Name), def.Name)
|
||||
}
|
||||
|
||||
var template string
|
||||
switch attribType {
|
||||
case boolType:
|
||||
template = boolTemplate
|
||||
case stringType:
|
||||
template = stringTemplate
|
||||
default:
|
||||
log.Fatal("unknown attribute type: ", attribType)
|
||||
}
|
||||
fmt.Fprintf(&output, template, strings.Title(attribName), attribName)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatalf("error scanning input: %s", err)
|
||||
}
|
||||
|
||||
formatted, err := format.Source(output.Bytes())
|
||||
if err != nil {
|
||||
log.Fatalf("error formatting output: %s", err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(outputPath, formatted, 0644)
|
||||
if err != nil {
|
||||
log.Fatalf("error writing output: %s", err)
|
||||
}
|
||||
type Defs struct {
|
||||
Attributes []AttribDef `json:"attributes"`
|
||||
Elements []ElemDef `json:"elements"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
genElements()
|
||||
genAttribs()
|
||||
inputPath := flag.String("input", "", "JSON input file")
|
||||
outputPath := flag.String("output", "", ".go output file")
|
||||
packageName := flag.String("package", "", "output package name")
|
||||
addImport := flag.Bool("import", false, "import hatmill in output package")
|
||||
flag.Parse()
|
||||
|
||||
input, err := ioutil.ReadFile(*inputPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var defs Defs
|
||||
err = json.Unmarshal(input, &defs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
output := new(bytes.Buffer)
|
||||
output.WriteString(fileHeader(*packageName, *addImport))
|
||||
for _, elemDef := range defs.Elements {
|
||||
output.WriteString(elemDef.String())
|
||||
}
|
||||
for _, attribDef := range defs.Attributes {
|
||||
output.WriteString(attribDef.String())
|
||||
}
|
||||
|
||||
formatted, err := format.Source(output.Bytes())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(*outputPath, formatted, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user