Esempio n. 1
0
	"github.com/pressly/chi/_examples/versions/presenter/v3"
	"github.com/pressly/chi/render"
)

// Article presented in API version 2.
type Article struct {
	*v3.Article `json:",inline" xml:",inline"`

	// Additional fields.
	SelfURL string `json:"self_url" xml:"self_url"`

	// Omitted fields.
	URL interface{} `json:"url,omitempty" xml:"url,omitempty"`
}

var Presenter = render.NewPresenter(ArticleV3ToV2)

func init() {
	Presenter.CopyFrom(v3.Presenter)
}

func ArticleV3ToV2(r *http.Request, from *v3.Article) (*Article, error) {
	to := &Article{
		Article: from,
		SelfURL: fmt.Sprintf("http://localhost:3333/v2?id=%v", from.ID),
	}
	to.APIVersion = "v2"
	return to, nil
}
Esempio n. 2
0
// Article presented in API version 2.
type Article struct {
	*data.Article `json:",inline" xml:",inline"`

	// Additional fields.
	URL        string `json:"url" xml:"url"`
	ViewsCount int64  `json:"views_count" xml:"views_count"`
	APIVersion string `json:"api_version" xml:"api_version"`

	// Omitted fields.
	// Show custom_data explicitly for auth'd users only.
	CustomDataForAuthUsers interface{} `json:"custom_data,omitempty" xml:"custom_data,omitempty"`
}

var Presenter = render.NewPresenter(CatchAll, ArticleToV3, ArticleChanToV3Chan, ArticleSliceToV3Slice)

func ArticleToV3(r *http.Request, from *data.Article) (*Article, error) {
	log.Printf("item presenter!")

	rand.Seed(time.Now().Unix())
	to := &Article{
		Article:    from,
		ViewsCount: rand.Int63n(100000),
		URL:        fmt.Sprintf("http://localhost:3333/v3/?id=%v", from.ID),
		APIVersion: "v3",
	}
	// Only show to auth'd user.
	if _, ok := r.Context().Value("auth").(bool); ok {
		to.CustomDataForAuthUsers = from.CustomDataForAuthUsers
	}
Esempio n. 3
0
import (
	"fmt"
	"net/http"

	"github.com/pressly/chi/_examples/versions/presenter/v2"
	"github.com/pressly/chi/render"
)

// Article presented in API version 1.
type Article struct {
	*v2.Article `json:",inline" xml:",inline"`

	Data map[string]bool `json:"data" xml:"data"`
}

var Presenter = render.NewPresenter(ArticleV2ToV1)

func init() {
	Presenter.CopyFrom(v2.Presenter)
}

func ArticleV2ToV1(r *http.Request, from *v2.Article) (*Article, error) {
	to := &Article{
		Article: from,
		Data:    map[string]bool{},
	}
	to.SelfURL = fmt.Sprintf("http://localhost:3333/v1?id=%v", from.ID)
	to.APIVersion = "v1"
	for _, item := range from.Data {
		to.Data[item] = true
	}