TemplatePrinter: Add support for string objects
authorMikael Berthe <mikael@lilotux.net>
Sun, 07 May 2017 19:30:24 +0200
changeset 90 b1da6dc9f689
parent 89 3758bb5f0979
child 91 02312ccc1fd3
TemplatePrinter: Add support for string objects This is a special case, but can be useful sometimes.
printer/templateprinter.go
--- a/printer/templateprinter.go	Sun May 07 17:32:33 2017 +0200
+++ b/printer/templateprinter.go	Sun May 07 19:30:24 2017 +0200
@@ -76,7 +76,7 @@
 		[]madon.Client, []madon.Context, []madon.Instance, []madon.Mention,
 		[]madon.Notification, []madon.Relationship, []madon.Report,
 		[]madon.Results, []madon.Status, []madon.StreamEvent, []madon.Tag,
-		[]*gomif.InstanceStatus:
+		[]*gomif.InstanceStatus, []string:
 		return p.templateForeach(ot, w)
 	}
 
@@ -84,6 +84,14 @@
 }
 
 func (p *TemplatePrinter) templatePrintSingleObj(obj interface{}, w io.Writer) error {
+	if s, ok := obj.(string); ok {
+		// obj is a simple string
+		if err := p.safeExecute(w, s); err != nil {
+			return fmt.Errorf("error executing template %q: %v", p.rawTemplate, err)
+		}
+		return nil
+	}
+
 	// This code comes from Kubernetes.
 	data, err := json.Marshal(obj)
 	if err != nil {