spew.go

  1/*
  2 * Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
  3 *
  4 * Permission to use, copy, modify, and distribute this software for any
  5 * purpose with or without fee is hereby granted, provided that the above
  6 * copyright notice and this permission notice appear in all copies.
  7 *
  8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15 */
 16
 17package spew
 18
 19import (
 20	"fmt"
 21	"io"
 22)
 23
 24// Errorf is a wrapper for fmt.Errorf that treats each argument as if it were
 25// passed with a default Formatter interface returned by NewFormatter.  It
 26// returns the formatted string as a value that satisfies error.  See
 27// NewFormatter for formatting details.
 28//
 29// This function is shorthand for the following syntax:
 30//
 31//	fmt.Errorf(format, spew.NewFormatter(a), spew.NewFormatter(b))
 32func Errorf(format string, a ...interface{}) (err error) {
 33	return fmt.Errorf(format, convertArgs(a)...)
 34}
 35
 36// Fprint is a wrapper for fmt.Fprint that treats each argument as if it were
 37// passed with a default Formatter interface returned by NewFormatter.  It
 38// returns the number of bytes written and any write error encountered.  See
 39// NewFormatter for formatting details.
 40//
 41// This function is shorthand for the following syntax:
 42//
 43//	fmt.Fprint(w, spew.NewFormatter(a), spew.NewFormatter(b))
 44func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
 45	return fmt.Fprint(w, convertArgs(a)...)
 46}
 47
 48// Fprintf is a wrapper for fmt.Fprintf that treats each argument as if it were
 49// passed with a default Formatter interface returned by NewFormatter.  It
 50// returns the number of bytes written and any write error encountered.  See
 51// NewFormatter for formatting details.
 52//
 53// This function is shorthand for the following syntax:
 54//
 55//	fmt.Fprintf(w, format, spew.NewFormatter(a), spew.NewFormatter(b))
 56func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
 57	return fmt.Fprintf(w, format, convertArgs(a)...)
 58}
 59
 60// Fprintln is a wrapper for fmt.Fprintln that treats each argument as if it
 61// passed with a default Formatter interface returned by NewFormatter.  See
 62// NewFormatter for formatting details.
 63//
 64// This function is shorthand for the following syntax:
 65//
 66//	fmt.Fprintln(w, spew.NewFormatter(a), spew.NewFormatter(b))
 67func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
 68	return fmt.Fprintln(w, convertArgs(a)...)
 69}
 70
 71// Print is a wrapper for fmt.Print that treats each argument as if it were
 72// passed with a default Formatter interface returned by NewFormatter.  It
 73// returns the number of bytes written and any write error encountered.  See
 74// NewFormatter for formatting details.
 75//
 76// This function is shorthand for the following syntax:
 77//
 78//	fmt.Print(spew.NewFormatter(a), spew.NewFormatter(b))
 79func Print(a ...interface{}) (n int, err error) {
 80	return fmt.Print(convertArgs(a)...)
 81}
 82
 83// Printf is a wrapper for fmt.Printf that treats each argument as if it were
 84// passed with a default Formatter interface returned by NewFormatter.  It
 85// returns the number of bytes written and any write error encountered.  See
 86// NewFormatter for formatting details.
 87//
 88// This function is shorthand for the following syntax:
 89//
 90//	fmt.Printf(format, spew.NewFormatter(a), spew.NewFormatter(b))
 91func Printf(format string, a ...interface{}) (n int, err error) {
 92	return fmt.Printf(format, convertArgs(a)...)
 93}
 94
 95// Println is a wrapper for fmt.Println that treats each argument as if it were
 96// passed with a default Formatter interface returned by NewFormatter.  It
 97// returns the number of bytes written and any write error encountered.  See
 98// NewFormatter for formatting details.
 99//
100// This function is shorthand for the following syntax:
101//
102//	fmt.Println(spew.NewFormatter(a), spew.NewFormatter(b))
103func Println(a ...interface{}) (n int, err error) {
104	return fmt.Println(convertArgs(a)...)
105}
106
107// Sprint is a wrapper for fmt.Sprint that treats each argument as if it were
108// passed with a default Formatter interface returned by NewFormatter.  It
109// returns the resulting string.  See NewFormatter for formatting details.
110//
111// This function is shorthand for the following syntax:
112//
113//	fmt.Sprint(spew.NewFormatter(a), spew.NewFormatter(b))
114func Sprint(a ...interface{}) string {
115	return fmt.Sprint(convertArgs(a)...)
116}
117
118// Sprintf is a wrapper for fmt.Sprintf that treats each argument as if it were
119// passed with a default Formatter interface returned by NewFormatter.  It
120// returns the resulting string.  See NewFormatter for formatting details.
121//
122// This function is shorthand for the following syntax:
123//
124//	fmt.Sprintf(format, spew.NewFormatter(a), spew.NewFormatter(b))
125func Sprintf(format string, a ...interface{}) string {
126	return fmt.Sprintf(format, convertArgs(a)...)
127}
128
129// Sprintln is a wrapper for fmt.Sprintln that treats each argument as if it
130// were passed with a default Formatter interface returned by NewFormatter.  It
131// returns the resulting string.  See NewFormatter for formatting details.
132//
133// This function is shorthand for the following syntax:
134//
135//	fmt.Sprintln(spew.NewFormatter(a), spew.NewFormatter(b))
136func Sprintln(a ...interface{}) string {
137	return fmt.Sprintln(convertArgs(a)...)
138}
139
140// convertArgs accepts a slice of arguments and returns a slice of the same
141// length with each argument converted to a default spew Formatter interface.
142func convertArgs(args []interface{}) (formatters []interface{}) {
143	formatters = make([]interface{}, len(args))
144	for index, arg := range args {
145		formatters[index] = NewFormatter(arg)
146	}
147	return formatters
148}