From 0bc7f74ce6d10339db2e0a51fb0dc2e0353ed7c6 Mon Sep 17 00:00:00 2001 From: Stanislas Michalak Date: Sat, 3 Mar 2018 12:49:50 +0100 Subject: [PATCH] Remove junk merge files (#952) --- render/auto.go~HEAD | 213 ---------------------------- render/auto_test.go~HEAD | 294 --------------------------------------- 2 files changed, 507 deletions(-) delete mode 100644 render/auto.go~HEAD delete mode 100644 render/auto_test.go~HEAD diff --git a/render/auto.go~HEAD b/render/auto.go~HEAD deleted file mode 100644 index f9ba46fa3..000000000 --- a/render/auto.go~HEAD +++ /dev/null @@ -1,213 +0,0 @@ -package render - -import ( - "context" - "fmt" - "io" - "path" - "reflect" - "strings" - - "github.com/markbates/inflect" - "github.com/pkg/errors" -) - -var errNoID = errors.New("no ID on model") - -// ErrRedirect indicates to Context#Render that this is a -// redirect and a template shouldn't be rendered. -type ErrRedirect struct { - Status int - URL string -} - -func (ErrRedirect) Error() string { - return "" -} - -// Auto figures out how to render the model based information -// about the request and the name of the model. Auto supports -// automatic rendering of HTML, JSON, and XML. Any status code -// give to Context#Render between 300 - 400 will be respected -// by Auto. Other status codes are not. -/* -# Rules for HTML template lookup: -GET /users - users/index.html -GET /users/id - users/show.html -GET /users/new - users/new.html -GET /users/id/edit - users/edit.html -POST /users - (redirect to /users/id or render user/new.html) -PUT /users/edit - (redirect to /users/id or render user/edit.html) -DELETE /users/id - redirect to /users -*/ -func Auto(ctx context.Context, i interface{}) Renderer { - e := New(Options{}) - return e.Auto(ctx, i) -} - -// Auto figures out how to render the model based information -// about the request and the name of the model. Auto supports -// automatic rendering of HTML, JSON, and XML. Any status code -// give to Context#Render between 300 - 400 will be respected -// by Auto. Other status codes are not. -/* -# Rules for HTML template lookup: -GET /users - users/index.html -GET /users/id - users/show.html -GET /users/new - users/new.html -GET /users/id/edit - users/edit.html -POST /users - (redirect to /users/id or render user/new.html) -PUT /users/edit - (redirect to /users/id or render user/edit.html) -DELETE /users/id - redirect to /users -*/ -func (e *Engine) Auto(ctx context.Context, i interface{}) Renderer { - ct, ok := ctx.Value("contentType").(string) - if !ok { - ct = "text/html" - } - ct = strings.ToLower(ct) - - if strings.Contains(ct, "json") { - return e.JSON(i) - } - - if strings.Contains(ct, "xml") { - return e.XML(i) - } - - return htmlAutoRenderer{ - Engine: e, - model: i, - } -} - -type htmlAutoRenderer struct { - *Engine - model interface{} -} - -func (htmlAutoRenderer) ContentType() string { - return "text/html" -} - -func (ir htmlAutoRenderer) Render(w io.Writer, data Data) error { - name := inflect.Name(ir.typeName().Singular()) - pname := inflect.Name(name.Plural()) - - if ir.isPlural() { - data[pname.VarCasePlural()] = ir.model - } else { - data[name.VarCaseSingular()] = ir.model - } - - cp, ok := data["current_path"].(string) - switch data["method"] { - case "PUT": - code := ir.status(data) - // if successful redirect to the GET version of the URL - // PUT /users/1 -> redirect -> GET /users/1 - if code < 400 { - return ErrRedirect{ - Status: code, - URL: cp, - } - } - if ok { - // PUT /users/1 -> /users - cp = path.Dir(cp) - } else { - cp = pname.File() - } - return ir.HTML(fmt.Sprintf("%s/edit.html", cp)).Render(w, data) - case "POST": - if err := ir.redirect(cp, w, data); err != nil { - if er, ok := err.(ErrRedirect); ok && er.Status >= 300 && er.Status < 400 { - return err - } - } - return ir.HTML(fmt.Sprintf("%s/new.html", cp)).Render(w, data) - case "DELETE": - if ok { - // DELETE /users/{id} -> /users - cp = path.Dir(cp) - } else { - cp = "/" + pname.URL() - } - return ErrRedirect{ - Status: 302, - URL: cp, - } - } - if ok { - if strings.HasSuffix(cp, "/edit") { - // GET /users/{id}/edit -> /users - cp = path.Dir(path.Dir(cp)) - return ir.HTML(fmt.Sprintf("%s/edit.html", cp)).Render(w, data) - } - if strings.HasSuffix(cp, "/new") { - // GET /users/new -> /users - cp = path.Dir(cp) - return ir.HTML(fmt.Sprintf("%s/new.html", cp)).Render(w, data) - } - - if ir.isPlural() { - // GET /users - if it's a slice/array render the index page - return ir.HTML(fmt.Sprintf("%s/%s.html", cp, "index")).Render(w, data) - } - // GET /users/{id} - return ir.HTML(fmt.Sprintf("%s/show.html", path.Dir(cp))).Render(w, data) - } - - return errors.New("could not auto render this model, please render it manually") -} - -func (ir htmlAutoRenderer) redirect(path string, w io.Writer, data Data) error { - rv := reflect.Indirect(reflect.ValueOf(ir.model)) - f := rv.FieldByName("ID") - if !f.IsValid() { - return errNoID - } - - fi := f.Interface() - rt := reflect.TypeOf(fi) - zero := reflect.Zero(rt) - if fi != zero.Interface() { - url := fmt.Sprintf("%s/%v", path, f.Interface()) - - return ErrRedirect{ - Status: ir.status(data), - URL: url, - } - } - return errNoID -} - -func (ir htmlAutoRenderer) status(data Data) int { - if i, ok := data["status"].(int); ok { - if i >= 300 { - return i - } - } - return 302 -} - -func (ir htmlAutoRenderer) typeName() inflect.Name { - rv := reflect.Indirect(reflect.ValueOf(ir.model)) - rt := rv.Type() - switch rt.Kind() { - case reflect.Slice, reflect.Array: - el := rt.Elem() - return inflect.Name(el.Name()) - } - return inflect.Name(rt.Name()) -} - -func (ir htmlAutoRenderer) isPlural() bool { - rv := reflect.Indirect(reflect.ValueOf(ir.model)) - rt := rv.Type() - switch rt.Kind() { - case reflect.Slice, reflect.Array, reflect.Map: - return true - } - return false -} diff --git a/render/auto_test.go~HEAD b/render/auto_test.go~HEAD deleted file mode 100644 index 052970810..000000000 --- a/render/auto_test.go~HEAD +++ /dev/null @@ -1,294 +0,0 @@ -package render_test - -import ( - "fmt" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/gobuffalo/buffalo" - "github.com/gobuffalo/buffalo/render" - "github.com/gobuffalo/packr" - "github.com/markbates/willie" - "github.com/stretchr/testify/require" -) - -type Car struct { - ID int - Name string -} - -type Cars []Car - -func Test_Auto_JSON(t *testing.T) { - r := require.New(t) - - app := buffalo.New(buffalo.Options{}) - app.GET("/cars", func(c buffalo.Context) error { - return c.Render(200, render.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"})) - }) - - w := willie.New(app) - - res := w.JSON("/cars").Get() - r.Equal(`["Honda","Toyota","Ford","Chevy"]`, strings.TrimSpace(res.Body.String())) -} - -func Test_Auto_XML(t *testing.T) { - r := require.New(t) - - app := buffalo.New(buffalo.Options{}) - app.GET("/cars", func(c buffalo.Context) error { - return c.Render(200, render.Auto(c, []string{"Honda", "Toyota", "Ford", "Chevy"})) - }) - - w := willie.New(app) - - res := w.XML("/cars").Get() - r.Equal("Honda\nToyota\nFord\nChevy", strings.TrimSpace(res.Body.String())) -} - -func Test_Auto_HTML_List(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/index.html", p), "INDEX: <%= len(cars) %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.GET(fmt.Sprintf("/%s", p), func(c buffalo.Context) error { - return c.Render(200, e.Auto(c, Cars{ - {Name: "Ford"}, - {Name: "Chevy"}, - })) - }) - - w := willie.New(app) - res := w.HTML("/%s", p).Get() - - r.Contains(res.Body.String(), "INDEX: 2") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_Show(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/show.html", p), "Show: <%= car.Name %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.GET(fmt.Sprintf("/%s/{id}", p), func(c buffalo.Context) error { - return c.Render(200, e.Auto(c, Car{Name: "Honda"})) - }) - - w := willie.New(app) - res := w.HTML("/%s/1", p).Get() - r.Contains(res.Body.String(), "Show: Honda") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_New(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/new.html", p), "New: <%= car.Name %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.GET(fmt.Sprintf("/%s/new", p), func(c buffalo.Context) error { - return c.Render(200, e.Auto(c, Car{Name: "Honda"})) - }) - - w := willie.New(app) - res := w.HTML("/%s/new", p).Get() - r.Contains(res.Body.String(), "New: Honda") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_Create(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/new.html", p), "New: <%= car.Name %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.POST(fmt.Sprintf("/%s", p), func(c buffalo.Context) error { - return c.Render(201, e.Auto(c, Car{Name: "Honda"})) - }) - - w := willie.New(app) - res := w.HTML("/%s", p).Post(nil) - r.Contains(res.Body.String(), "New: Honda") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_Create_Redirect(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - app := buffalo.New(buffalo.Options{}) - app.POST(fmt.Sprintf("/%s", p), func(c buffalo.Context) error { - return c.Render(201, render.Auto(c, Car{ - ID: 1, - Name: "Honda", - })) - }) - - w := willie.New(app) - res := w.HTML("/%s", p).Post(nil) - r.Equal(fmt.Sprintf("/%s/1", p), res.Location()) - r.Equal(302, res.Code) - } -} - -func Test_Auto_HTML_Create_Redirect_Error(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/new.html", p), "Create: <%= car.Name %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.POST(fmt.Sprintf("/%s", p), func(c buffalo.Context) error { - b := Car{ - Name: "Honda", - } - return c.Render(422, e.Auto(c, b)) - }) - - w := willie.New(app) - res := w.HTML("/%s", p).Post(nil) - r.Equal(422, res.Code) - r.Contains(res.Body.String(), "Create: Honda") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_Edit(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/edit.html", p), "Edit: <%= car.Name %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.GET(fmt.Sprintf("/%s/{id}/edit", p), func(c buffalo.Context) error { - return c.Render(200, e.Auto(c, Car{Name: "Honda"})) - }) - - w := willie.New(app) - res := w.HTML("/%s/1/edit", p).Get() - r.Contains(res.Body.String(), "Edit: Honda") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_Update(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/edit.html", p), "Update: <%= car.Name %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.PUT(fmt.Sprintf("/%s/{id}", p), func(c buffalo.Context) error { - return c.Render(422, e.Auto(c, Car{Name: "Honda"})) - }) - - w := willie.New(app) - res := w.HTML("/%s/1", p).Put(nil) - - r.Contains(res.Body.String(), "Update: Honda") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_Update_Redirect(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - app := buffalo.New(buffalo.Options{}) - app.PUT(fmt.Sprintf("/%s/{id}", p), func(c buffalo.Context) error { - b := Car{ - ID: 1, - Name: "Honda", - } - return c.Render(200, render.Auto(c, b)) - }) - - w := willie.New(app) - res := w.HTML("/%s/1", p).Put(nil) - r.Equal(fmt.Sprintf("/%s/1", p), res.Location()) - r.Equal(302, res.Code) - } -} - -func Test_Auto_HTML_Update_Redirect_Error(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - err := withHTMLFile(fmt.Sprintf("%s/edit.html", p), "Update: <%= car.Name %>", func(e *render.Engine) { - app := buffalo.New(buffalo.Options{}) - app.PUT(fmt.Sprintf("/%s/{id}", p), func(c buffalo.Context) error { - b := Car{ - ID: 1, - Name: "Honda", - } - return c.Render(422, e.Auto(c, b)) - }) - - w := willie.New(app) - res := w.HTML("/%s/1", p).Put(nil) - r.Equal(422, res.Code) - r.Contains(res.Body.String(), "Update: Honda") - }) - r.NoError(err) - } -} - -func Test_Auto_HTML_Destroy_Redirect(t *testing.T) { - r := require.New(t) - - for _, p := range []string{"cars", "admin/cars"} { - app := buffalo.New(buffalo.Options{}) - app.DELETE(fmt.Sprintf("/%s/{id}", p), func(c buffalo.Context) error { - b := Car{ - ID: 1, - Name: "Honda", - } - return c.Render(200, render.Auto(c, b)) - }) - - w := willie.New(app) - res := w.HTML("/%s/1", p).Delete() - r.Equal(fmt.Sprintf("/%s", p), res.Location()) - r.Equal(302, res.Code) - } -} - -func withHTMLFile(name string, contents string, fn func(*render.Engine)) error { - tmpDir := filepath.Join(os.TempDir(), filepath.Dir(name)) - err := os.MkdirAll(tmpDir, 0766) - if err != nil { - return err - } - defer os.Remove(tmpDir) - - tmpFile, err := os.Create(filepath.Join(tmpDir, filepath.Base(name))) - if err != nil { - return err - } - defer os.Remove(tmpFile.Name()) - - _, err = tmpFile.Write([]byte(contents)) - if err != nil { - return err - } - - e := render.New(render.Options{ - TemplatesBox: packr.NewBox(os.TempDir()), - }) - - fn(e) - return nil -}