Typo-proof names
wardn already rejects unknown permissions at startup. wardn gen goes one step further: when you use its constants, a misspelled permission is a compile error. (A misspelled string literal, such as Require("loan:aprove"), still compiles, and is caught at startup instead.)
Generate
Section titled “Generate”Add the CLI as a tool of your module, so its version always matches the library’s:
go get -tool github.com/MogRex-Limited/wardn/cmd/wardngo tool wardn gen # reads wardn.yaml, writes perm/perm.gogo tool wardn gen -o internal/authz/names.goOr keep it next to the code that uses it:
//go:generate go tool wardn gen -f ../wardn.yaml -o perm/perm.gor.With(w.Require(perm.LoanApprove)).Post("/loans/{id}/approve", approveLoan)
w.System().Assign(ctx, userID, perm.RoleTeller, perm.ScopeBranch("lagos"))The generated package contains:
| From | Becomes |
|---|---|
Permission loan:approve |
perm.LoanApprove |
Permission loan:approve_own |
perm.LoanApproveOwn |
Built-in wardn:roles.assign |
perm.WardnRolesAssign |
Role teller |
perm.RoleTeller |
Scope type branch |
perm.ScopeBranch(id) |
Descriptions from your policy become doc comments. If two names would become the same Go identifier, wardn gen stops and names both.
Keep it current in CI
Section titled “Keep it current in CI”go tool wardn gen -check # fails if perm/perm.go is out of date with wardn.yamlgo tool wardn check # fails if wardn.yaml is invalidNothing in a policy file can inject code into the output: identifiers are built only from characters policy names allow, values are quoted, and descriptions can’t contain line breaks.
Questions about this page?
Ask below, point out something wrong or unclear, or suggest an improvement. Reading needs no account; to post, sign in with GitHub.