Skip to content

Commit

Permalink
Implement stderrPrint template function, suppress <nil> output (#224)
Browse files Browse the repository at this point in the history
Implement stderrPrint template function, suppress <nil> output from stderrPrintf
  • Loading branch information
VojtechVitek committed Jul 28, 2023
1 parent e85039a commit 31da006
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ See https://pkg.go.dev/text/template#hdr-Functions
| Template flow | Description | webrpc-gen |
|------------------------------------------------|-------------------------------------------------|-------------|
| `minVersion {{.WebrpcVersion}} v1.4` | Returns `boolean` if the given major/minor semver is at least v1.4 | v0.7.0 |
| `stderrPrintf "format %v" ARGS...` | `printf` to `webrpc-gen` stderr | v0.7.0 |
| `stderrPrint ARGS...` | `print` to `webrpc-gen` CLI stderr | v0.13.0 |
| `stderrPrintf "format %v" ARGS...` | `printf` to `webrpc-gen` CLI stderr | v0.7.0 |
| `exit INT` | Terminate template execution, useful for fatal errors | v0.7.0 |
| `dump VAR` | Dump variable | v0.13.0 |
| `hasField OBJ FIELD` | Check if object has a given field | v0.13.0 |
Expand Down
1 change: 1 addition & 0 deletions gen/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
func templateFuncMap(opts map[string]interface{}) map[string]interface{} {
return map[string]interface{}{
// Template flow, errors, debugging.
"stderrPrint": stderrPrint, // v0.13.0
"stderrPrintf": stderrPrintf, // v0.7.0
"exit": exit, // v0.7.0
"minVersion": minVersion, // v0.7.0
Expand Down
14 changes: 11 additions & 3 deletions gen/funcmap_flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ import (
// Similar to "printf" but instead of writing into the generated
// output file, stderrPrintf writes to webrpc-gen CLI stderr.
// Useful for printing template errors / debugging.
func stderrPrintf(format string, a ...interface{}) error {
_, err := fmt.Fprintf(os.Stderr, format, a...)
return err
func stderrPrintf(format string, a ...interface{}) string {
_, _ = fmt.Fprintf(os.Stderr, format, a...)
return ""
}

// Similar to "print" but instead of writing into the generated
// output file, stderrPrint writes to webrpc-gen CLI stderr.
// Useful for printing template errors / debugging.
func stderrPrint(a ...interface{}) string {
_, _ = fmt.Fprint(os.Stderr, a...)
return ""
}

// Terminate template execution with a status code.
Expand Down

0 comments on commit 31da006

Please sign in to comment.