encoder

package module
v0.0.0-...-0a43224 Latest Latest
Warning

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

Go to latest
Published: May 16, 2014 License: MIT Imports: 7 Imported by: 0

README

encoder wercker status

This is a simple wrapper to the json.Marshal, which adds ability to skip some fields of structure. Unlike 'render' package it doesn't write anything, just returns marshalled data. It's useful for things like passwords, statuses, activation codes, etc...

E.g.:

type Some struct {
	Login    string        `json:"login"`
	Password string        `json:"password,omitempty"  out:"false"`
}

Field 'Password' won't be exported.

Usage.

It's pretty straightforward:

m.Use(func(c martini.Context, w http.ResponseWriter) {
	c.MapTo(encoder.JsonEncoder{}, (*encoder.Encoder)(nil))
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
})

Here is a ready to use example:

package main

import (
	"github.com/martini-contrib/encoder"
	"github.com/codegangsta/martini"
	"log"
	"net/http"
)

type Some struct {
	Login    string `json:"login"`
	Password string `json:"password" out:"false"`
}

func main() {
	m := martini.New()
	route := martini.NewRouter()

	// map json encoder
	m.Use(func(c martini.Context, w http.ResponseWriter) {
		c.MapTo(encoder.JsonEncoder{}, (*encoder.Encoder)(nil))
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
	})

	route.Get("/test", func(enc encoder.Encoder) (int, []byte) {
		result := &Some{"awesome", "hidden"}
		return http.StatusOK, encoder.Must(enc.Encode(result))
	})

	m.Action(route.Handle)

	log.Println("Waiting for connections...")

	if err := http.ListenAndServe(":8000", m); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSON

func JSON() func(martini.Context, http.ResponseWriter)

func Must

func Must(data []byte, err error) []byte

Because `panic`s are caught by martini's Recovery handler, it can be used to return server-side errors (500). Some helpful text message should probably be sent, although not the technical error (which is printed in the log).

func XML

func XML() func(martini.Context, http.ResponseWriter)

Types

type Encoder

type Encoder interface {
	Encode(v ...interface{}) ([]byte, error)
	Render(v ...interface{}) []byte
}

An Encoder implements an encoding format of values to be sent as response to requests on the API endpoints.

type JsonEncoder

type JsonEncoder struct{}

func (JsonEncoder) Encode

func (_ JsonEncoder) Encode(v ...interface{}) ([]byte, error)

@todo test for slice of structure support

func (JsonEncoder) Render

func (enc JsonEncoder) Render(v ...interface{}) []byte

type XmlEncoder

type XmlEncoder struct{}

func (XmlEncoder) Encode

func (_ XmlEncoder) Encode(v ...interface{}) ([]byte, error)

Since we don't use xml as a binding source, we don't need to use copyStruct here. Just Marshal.

func (XmlEncoder) Render

func (enc XmlEncoder) Render(v ...interface{}) []byte

Jump to

Keyboard shortcuts

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