Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wymli opened this issue Jan 22, 2023 · 1 comment
Open

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

wymli opened this issue Jan 22, 2023 · 1 comment
Labels
bug Something isn't working help wanted We'd love your help! improvement New feature or request

Comments

@wymli
Copy link

wymli commented Jan 22, 2023

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
Copy link
Contributor

matthewmueller commented Jan 24, 2023

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: #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.

func (c *newController) Route() string {
if c.route != "" {
return c.route
}
segments := strings.Split(c.key, "/")
path := new(strings.Builder)
for i := 0; i < len(segments); i++ {
if i%2 != 0 {
path.WriteString("/")
path.WriteString(":" + text.Slug(text.Singular(segments[i-1])) + "_id")
path.WriteString("/")
}
path.WriteString(text.Slug(segments[i]))
}
c.route = strings.TrimSuffix("/"+path.String(), "/")
return c.route
}
func (c *newController) IndexPath() string {
if c.indexPath != "" {
return c.indexPath
}
segments := strings.Split(c.key, "/")
path := new(strings.Builder)
for i := 0; i < len(segments); i++ {
if i%2 != 0 {
path.WriteString("/")
path.WriteString("${" + c.Singular + "." + text.Slug(text.Singular(segments[i-1])) + "_id || 0}")
path.WriteString("/")
}
path.WriteString(text.Slug(segments[i]))
}
c.indexPath = "/" + strings.TrimSuffix(path.String(), "/")
return c.indexPath
}

Accepting PRs!

@matthewmueller matthewmueller added bug Something isn't working improvement New feature or request help wanted We'd love your help! labels Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted We'd love your help! improvement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants