Browse Source

Implement toInt built in action

master v0.1.1
Jason T. Lenz 1 year ago
parent
commit
dea22af226
  1. 23
      README.md
  2. 74
      lib/gotFuncs.go
  3. 20
      main.go
  4. 23
      template/README.md.got
  5. 1
      tests/invalid-builtin-lnp-wrong-parameter/tCmd
  6. 1
      tests/invalid-builtin-lnp-wrong-parameter/tExit
  7. 1
      tests/invalid-builtin-lnp-wrong-parameter/tStderr
  8. 3
      tests/invalid-builtin-lnp-wrong-parameter/tStdin
  9. 0
      tests/invalid-builtin-lnp-wrong-parameter/tStdout
  10. 1
      tests/invalid-builtin-toInt-wrong-parameter/tCmd
  11. 1
      tests/invalid-builtin-toInt-wrong-parameter/tExit
  12. 1
      tests/invalid-builtin-toInt-wrong-parameter/tStderr
  13. 3
      tests/invalid-builtin-toInt-wrong-parameter/tStdin
  14. 0
      tests/invalid-builtin-toInt-wrong-parameter/tStdout
  15. 1
      tests/invalid-builtin-toInt-wrong-type/tCmd
  16. 1
      tests/invalid-builtin-toInt-wrong-type/tExit
  17. 1
      tests/invalid-builtin-toInt-wrong-type/tStderr
  18. 3
      tests/invalid-builtin-toInt-wrong-type/tStdin
  19. 0
      tests/invalid-builtin-toInt-wrong-type/tStdout
  20. 1
      tests/valid-builtin-toInt-number/tCmd
  21. 1
      tests/valid-builtin-toInt-number/tExit
  22. 0
      tests/valid-builtin-toInt-number/tStderr
  23. 3
      tests/valid-builtin-toInt-number/tStdin
  24. 1
      tests/valid-builtin-toInt-number/tStdout
  25. 1
      tests/valid-builtin-toInt-string/tCmd
  26. 1
      tests/valid-builtin-toInt-string/tExit
  27. 0
      tests/valid-builtin-toInt-string/tStderr
  28. 3
      tests/valid-builtin-toInt-string/tStdin
  29. 1
      tests/valid-builtin-toInt-string/tStdout

23
README.md

@ -188,13 +188,13 @@ advisable to avoid naming any elements in frontmatter starting with "pgot".
## Custom pgot actions
In addition to the standard [template
actions](https://golang.org/pkg/text/template/#hdr-Actions), there is currently
one custom template action defined by pgot (below). This is included more as a
simple example rather than a critical feature. For those interested in
implementing custom template actions the
[gotFuncs.go](/lenzj/pgot/src/branch/master/lib/gotFuncs.go) source
code demonstrates how custom template actions can be added.
In addition to the standard
[template actions](https://golang.org/pkg/text/template/#hdr-Actions), there are
currently two custom template actions defined by pgot (below). These are
included more as simple examples rather than critical features. For those
interested in implementing custom template actions the
[gotFuncs.go](/lenzj/pgot/src/branch/master/lib/gotFuncs.go) source code
demonstrates how custom template actions can be added.
```text
{{lnp "label" "url"}}
@ -204,6 +204,15 @@ lnp (link new page) generates the specified url as an html link which opens a
new page when the user selects the link. If label is nil (aka "") then the url
is displayed as the clickable link, otherwise the label itself is displayed.
```text
{{toInt value}}
```
toInt (to integer) is able to convert a value parameter to an integer. Multiple
types can be handled. For example a string representation of a number can be
converted. This can also convert the JSON default number type (float64) to an
integer.
## Compiling from source
### Dependencies

74
lib/gotFuncs.go

@ -1,13 +1,16 @@
package pgot
import (
"reflect"
"strconv"
"text/template"
)
var (
// This should include all publicly accessible custom got functions
funcMap = template.FuncMap{
"lnp": lnp,
"lnp": lnp,
"toInt": toInt,
}
)
@ -21,3 +24,72 @@ func lnp(label, url string) string {
return "<a href=\"" + url + "\" target=\"_blank\">" + label + "</a>"
}
}
// From html/template/content.go
// Copyright 2011 The Go Authors. All rights reserved.
// indirect returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil).
func indirect(a interface{}) interface{} {
if a == nil {
return nil
}
if t := reflect.TypeOf(a); t.Kind() != reflect.Ptr {
// Avoid creating a reflect.Value if it's not a pointer.
return a
}
v := reflect.ValueOf(a)
for v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
return v.Interface()
}
// Leveraged from https://github.com/spf13/cast
// caste.go
// Copyright (c) 2014 Steve Francia
// toInt casts an interface to an int type.
func toInt(i interface{}) int {
i = indirect(i)
switch s := i.(type) {
case int:
return s
case int64:
return int(s)
case int32:
return int(s)
case int16:
return int(s)
case int8:
return int(s)
case uint:
return int(s)
case uint64:
return int(s)
case uint32:
return int(s)
case uint16:
return int(s)
case uint8:
return int(s)
case float64:
return int(s)
case float32:
return int(s)
case string:
v, err := strconv.ParseInt(s, 0, 0)
if err == nil {
return int(v)
}
panic("unable to cast variable to int")
case bool:
if s {
return 1
}
return 0
case nil:
return 0
default:
panic("unable to cast variable to int")
}
}

20
main.go

@ -14,8 +14,8 @@ import (
var Version string
func main() {
// Get application name as executed from command prompt
appName := filepath.Base(os.Args[0])
// Get application name as executed from command prompt
appName := filepath.Base(os.Args[0])
// Set up formatting for error messages
log.SetFlags(0)
@ -25,22 +25,22 @@ func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
"Usage: %s [OPTION]... [FILE]...\n"+
"Process a got (golang template) file and send transformed text to output.\n"+
"Options:\n", appName)
"Process a got (golang template) file and send transformed text to output.\n"+
"Options:\n", appName)
flag.PrintDefaults()
}
var oflag = flag.String("o", "-", "output `file` path")
var iflag = flag.String("i", "", "colon separated list of `paths` to search with pgotInclude")
var dflag = flag.String("d", "", "string of json frontmatter to include")
var vflag = flag.Bool("v", false, "display " + appName + " version")
var vflag = flag.Bool("v", false, "display "+appName+" version")
flag.Parse()
// Display application version if requested
if *vflag {
fmt.Println(appName + " " + Version)
os.Exit(0)
}
// Display application version if requested
if *vflag {
fmt.Println(appName + " " + Version)
os.Exit(0)
}
// Prepare input and output streams
var (

23
template/README.md.got

@ -194,13 +194,13 @@ advisable to avoid naming any elements in frontmatter starting with "pgot".
## Custom pgot actions
In addition to the standard [template
actions](https://golang.org/pkg/text/template/#hdr-Actions), there is currently
one custom template action defined by pgot (below). This is included more as a
simple example rather than a critical feature. For those interested in
implementing custom template actions the
[gotFuncs.go]({{.repLoc}}{{.rname}}/src/branch/master/lib/gotFuncs.go) source
code demonstrates how custom template actions can be added.
In addition to the standard
[template actions](https://golang.org/pkg/text/template/#hdr-Actions), there are
currently two custom template actions defined by pgot (below). These are
included more as simple examples rather than critical features. For those
interested in implementing custom template actions the
[gotFuncs.go](/lenzj/pgot/src/branch/master/lib/gotFuncs.go) source code
demonstrates how custom template actions can be added.
```text
{{`{{lnp "label" "url"}}`}}
@ -210,6 +210,15 @@ lnp (link new page) generates the specified url as an html link which opens a
new page when the user selects the link. If label is nil (aka "") then the url
is displayed as the clickable link, otherwise the label itself is displayed.
```text
{{`{{toInt value}}`}}
```
toInt (to integer) is able to convert a value parameter to an integer. Multiple
types can be handled. For example a string representation of a number can be
converted. This can also convert the JSON default number type (float64) to an
integer.
## Compiling from source
### Dependencies

1
tests/invalid-builtin-lnp-wrong-parameter/tCmd

@ -0,0 +1 @@
{{.cli}} < tStdin

1
tests/invalid-builtin-lnp-wrong-parameter/tExit

@ -0,0 +1 @@
1

1
tests/invalid-builtin-lnp-wrong-parameter/tStderr

@ -0,0 +1 @@
pgot: template: .:1:2: executing "." at <lnp>: wrong number of args for lnp: want 2 got 0

3
tests/invalid-builtin-lnp-wrong-parameter/tStdin

@ -0,0 +1,3 @@
;;;
;;;
{{lnp}}

0
tests/invalid-builtin-lnp-wrong-parameter/tStdout

1
tests/invalid-builtin-toInt-wrong-parameter/tCmd

@ -0,0 +1 @@
{{.cli}} < tStdin

1
tests/invalid-builtin-toInt-wrong-parameter/tExit

@ -0,0 +1 @@
1

1
tests/invalid-builtin-toInt-wrong-parameter/tStderr

@ -0,0 +1 @@
pgot: template: .:1:2: executing "." at <toInt>: wrong number of args for toInt: want 1 got 4

3
tests/invalid-builtin-toInt-wrong-parameter/tStdin

@ -0,0 +1,3 @@
;;;
;;;
{{toInt "3.14" 4 5 6}}

0
tests/invalid-builtin-toInt-wrong-parameter/tStdout

1
tests/invalid-builtin-toInt-wrong-type/tCmd

@ -0,0 +1 @@
{{.cli}} < tStdin

1
tests/invalid-builtin-toInt-wrong-type/tExit

@ -0,0 +1 @@
1

1
tests/invalid-builtin-toInt-wrong-type/tStderr

@ -0,0 +1 @@
pgot: template: .:1:2: executing "." at <toInt "3.14">: error calling toInt: unable to cast variable to int

3
tests/invalid-builtin-toInt-wrong-type/tStdin

@ -0,0 +1,3 @@
;;;
;;;
{{toInt "3.14"}}

0
tests/invalid-builtin-toInt-wrong-type/tStdout

1
tests/valid-builtin-toInt-number/tCmd

@ -0,0 +1 @@
{{.cli}} < tStdin

1
tests/valid-builtin-toInt-number/tExit

@ -0,0 +1 @@
0

0
tests/valid-builtin-toInt-number/tStderr

3
tests/valid-builtin-toInt-number/tStdin

@ -0,0 +1,3 @@
;;;
;;;
{{toInt 4345}}

1
tests/valid-builtin-toInt-number/tStdout

@ -0,0 +1 @@
4345

1
tests/valid-builtin-toInt-string/tCmd

@ -0,0 +1 @@
{{.cli}} < tStdin

1
tests/valid-builtin-toInt-string/tExit

@ -0,0 +1 @@
0

0
tests/valid-builtin-toInt-string/tStderr

3
tests/valid-builtin-toInt-string/tStdin

@ -0,0 +1,3 @@
;;;
;;;
{{toInt "3"}}

1
tests/valid-builtin-toInt-string/tStdout

@ -0,0 +1 @@
3
Loading…
Cancel
Save