From 296f7c71dadd19d4db6e93aba37a3a714f7555a7 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 22 Apr 2019 22:12:12 -0600 Subject: [PATCH] Added float attribute type --- attribute/generated.go | 20 ++++++++++---------- defs.json | 10 +++++----- hatmill_test.go | 16 ++++++---------- internal/codegen/codegen.go | 12 ++++++++++++ 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/attribute/generated.go b/attribute/generated.go index 02d05d1..355410b 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -342,10 +342,10 @@ func Hidden() hatmill.Attrib { } // High creates a "high" attribute -func High(value string) hatmill.Attrib { +func High(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "high", - Value: value, + Value: strconv.FormatFloat(float64(value), 'G', -1, 32), } } @@ -468,10 +468,10 @@ func Loop() hatmill.Attrib { } // Low creates a "low" attribute -func Low(value string) hatmill.Attrib { +func Low(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "low", - Value: value, + Value: strconv.FormatFloat(float64(value), 'G', -1, 32), } } @@ -484,10 +484,10 @@ func Manifest(value string) hatmill.Attrib { } // Max creates a "max" attribute -func Max(value string) hatmill.Attrib { +func Max(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "max", - Value: value, + Value: strconv.FormatFloat(float64(value), 'G', -1, 32), } } @@ -516,10 +516,10 @@ func Method(value string) hatmill.Attrib { } // Min creates a "min" attribute -func Min(value string) hatmill.Attrib { +func Min(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "min", - Value: value, + Value: strconv.FormatFloat(float64(value), 'G', -1, 32), } } @@ -568,10 +568,10 @@ func Open() hatmill.Attrib { } // Optimum creates a "optimum" attribute -func Optimum(value string) hatmill.Attrib { +func Optimum(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "optimum", - Value: value, + Value: strconv.FormatFloat(float64(value), 'G', -1, 32), } } diff --git a/defs.json b/defs.json index 72ea458..93ee1d5 100644 --- a/defs.json +++ b/defs.json @@ -43,7 +43,7 @@ {"name": "headers", "type": "string"}, {"name": "height", "type": "int"}, {"name": "hidden", "type": "bool"}, - {"name": "high", "type": "string"}, + {"name": "high", "type": "float"}, {"name": "href", "type": "string"}, {"name": "hreflang", "type": "string"}, {"name": "http-equiv", "type": "string"}, @@ -59,20 +59,20 @@ {"name": "language", "type": "string"}, {"name": "list", "type": "string"}, {"name": "loop", "type": "bool"}, - {"name": "low", "type": "string"}, + {"name": "low", "type": "float"}, {"name": "manifest", "type": "string"}, - {"name": "max", "type": "string"}, + {"name": "max", "type": "float"}, {"name": "maxlength", "type": "int"}, {"name": "media", "type": "string"}, {"name": "method", "type": "string"}, - {"name": "min", "type": "string"}, + {"name": "min", "type": "float"}, {"name": "minlength", "type": "int"}, {"name": "multiple", "type": "bool"}, {"name": "muted", "type": "bool"}, {"name": "name", "type": "string"}, {"name": "novalidate", "type": "bool"}, {"name": "open", "type": "bool"}, - {"name": "optimum", "type": "string"}, + {"name": "optimum", "type": "float"}, {"name": "pattern", "type": "string"}, {"name": "ping", "type": "string"}, {"name": "placeholder", "type": "string"}, diff --git a/hatmill_test.go b/hatmill_test.go index a9bd1df..e7509ba 100644 --- a/hatmill_test.go +++ b/hatmill_test.go @@ -182,18 +182,14 @@ func Example() { userInput := "" document := he.Html()( - he.Head()( - he.Meta(ha.HttpEquiv("refresh"), ha.Content("5")), - ), he.Body()( - he.Div()( - he.Img(ha.Src("./me.jpg"), ha.Id("profile-photo")), - hatmill.Text(html.EscapeString(userInput)), - he.Div(ha.Disabled(), ha.CustomData("coolness", "awesome"))(), - he.Textarea(ha.Height(25))(), - ), + he.Img(ha.Src("./photo.jpg")), + hatmill.Text(html.EscapeString(userInput)), + he.Div(ha.Disabled(), ha.CustomData("coolness", "awesome"))(), + he.Textarea(ha.Rows(25))(), + he.Meter(ha.Min(-1.3), ha.Max(5.5E12))(), ), ) hatmill.WriteDocument(os.Stdout, document) - // Output:
<script>launchMissiles();</script>
+ // Output: <script>launchMissiles();</script>
} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index f437b4f..7c58775 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -57,6 +57,18 @@ var attribTypes = map[string]AttribTypeInfo{ `, Imports: []string{"strconv"}, }, + + "float": { + Template: `// %[1]s creates a "%[2]s" attribute + func %[1]s(value float32) hatmill.Attrib { + return hatmill.Attrib{ + Key: "%[2]s", + Value: strconv.FormatFloat(float64(value), 'G', -1, 32), + } + } + `, + Imports: []string{"strconv"}, + }, } // Def represents a top-level definition and its required imports.