Ran go fmt
This commit is contained in:
parent
f637b196fe
commit
8573ee37ad
@ -1,6 +1,5 @@
|
||||
package hatmill
|
||||
|
||||
|
||||
//go:generate go run internal/codegen/codegen.go -input defs.json -elemfile element/generated.go -elempkg element -attribfile attribute/generated.go -attribpkg attribute
|
||||
import "io"
|
||||
|
||||
|
@ -179,9 +179,9 @@ func TestParentElement(t *testing.T) {
|
||||
}
|
||||
|
||||
func Example() {
|
||||
userInput := "<script>launchMissiles();</script>"
|
||||
userInput := "<script>launchMissiles();</script>"
|
||||
|
||||
document := he.Html()(
|
||||
document := he.Html()(
|
||||
he.Head()(
|
||||
he.Meta(ha.HttpEquiv("refresh"), ha.Content("5")),
|
||||
),
|
||||
@ -194,5 +194,5 @@ func Example() {
|
||||
),
|
||||
)
|
||||
hatmill.WriteDocument(os.Stdout, document)
|
||||
// Output: <!DOCTYPE html><html><head><meta http-equiv='refresh' content='5'></head><body><div><img src='./me.jpg' id='profile-photo'><script>launchMissiles();</script><div disabled data-coolness='awesome'></div></div></body></html>
|
||||
// Output: <!DOCTYPE html><html><head><meta http-equiv='refresh' content='5'></head><body><div><img src='./me.jpg' id='profile-photo'><script>launchMissiles();</script><div disabled data-coolness='awesome'></div></div></body></html>
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
@ -28,44 +28,44 @@ func identifier(s string) string {
|
||||
}
|
||||
|
||||
func fileHeader(packageName string) string {
|
||||
return fmt.Sprintf(headerFmt, packageName)
|
||||
return fmt.Sprintf(headerFmt, packageName)
|
||||
}
|
||||
|
||||
type AttribType int
|
||||
|
||||
const (
|
||||
String AttribType = iota
|
||||
Bool
|
||||
String AttribType = iota
|
||||
Bool
|
||||
)
|
||||
|
||||
func (t *AttribType) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == "null" {
|
||||
return nil
|
||||
}
|
||||
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
|
||||
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"`
|
||||
Name string `json:"name"`
|
||||
Type AttribType `json:"type"`
|
||||
}
|
||||
|
||||
func (def AttribDef) Generate() string {
|
||||
const (
|
||||
const (
|
||||
boolType = "bool"
|
||||
stringType = "string"
|
||||
|
||||
@ -86,27 +86,27 @@ func (def AttribDef) Generate() string {
|
||||
`
|
||||
)
|
||||
|
||||
var template string
|
||||
switch def.Type {
|
||||
case Bool:
|
||||
template = boolTemplate
|
||||
case String:
|
||||
template = stringTemplate
|
||||
default:
|
||||
panic(fmt.Errorf("unknown attribute type: %v", def.Type))
|
||||
}
|
||||
var template string
|
||||
switch def.Type {
|
||||
case Bool:
|
||||
template = boolTemplate
|
||||
case String:
|
||||
template = stringTemplate
|
||||
default:
|
||||
panic(fmt.Errorf("unknown attribute type: %v", def.Type))
|
||||
}
|
||||
|
||||
return fmt.Sprintf(template, identifier(def.Name), def.Name)
|
||||
return fmt.Sprintf(template, identifier(def.Name), def.Name)
|
||||
}
|
||||
|
||||
type ElemDef struct {
|
||||
Name string `json:"name"`
|
||||
Void bool `json:"void"`
|
||||
Name string `json:"name"`
|
||||
Void bool `json:"void"`
|
||||
}
|
||||
|
||||
func (def ElemDef) Generate() string {
|
||||
const (
|
||||
parentTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
const (
|
||||
parentTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return func(children ...hatmill.Term) hatmill.ParentElement {
|
||||
return hatmill.ParentElement{
|
||||
@ -119,7 +119,7 @@ func (def ElemDef) Generate() string {
|
||||
}
|
||||
}
|
||||
`
|
||||
voidTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
voidTemplate = `// %[1]s creates a <%[2]s> element.
|
||||
func %[1]s(attribs ...hatmill.Attrib) hatmill.VoidElement {
|
||||
return hatmill.VoidElement{
|
||||
TagName: "%[2]s",
|
||||
@ -127,18 +127,18 @@ func (def ElemDef) Generate() string {
|
||||
}
|
||||
}
|
||||
`
|
||||
)
|
||||
)
|
||||
|
||||
template := parentTemplate
|
||||
if def.Void {
|
||||
template = voidTemplate
|
||||
}
|
||||
return fmt.Sprintf(template, identifier(def.Name), def.Name)
|
||||
template := parentTemplate
|
||||
if def.Void {
|
||||
template = voidTemplate
|
||||
}
|
||||
return fmt.Sprintf(template, identifier(def.Name), def.Name)
|
||||
}
|
||||
|
||||
type Defs struct {
|
||||
Attributes []AttribDef `json:"attributes"`
|
||||
Elements []ElemDef `json:"elements"`
|
||||
Attributes []AttribDef `json:"attributes"`
|
||||
Elements []ElemDef `json:"elements"`
|
||||
}
|
||||
|
||||
func writeFormatted(fileName, packageName string, writeDefs func(*bytes.Buffer)) error {
|
||||
@ -153,28 +153,28 @@ func writeFormatted(fileName, packageName string, writeDefs func(*bytes.Buffer))
|
||||
}
|
||||
|
||||
func main() {
|
||||
inputPath := flag.String("input", "", "JSON input file")
|
||||
inputPath := flag.String("input", "", "JSON input file")
|
||||
elemPath := flag.String("elemfile", "", "generated element .go file")
|
||||
elemPkg := flag.String("elempkg", "", "generated element package name")
|
||||
attribPath := flag.String("attribfile", "", "generated attribute .go file")
|
||||
attribPkg := flag.String("attribpkg", "", "generated attribute package name")
|
||||
flag.Parse()
|
||||
flag.Parse()
|
||||
|
||||
input, err := ioutil.ReadFile(*inputPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
var defs Defs
|
||||
err = json.Unmarshal(input, &defs)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = writeFormatted(*attribPath, *attribPkg, func(buf *bytes.Buffer) {
|
||||
for _, attribDef := range defs.Attributes {
|
||||
buf.WriteString(attribDef.Generate())
|
||||
}
|
||||
buf.WriteString(attribDef.Generate())
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -182,8 +182,8 @@ func main() {
|
||||
|
||||
err = writeFormatted(*elemPath, *elemPkg, func(buf *bytes.Buffer) {
|
||||
for _, elemDef := range defs.Elements {
|
||||
buf.WriteString(elemDef.Generate())
|
||||
}
|
||||
buf.WriteString(elemDef.Generate())
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
Loading…
Reference in New Issue
Block a user