google-json-style-response

module
v0.0.0-...-49cfd34 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2015 License: Apache-2.0

README

google-json-style-response

Build Status

Use one command to style your JSON responses like Google does. For more information, check out Google's JSON Style Guide.

WARNING: This package is still in development, API might receive changes and features like pagination are still missing.

Usage

go get github.com/ory-am/google-json-style-response/responder
go get github.com/ory-am/google-json-style-response/receiver
Send a success response
import "github.com/ory-am/google-json-style-response/responder"

// ...
func(w http.ResponseWriter, r *http.Request) {
    data := struct{
        A string
    } {
        A: "b",
    }

    responder := New("1.0")
    o := responder.Success(data)
    err := responder.Write(w, o)
    if err != nil {
        // ...
    }
}
// ...
Send an error response
import "github.com/ory-am/google-json-style-response/responder"

// ...
func(w http.ResponseWriter, r *http.Request) {
    responder := New("1.0")
    o := responder.Error(500, "Internal Server Error")
    err := responder.Write(w, o)
}
// ...
Send multiple errors
import "github.com/ory-am/google-json-style-response/responder"

// ...
func(w http.ResponseWriter, r *http.Request) {
    responder := New("1.0")
    o := responder.Error(500, "Internal Server Error")
    responder.AddError(ErrorItem{
        Message: "baz",
    })
    err := responder.Write(w, o)
}
// ...
Responder & Receiver

Taken from the unit tests:

rc := responder.New("1.0")
o := rc.Success(struct{
        A string
    }{
        A: "a",
    })

b, err := o.Marshal()
// if err != nil ...

r := New("1.1")
reader := bytes.NewReader(b)
response, err = r.GetResponse(reader)
// if err != nil ...

// fmt.Printf("%s", response)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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