From 94a7c075c75dafb03be34c093bc342fc4d8071a0 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Tue, 2 Apr 2019 20:05:48 -0600 Subject: [PATCH] Assume generated code lands in a subdirectory --- html5/html5.go | 2 +- internal/codegen/codegen.go | 53 +++++++++++++++---------------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/html5/html5.go b/html5/html5.go index aa267aa..01186ea 100644 --- a/html5/html5.go +++ b/html5/html5.go @@ -1,6 +1,6 @@ package html5 -//go:generate go run ../internal/codegen/codegen.go -input defs.json -output generated.go -package html5 -import +//go:generate go run ../internal/codegen/codegen.go -input defs.json -output generated.go -package html5 import "gitlab.codemonkeysoftware.net/b/hatmill" diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 063adb6..19df4e5 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -15,14 +15,12 @@ const headerFmt = `// GENERATED BY gitlab.codemonkeysoftware.net/b/hatmill/inter // DO NOT EDIT! package %s + +import "gitlab.codemonkeysoftware.net/b/hatmill" ` -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 fileHeader(packageName string) string { + return fmt.Sprintf(headerFmt, packageName) } type AttribType int @@ -58,32 +56,28 @@ type AttribDef struct { Type AttribType `json:"type"` } -func (def AttribDef) Generate(qualified bool) string { +func (def AttribDef) Generate() string { const ( boolType = "bool" stringType = "string" stringTemplate = `// %[1]s creates a "%[2]s" attribute - func %[1]s(value string) %[3]sAttrib { - return %[3]sAttrib{ + func %[1]s(value string) hatmill.Attrib { + return hatmill.Attrib{ Key: "%[2]s", Value: value, } } ` boolTemplate = `// %[1]s creates a "%[2]s" attribute - func %[1]s() %[3]sAttrib { - return %[3]sAttrib{ + func %[1]s() hatmill.Attrib { + return hatmill.Attrib{ Key: "%[2]s", } } ` ) - var pkg string - if qualified { - pkg = "hatmill." - } var template string switch def.Type { case Bool: @@ -94,7 +88,7 @@ func (def AttribDef) Generate(qualified bool) string { panic(fmt.Errorf("unknown attribute type: %v", def.Type)) } - return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg) + return fmt.Sprintf(template, strings.Title(def.Name), def.Name) } type ElemDef struct { @@ -102,13 +96,13 @@ type ElemDef struct { Void bool `json:"void"` } -func (def ElemDef) Generate(qualified bool) string { +func (def ElemDef) Generate() string { const ( parentTemplate = `// %[1]s creates a <%[2]s> element. - func %[1]s(attribs ...%[3]sAttrib) func(children ...%[3]sTerm) %[3]sParentElement { - return func(children ...%[3]sTerm) %[3]sParentElement { - return %[3]sParentElement{ - VoidElement: %[3]sVoidElement{ + func %[1]s(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement { + return func(children ...hatmill.Term) hatmill.ParentElement { + return hatmill.ParentElement{ + VoidElement: hatmill.VoidElement{ TagName: "%[2]s", Attribs: attribs, }, @@ -118,8 +112,8 @@ func (def ElemDef) Generate(qualified bool) string { } ` voidTemplate = `// %[1]s creates a <%[2]s> element. - func %[1]s(attribs ...%[3]sAttrib) %[3]sVoidElement { - return %[3]sVoidElement{ + func %[1]s(attribs ...hatmill.Attrib) hatmill.VoidElement { + return hatmill.VoidElement{ TagName: "%[2]s", Attribs: attribs, } @@ -127,15 +121,11 @@ func (def ElemDef) Generate(qualified bool) string { ` ) - var pkg string - if qualified { - pkg = "hatmill." - } template := parentTemplate if def.Void { template = voidTemplate } - return fmt.Sprintf(template, strings.Title(def.Name), def.Name, pkg) + return fmt.Sprintf(template, strings.Title(def.Name), def.Name) } type Defs struct { @@ -147,7 +137,6 @@ func main() { 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) @@ -162,12 +151,12 @@ func main() { } output := new(bytes.Buffer) - output.WriteString(fileHeader(*packageName, *addImport)) + output.WriteString(fileHeader(*packageName)) for _, elemDef := range defs.Elements { - output.WriteString(elemDef.Generate(*addImport)) + output.WriteString(elemDef.Generate()) } for _, attribDef := range defs.Attributes { - output.WriteString(attribDef.Generate(*addImport)) + output.WriteString(attribDef.Generate()) } formatted, err := format.Source(output.Bytes())