ex

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2020 License: MIT Imports: 7 Imported by: 0

README

Build Status codecov

ex - implements interfaces which write output to stdout

This is useful for writing short Examples in go, eg. when testing output from http handlers.

Examples

Writers implement the http.ResponseWriter interface

JsonWriter

r := bytes.NewBufferString(`{"name":"John","car":{"model":"x,2","plate":"abc123"}}`)
io.Copy(jsonWriter, r)

Output:

{
    "name":"John",
    "car":{
        "model":"x,2",
        "plate":"abc123"
    }
}

StatusWriter

w := NewStatusWriter()
w.Write([]byte("Hello, world!"))

Output:

200

BodyWriter

bodyWriter.Write([]byte("Hello, world!"))

Output:

Hello, world!

Documentation

Overview

Package ex implements interfaces which writes output to stdout

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpParts

func DumpParts(msg io.Reader, boundary string)

DumpParts prints each part of a multipart message

Example
buf := bytes.NewBufferString("")
body := multipart.NewWriter(buf)
body.SetBoundary("--X")

w, _ := body.CreatePart(textproto.MIMEHeader{})
w.Write([]byte("Hello"))

w, _ = body.CreatePart(textproto.MIMEHeader{})
w.Write([]byte(", world!"))

DumpParts(buf, "--X")
Output:

Hello, world!
Example (With_nice_json)
buf := bytes.NewBufferString("")
body := multipart.NewWriter(buf)
body.SetBoundary("--X")

header := textproto.MIMEHeader{}
header["Content-Type"] = []string{"application/json"}
w, _ := body.CreatePart(header)
w.Write([]byte(`{"text":"hello","more":"world"}`))

w, _ = body.CreatePart(textproto.MIMEHeader{})
w.Write([]byte("next part"))

DumpParts(buf, "--X")
Output:

{
    "text":"hello",
    "more":"world"
}
next part

func Print

func Print(any ...interface{})

Print writes any element to stdout.

Example
h := http.Header{}
h.Add("Content-Type", "text/plain")
h.Add("Abbe", "one")
h.Add("Abbe", "two")
Print(h)
Output:

Abbe: one
Abbe: two
Content-Type: text/plain

func Tfprint

func Tfprint(tidy TidyFunc, w io.Writer, any ...interface{})

func Tprint

func Tprint(tidy TidyFunc, any ...interface{})
Example
h := http.Header{}
h.Add("Content-Type", "text/plain")
h.Add("Abbe", "one")
h.Add("Abbe", "two")
tidy := func(v string) string {
	return strings.Replace(v, "one", "zero", -1)
}
Tprint(tidy, h)
Output:

Abbe: zero
Abbe: two
Content-Type: text/plain

Types

type BodyWriter

type BodyWriter struct {
	// contains filtered or unexported fields
}

func BodyOf

func BodyOf(r *http.Request, err error) (*BodyWriter, *http.Request)
Example
handler := func(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %s!", "world")
}
handler(BodyOf(http.NewRequest("GET", "/", nil)))
Output:

Hello, world!

func NewBodyWriter

func NewBodyWriter() *BodyWriter

func (*BodyWriter) Header

func (w *BodyWriter) Header() http.Header

func (*BodyWriter) Write

func (w *BodyWriter) Write(b []byte) (int, error)

Write the given byte slice to stdout

Example
bodyWriter.Write([]byte("Hello, world!"))
Output:

Hello, world!

func (*BodyWriter) WriteHeader

func (w *BodyWriter) WriteHeader(v int)
Example
bodyWriter.WriteHeader(200)
Output:

type HeaderWriter

type HeaderWriter struct {
	// contains filtered or unexported fields
}

func HeadersOf

func HeadersOf(r *http.Request, err error) (*HeaderWriter, *http.Request)
Example
handler := func(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	fmt.Fprintf(w, "Hello, %s!", "world")
}
handler(HeadersOf(http.NewRequest("GET", "/", nil)))
Output:

func NewHeaderWriter

func NewHeaderWriter() *HeaderWriter

func (*HeaderWriter) Header

func (w *HeaderWriter) Header() http.Header

func (*HeaderWriter) Write

func (w *HeaderWriter) Write(b []byte) (int, error)

Write the given byte slice to stdout

Example
headerWriter.Write([]byte("body not visible"))
Output:

func (*HeaderWriter) WriteHeader

func (w *HeaderWriter) WriteHeader(v int)
Example
w := NewHeaderWriter()
w.Header().Add("Content-type", "text/plain")
w.Header().Add("Accept", "*")
w.Header().Add("Accept", "application/json")
w.Write([]byte("body not visible"))
w.Write([]byte("second write does nothing"))
Output:

Accept: *; application/json
Content-Type: text/plain

type JsonWriter

type JsonWriter struct {
	Out io.Writer
	// contains filtered or unexported fields
}

JsonWriter formats incomming json and implements http.ResponseWriter.

func JsonOf

func JsonOf(r *http.Request, err error) (*JsonWriter, *http.Request)
Example
handler := func(w http.ResponseWriter, r *http.Request) {
	json.NewEncoder(w).Encode(
		struct {
			Message string
		}{"Hello, World!"})
}
handler(JsonOf(http.NewRequest("GET", "/", nil)))
Output:

{
    "Message":"Hello, World!"
}

func NewJsonWriter

func NewJsonWriter() *JsonWriter

func (*JsonWriter) Header

func (w *JsonWriter) Header() http.Header

func (*JsonWriter) Write

func (w *JsonWriter) Write(b []byte) (int, error)

Write the given byte slice to stdout

Example
json := `{"name":"John","car":{"model":"x,2","plate":"abc\"123"}}`
reader := bytes.NewBufferString(json)
io.Copy(jsonWriter, reader)
Output:

{
    "name":"John",
    "car":{
        "model":"x,2",
        "plate":"abc\"123"
    }
}
Example (Specific_output)
json := `{"name":"John","car":{"model":"x,2","plate":"abc\"123"}}`
reader := bytes.NewBufferString(json)
jsonWriter := NewJsonWriter()
jsonWriter.Out = os.Stdout
io.Copy(jsonWriter, reader)
Output:

{
    "name":"John",
    "car":{
        "model":"x,2",
        "plate":"abc\"123"
    }
}

func (*JsonWriter) WriteHeader

func (w *JsonWriter) WriteHeader(v int)
Example
jsonWriter.WriteHeader(200)
Output:

func (*JsonWriter) WriteTo

func (w *JsonWriter) WriteTo(out io.Writer, b []byte) (int, error)

Write the given byte slice to stdout

Example
json := `{"name":"John","car":{"model":"x,2","plate":"abc\"123"}}`
buf := bytes.NewBufferString("")
jsonWriter.WriteTo(buf, []byte(json))
fmt.Print(buf.String())
Output:

{
    "name":"John",
    "car":{
        "model":"x,2",
        "plate":"abc\"123"
    }
}

type StatusWriter

type StatusWriter struct {
	// contains filtered or unexported fields
}

func NewStatusWriter

func NewStatusWriter() *StatusWriter

func StatusOf

func StatusOf(r *http.Request, err error) (*StatusWriter, *http.Request)
Example
handler := func(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
}
handler(StatusOf(http.NewRequest("GET", "/", nil)))
Output:

200

func (*StatusWriter) Header

func (w *StatusWriter) Header() http.Header

func (*StatusWriter) Write

func (w *StatusWriter) Write(b []byte) (int, error)
Example
w := NewStatusWriter()
w.Write([]byte("Hello, world!"))
Output:

200

func (*StatusWriter) WriteHeader

func (w *StatusWriter) WriteHeader(v int)

Write the given value to stdout

Example
statusWriter.WriteHeader(200)
Output:

200

type TidyFunc

type TidyFunc func(string) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL