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

Generate less boilerplate code when making a new Go module #4668

Merged

Conversation

penguinland
Copy link
Member

This is a follow-up to #4455.

Nearly all modules can really have a 1-line main function, rather than all the boilerplate that gets generated in the current version.

Tried locally: running ./main module generate can make a new codebase whose main.go looks like this:

package main

import (
	"branch-module-one-space/models"
	"go.viam.com/rdk/module"
	"go.viam.com/rdk/resource"
	"go.viam.com/rdk/components/audioinput"
)

func main() {
	// ModularMain can take multiple APIModel arguments, if your module implements multiple models.
	module.ModularMain(resource.APIModel{ audioinput.API, models.BranchModelOnespace})
}

Running make not only successfully compiles everything, but main.go gets cleaned up like this (note the reordering of the imports, and the space removed from the APIModel constructor):

package main

import (
	"branch-module-one-space/models"
	"go.viam.com/rdk/components/audioinput"
	"go.viam.com/rdk/module"
	"go.viam.com/rdk/resource"
)

func main() {
	// ModularMain can take multiple APIModel arguments, if your module implements multiple models.
	module.ModularMain(resource.APIModel{audioinput.API, models.BranchModelOnespace})
}

@penguinland penguinland requested a review from jckras January 2, 2025 18:46
@penguinland penguinland requested a review from a team as a code owner January 2, 2025 18:46
@viambot viambot added the safe to test This pull request is marked safe to test from a trusted zone label Jan 2, 2025
<-ctx.Done()
return nil
// ModularMain can take multiple APIModel arguments, if your module implements multiple models.
module.ModularMain(resource.APIModel{ {{.ResourceSubtype}}.API, models.{{.ModelPascal}}})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I don't put a space between the { and the {{, it fails with Error: Unable to generate module: template: tmpl-main.go:12: unexpected "{" in command. I'd prefer to remove the space, but don't know how to get that to work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, \{{{ gives the same error, and {\{{ puts a literal backslash in the generated main.go, resulting in compiler errors later. Perhaps having the space is the least bad option. 😞

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the space doesn't bother me too much! i agree i think its the best option.

Copy link
Member

@jckras jckras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! one question - you said "nearly all modules can have a 1-line main function", so how does this change affect the modules that need more than this?

@penguinland
Copy link
Member Author

penguinland commented Jan 2, 2025

TBH, I said "nearly" because I was afraid of saying "all." 🙃 Show me a module where this doesn't work, and I'm confident its creator knows to do something different.

edit: ModularMain is the same as what this template used to be. If it doesn't work, the module creator would have needed to redo parts of main.go anyway.

@penguinland penguinland force-pushed the less_boilerplate_generated branch from 2f4ef4f to 81d257a Compare January 3, 2025 21:29
@viambot viambot added safe to test This pull request is marked safe to test from a trusted zone and removed safe to test This pull request is marked safe to test from a trusted zone labels Jan 3, 2025
@penguinland
Copy link
Member Author

Rebased to fix conflicts with #4670...

@penguinland penguinland merged commit af605e7 into viamrobotics:main Jan 3, 2025
16 checks passed
@penguinland penguinland deleted the less_boilerplate_generated branch January 3, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test This pull request is marked safe to test from a trusted zone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants