简体   繁体   中英

“Generate resource” command with “--skip-model” flag results in faulty resource

Description

Just started using Buffalo, trying out all the beautiful features:)

I'm having an issue however with the "generate resource" command in combination with the "--skip-model" flag. When this flag is used, all the generated functions ("List", "Show", "Create", "Update" and "Destroy") are created fully in lowercase. The struct however that is also generated refers to "buffalo.Resource" and contains these functions with the first letter in uppercase, resulting in a resource that doesn't work.

Steps to Reproduce the Problem

  1. Use generate resource command with "--skip-model" flag: buffalo g r todo --skip-model .
  2. Run the application using: buffalo dev .
  3. Navigate to "http://127.0.0.1:3000/todoes"; verify that you get an error saying "runtime error: invalid memory address or nil pointer dereference".
  4. Verify in the generated file that "todoes.go" contains the generated functions ("List", "Show", "Create", "Update" and "Destroy") fully in lowercase, while the generated struct called "TodoesResource" refers to "buffalo.Resource" and contains these functions with the first letter in uppercase.

Expected Behavior

I expected the generated functions to have the first letter in uppercase, matching the names in "buffalo.Resource" and resulting in the response "Todo#list" when navigating to "http://127.0.0.1:3000/todoes" (after starting the application). This is the case when you don't use the "--skip-model" flag, so I'm not sure why this would behave differently when you do use this flag.

Actual Behavior

The generated functions ("List", "Show", "Create", "Update" and "Destroy") are fully in lowercase, while the generated struct called "TodoesResource" refers to "buffalo.Resource" and contains these functions with the first letter in uppercase. This results in the error "runtime error: invalid memory address or nil pointer dereference" when navigating to "http://127.0.0.1:3000/todoes" (after starting the application).

Suggested solution(s)

I'm not able to create a pull request (as I get the error "Permission to gobuffalo/buffalo.git denied" when trying to publish a branch), but I think there are two possible solutions to this issue:

Preferred solution

Modifying the file "genny/resource/templates/standard/action/resource-name.go.tmpl" to change the code below:

// {{$a.String}} default implementation.
func (v {{$.opts.Name.Resource}}Resource) {{$a.String}}(c buffalo.Context) error {
  return c.Render(http.StatusOK, r.String("{{$.opts.Model.Proper}}#{{$a.String}}"))
}

And change this to:

// {{$a.Pascalize}} default implementation.
func (v {{$.opts.Name.Resource}}Resource) {{$a.Pascalize}}(c buffalo.Context) error {
  return c.Render(http.StatusOK, r.String("{{$.opts.Model.Proper}}#{{$a.Pascalize}}"))
}

Alternative solution

Modifying the file "genny/resource/actions.go" to change the code below:

func actions(opts *Options) []name.Ident {
    actions := []name.Ident{
        name.New("list"),
        name.New("show"),
        name.New("create"),
        name.New("update"),
        name.New("destroy"),
    }
    if opts.App.AsWeb {
        actions = append(actions, name.New("new"), name.New("edit"))
    }
    return actions
}

And change this to:

func actions(opts *Options) []name.Ident {
    actions := []name.Ident{
        name.New("List"),
        name.New("Show"),
        name.New("Create"),
        name.New("Update"),
        name.New("Destroy"),
    }
    if opts.App.AsWeb {
        actions = append(actions, name.New("New"), name.New("Edit"))
    }
    return actions
}

This was a bug and is currently being fixed. Also see: https://github.com/gobuffalo/buffalo/issues/2023 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM