Ran go fmt

This commit is contained in:
Brandon Dyck 2019-04-08 22:10:02 -06:00
parent f637b196fe
commit 8573ee37ad
3 changed files with 66 additions and 67 deletions

View File

@ -1,6 +1,5 @@
package hatmill 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 //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" import "io"

View File

@ -179,9 +179,9 @@ func TestParentElement(t *testing.T) {
} }
func Example() { func Example() {
userInput := "<script>launchMissiles();</script>" userInput := "<script>launchMissiles();</script>"
document := he.Html()( document := he.Html()(
he.Head()( he.Head()(
he.Meta(ha.HttpEquiv("refresh"), ha.Content("5")), he.Meta(ha.HttpEquiv("refresh"), ha.Content("5")),
), ),
@ -194,5 +194,5 @@ func Example() {
), ),
) )
hatmill.WriteDocument(os.Stdout, document) 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'>&lt;script&gt;launchMissiles();&lt;/script&gt;<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'>&lt;script&gt;launchMissiles();&lt;/script&gt;<div disabled data-coolness='awesome'></div></div></body></html>
} }

View File

@ -2,8 +2,8 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"go/format" "go/format"
"io/ioutil" "io/ioutil"
@ -28,44 +28,44 @@ func identifier(s string) string {
} }
func fileHeader(packageName string) string { func fileHeader(packageName string) string {
return fmt.Sprintf(headerFmt, packageName) return fmt.Sprintf(headerFmt, packageName)
} }
type AttribType int type AttribType int
const ( const (
String AttribType = iota String AttribType = iota
Bool Bool
) )
func (t *AttribType) UnmarshalJSON(data []byte) error { func (t *AttribType) UnmarshalJSON(data []byte) error {
if string(data) == "null" { if string(data) == "null" {
return nil return nil
} }
var typeName string var typeName string
err := json.Unmarshal(data, &typeName) err := json.Unmarshal(data, &typeName)
if err != nil { if err != nil {
return fmt.Errorf("type property must be a string") return fmt.Errorf("type property must be a string")
} }
switch typeName { switch typeName {
case "bool": case "bool":
*t = Bool *t = Bool
case "string": case "string":
*t = String *t = String
default: default:
return fmt.Errorf("unrecognized attribute type %s", typeName) return fmt.Errorf("unrecognized attribute type %s", typeName)
} }
return nil return nil
} }
type AttribDef struct { type AttribDef struct {
Name string `json:"name"` Name string `json:"name"`
Type AttribType `json:"type"` Type AttribType `json:"type"`
} }
func (def AttribDef) Generate() string { func (def AttribDef) Generate() string {
const ( const (
boolType = "bool" boolType = "bool"
stringType = "string" stringType = "string"
@ -86,27 +86,27 @@ func (def AttribDef) Generate() string {
` `
) )
var template string var template string
switch def.Type { switch def.Type {
case Bool: case Bool:
template = boolTemplate template = boolTemplate
case String: case String:
template = stringTemplate template = stringTemplate
default: default:
panic(fmt.Errorf("unknown attribute type: %v", def.Type)) 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 { type ElemDef struct {
Name string `json:"name"` Name string `json:"name"`
Void bool `json:"void"` Void bool `json:"void"`
} }
func (def ElemDef) Generate() string { func (def ElemDef) Generate() string {
const ( const (
parentTemplate = `// %[1]s creates a <%[2]s> element. parentTemplate = `// %[1]s creates a <%[2]s> element.
func %[1]s(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { func %[1]s(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement { return func(children ...hatmill.Term) hatmill.ParentElement {
return 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 { func %[1]s(attribs ...hatmill.Attrib) hatmill.VoidElement {
return hatmill.VoidElement{ return hatmill.VoidElement{
TagName: "%[2]s", TagName: "%[2]s",
@ -127,18 +127,18 @@ func (def ElemDef) Generate() string {
} }
} }
` `
) )
template := parentTemplate template := parentTemplate
if def.Void { if def.Void {
template = voidTemplate template = voidTemplate
} }
return fmt.Sprintf(template, identifier(def.Name), def.Name) return fmt.Sprintf(template, identifier(def.Name), def.Name)
} }
type Defs struct { type Defs struct {
Attributes []AttribDef `json:"attributes"` Attributes []AttribDef `json:"attributes"`
Elements []ElemDef `json:"elements"` Elements []ElemDef `json:"elements"`
} }
func writeFormatted(fileName, packageName string, writeDefs func(*bytes.Buffer)) error { 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() { func main() {
inputPath := flag.String("input", "", "JSON input file") inputPath := flag.String("input", "", "JSON input file")
elemPath := flag.String("elemfile", "", "generated element .go file") elemPath := flag.String("elemfile", "", "generated element .go file")
elemPkg := flag.String("elempkg", "", "generated element package name") elemPkg := flag.String("elempkg", "", "generated element package name")
attribPath := flag.String("attribfile", "", "generated attribute .go file") attribPath := flag.String("attribfile", "", "generated attribute .go file")
attribPkg := flag.String("attribpkg", "", "generated attribute package name") attribPkg := flag.String("attribpkg", "", "generated attribute package name")
flag.Parse() flag.Parse()
input, err := ioutil.ReadFile(*inputPath) input, err := ioutil.ReadFile(*inputPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
var defs Defs var defs Defs
err = json.Unmarshal(input, &defs) err = json.Unmarshal(input, &defs)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = writeFormatted(*attribPath, *attribPkg, func(buf *bytes.Buffer) { err = writeFormatted(*attribPath, *attribPkg, func(buf *bytes.Buffer) {
for _, attribDef := range defs.Attributes { for _, attribDef := range defs.Attributes {
buf.WriteString(attribDef.Generate()) buf.WriteString(attribDef.Generate())
} }
}) })
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -182,8 +182,8 @@ func main() {
err = writeFormatted(*elemPath, *elemPkg, func(buf *bytes.Buffer) { err = writeFormatted(*elemPath, *elemPkg, func(buf *bytes.Buffer) {
for _, elemDef := range defs.Elements { for _, elemDef := range defs.Elements {
buf.WriteString(elemDef.Generate()) buf.WriteString(elemDef.Generate())
} }
}) })
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)