livebud / bud

The Full-Stack Web Framework for Go
MIT License
5.53k stars 182 forks source link

How to change default id:int to name:string in generated url path? #363

Open wymli opened 1 year ago

wymli commented 1 year ago

First of all thank you very much for the tool, very useful.
I have some questions waiting for your help.

1. how to change id:int to name:string?

If I run the following commands to generate controller and view, I got a generated path like // GET /pscluster/:pscluster_id/psmeta/:id. Do we have some cli flags to change ":pscluter_id int" to ":pscluster_name string"?

bud new controller pscluster index new create show edit update delete
bud new controller pscluster/psmeta create show edit update delete

// GET /pscluster/:pscluster_id/psmeta/:id

2. why this strange name generated?

when I run bud new controller pscluster/psmeta create show edit update delete, I got a strange struct name, which change psmeta to psmetum

// Psmetum struct
type Psmetum struct {
    ID int `json:"id"`
}

// Create psmetum
// POST /pscluster/:pscluster_id/psmeta
func (c *Controller) Create(ctx context.Context) (psmetum *Psmetum, err error) {
    return &Psmetum{
        ID: 0,
    }, nil
}

3. Underscores become dashes in url

bud new controller ps_cluster index new create show edit update delete
// GET /ps-cluster
matthewmueller commented 1 year ago

Hey @wymli, thanks for trying Bud!

  1. how to change id:int to name:string?

This will improve soon with support for custom routing. I believe this is similar to: https://github.com/livebud/bud/issues/170.

If you're just looking to fill in a different field for your struct, I think you could alter the json tag to use pscluster_id.

Something like (untested):

type ShowInput struct {
  Name string `json:"pscluster_id,omitempty"`
}

func (c *Controller) Show(in *ShowInput) { 
  // ...
}
  1. why this strange name generated?

That looks strange indeed! If you have a chance, a failing test would be appreciated in https://github.com/livebud/bud/blob/transpiler/internal/cli/new_controller_test.go!

Otherwise, I'll have a look when I revisit the controller implementation!

  1. Underscores become dashes in url

Hmm, yah. I guess we probably want snake case here instead of slug case. https://github.com/livebud/bud/blob/bb1741fda309f9402c130d22ac5775049f51a175/internal/cli/new_controller.go#L193-L228

Accepting PRs!